@native-sdk/cli 0.7.1 → 0.8.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/build/app.zig +273 -65
- package/build/ts_run.mjs +6 -8
- package/build.zig +341 -361
- package/native-sdk.d.ts +6 -3
- package/package.json +13 -11
- package/packages/core/compile-surface/core.ts +559 -0
- package/packages/core/package-lock.json +375 -12
- package/packages/core/package.json +9 -6
- package/packages/core/scripts/compiler_typecheck.mjs +58 -0
- package/packages/core/scripts/gen_case_tables.mjs +130 -0
- package/packages/core/scripts/gen_declarations.mjs +97 -0
- package/packages/core/scripts/run_external_core_compiler.mjs +108 -0
- package/packages/core/scripts/stage_external_core.mjs +148 -0
- package/packages/core/sdk/core.d.ts +363 -0
- package/packages/core/sdk/events.d.ts +65 -0
- package/packages/core/sdk/text.d.ts +51 -0
- package/packages/core/src/checker.ts +3 -3
- package/packages/core/src/cli.ts +40 -38
- package/packages/core/src/contract.ts +749 -0
- package/packages/core/src/devhost.ts +4 -3
- package/packages/core/src/diagnostics.ts +5 -7
- package/packages/core/src/{transpile.ts → frontend.ts} +38 -50
- package/packages/core/src/infer.ts +1 -1
- package/packages/core/src/typed_ast.ts +16 -15
- package/packages/core/src/wyhash.ts +79 -0
- package/skill-data/native-ui/SKILL.md +4 -2
- package/skill-data/ts-core/SKILL.md +29 -29
- package/src/platform/macos/appkit_host.m +51 -3
- package/src/platform/types.zig +4 -0
- package/src/platform/windows/gpu_surface_renderer.cpp +2280 -0
- package/src/platform/windows/gpu_surface_renderer.h +86 -0
- package/src/platform/windows/root.zig +298 -20
- package/src/platform/windows/webview2_host.cpp +343 -33
- package/src/primitives/canvas/code.zig +50 -0
- package/src/primitives/canvas/code_tests.zig +174 -1
- package/src/primitives/canvas/frame.zig +45 -1
- package/src/primitives/canvas/gpu.zig +6 -0
- package/src/primitives/canvas/render_tests.zig +93 -0
- package/src/primitives/canvas/root.zig +2 -0
- package/src/primitives/canvas/serialization.zig +150 -21
- package/src/primitives/canvas/themes/geist.zig +51 -5
- package/src/primitives/canvas/tokens.zig +33 -4
- package/src/primitives/canvas/ui.zig +52 -7
- package/src/primitives/canvas/ui_markup.zig +38 -1
- package/src/primitives/canvas/ui_markup_compiled.zig +28 -0
- package/src/primitives/canvas/ui_markup_contract.zig +9 -0
- package/src/primitives/canvas/ui_markup_view.zig +28 -0
- package/src/primitives/canvas/ui_markup_view_tests.zig +10 -2
- package/src/primitives/canvas/ui_schema.zig +5 -0
- package/src/primitives/canvas/ui_schema_tests.zig +4 -3
- package/src/primitives/canvas/ui_tests.zig +14 -0
- package/src/primitives/canvas/widget_builtin_tests.zig +513 -13
- package/src/primitives/canvas/widget_invalidation.zig +11 -1
- package/src/primitives/canvas/widget_layout.zig +260 -21
- package/src/primitives/canvas/widget_metrics.zig +45 -9
- package/src/primitives/canvas/widget_render.zig +220 -70
- package/src/primitives/canvas/widget_render_controls.zig +79 -21
- package/src/primitives/canvas/widget_render_style.zig +52 -8
- package/src/primitives/canvas/widget_text_input.zig +2 -0
- package/src/primitives/canvas/widgets.zig +53 -2
- package/src/runtime/canvas_frame.zig +60 -7
- package/src/runtime/canvas_frame_invalidation_tests.zig +80 -0
- package/src/runtime/canvas_frame_packet_tests.zig +83 -9
- package/src/runtime/canvas_frame_patch_tests.zig +16 -0
- package/src/runtime/canvas_images.zig +8 -2
- package/src/runtime/canvas_widget_clipboard_tests.zig +1 -1
- package/src/runtime/canvas_widget_runtime.zig +11 -8
- package/src/runtime/effects.zig +6 -0
- package/src/runtime/effects_tests.zig +30 -0
- package/src/runtime/gpu_surface_events.zig +38 -7
- package/src/runtime/media_surface_tests.zig +1 -1
- package/src/runtime/runtime_event_tests.zig +74 -0
- package/src/runtime/ts_core_host.zig +38 -38
- package/src/runtime/ts_ui_app.zig +12 -12
- package/src/runtime/ui_app.zig +10 -8
- package/src/runtime/ui_app_tests.zig +67 -0
- package/src/runtime/view.zig +6 -0
- package/src/runtime/view_canvas.zig +15 -0
- package/src/runtime/view_widget_text.zig +6 -4
- package/src/runtime/window_view_runtime.zig +1 -0
- package/src/tooling/manifest.zig +32 -0
- package/src/tooling/raw_manifest.zig +4 -0
- package/src/tooling/templates.zig +34 -26
- package/src/tooling/ts_core.zig +55 -34
- package/src/tooling/verbs.zig +1 -1
- package/tools/corewire/core_abi.zig +169 -0
- package/tools/corewire/emit.zig +2418 -0
- package/tools/corewire/emit_facade.zig +3984 -0
- package/tools/corewire/emit_profile.zig +501 -0
- package/tools/corewire/main.zig +561 -0
- package/tools/corewire/shim_rt.zig +632 -0
- package/tools/corewire/sidecar.zig +2507 -0
- package/packages/core/rt/rt.zig +0 -1986
- package/packages/core/src/emitter.ts +0 -11898
|
@@ -0,0 +1,749 @@
|
|
|
1
|
+
// The contract sidecar (core.contract.json, schema format 1), emitted
|
|
2
|
+
// DIRECTLY from frontend analysis: the type table, integer inference,
|
|
3
|
+
// and the entry module's channel exports carry every fact the document
|
|
4
|
+
// states, so no Zig emission sits in the loop.
|
|
5
|
+
//
|
|
6
|
+
// This is the same document tools/corewire/extract.zig reflects out of
|
|
7
|
+
// a transpiler-emitted core module, byte for byte: section layout,
|
|
8
|
+
// naming (synthesized `<Container>_<member>` records included), slot
|
|
9
|
+
// paths, origin facts, and the synthesized Wyhash identities all follow
|
|
10
|
+
// the extractor's construction, and the build pins the equivalence per
|
|
11
|
+
// fixture (test-contract-equivalence) for as long as both producers
|
|
12
|
+
// exist. Keeping the two in lockstep is deliberate: corewire's facade,
|
|
13
|
+
// profile, and mirror projections consume this document, and the
|
|
14
|
+
// external-compile lane must see exactly the contract the transpiler
|
|
15
|
+
// lane attests.
|
|
16
|
+
//
|
|
17
|
+
// Identity fields are synthesized deterministically (a frontend sidecar
|
|
18
|
+
// attests no real compile): hashes derive from the entry path and the
|
|
19
|
+
// emitted surface, so re-runs are byte-identical and a core edit moves
|
|
20
|
+
// them.
|
|
21
|
+
|
|
22
|
+
import { ts, hasExportModifier, exportListBindings, sdkCoreModulePath, type TypedAst } from "./typed_ast.ts";
|
|
23
|
+
import { TypeTable, type ZType, type ZField, type UnionArm } from "./types.ts";
|
|
24
|
+
import type { IntInference } from "./infer.ts";
|
|
25
|
+
import type { CheckResult } from "./checker.ts";
|
|
26
|
+
import { wyhashHex } from "./wyhash.ts";
|
|
27
|
+
import path from "node:path";
|
|
28
|
+
|
|
29
|
+
/// A contract fact the schema cannot carry (the extractor's
|
|
30
|
+
/// @compileError twin): thrown with the offending node for a teaching
|
|
31
|
+
/// diagnostic at the caller.
|
|
32
|
+
export class ContractError extends Error {
|
|
33
|
+
readonly node: ts.Node;
|
|
34
|
+
constructor(message: string, node: ts.Node) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.node = node;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// The reflected-type view: what the emitted Zig type WOULD reflect as
|
|
41
|
+
/// (aliases erased, numbers classed per slot, storage kind resolved) —
|
|
42
|
+
/// the vocabulary the sidecar's TypeRef family spells.
|
|
43
|
+
type RType =
|
|
44
|
+
| { readonly k: "bool" | "f64" | "i64" | "bytes" }
|
|
45
|
+
| { readonly k: "optional"; readonly inner: RType }
|
|
46
|
+
| { readonly k: "slice"; readonly elem: RType }
|
|
47
|
+
| { readonly k: "value" | "node" | "enum" | "union"; readonly name: string };
|
|
48
|
+
|
|
49
|
+
interface TableField {
|
|
50
|
+
readonly name: string;
|
|
51
|
+
readonly rt: RType;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface TableArm {
|
|
55
|
+
readonly tag: string;
|
|
56
|
+
/// The authored member name of a single-payload arm; null otherwise.
|
|
57
|
+
readonly member: string | null;
|
|
58
|
+
/// Null payload = void arm.
|
|
59
|
+
readonly payload: RType | null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type TableEntry =
|
|
63
|
+
| { readonly kind: "struct"; readonly name: string; readonly fields: readonly TableField[] }
|
|
64
|
+
| { readonly kind: "enum"; readonly name: string; readonly members: readonly string[] }
|
|
65
|
+
| { readonly kind: "union"; readonly name: string; readonly arms: readonly TableArm[] };
|
|
66
|
+
|
|
67
|
+
export interface ContractInput {
|
|
68
|
+
readonly tast: TypedAst;
|
|
69
|
+
readonly table: TypeTable;
|
|
70
|
+
readonly infer: IntInference;
|
|
71
|
+
readonly checkResult: CheckResult;
|
|
72
|
+
readonly files: readonly ts.SourceFile[];
|
|
73
|
+
/// The entry spelling the document states (the extractor takes the
|
|
74
|
+
/// build-root-relative fixture path; app builds state the app-relative
|
|
75
|
+
/// core path). Never a filesystem-absolute leak.
|
|
76
|
+
readonly entry: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// A JSON string literal: quoted and escaped exactly as the extractor's
|
|
80
|
+
/// `js` helper spells it (JSON.stringify would spell control characters
|
|
81
|
+
/// differently — \b instead of ).
|
|
82
|
+
function js(text: string): string {
|
|
83
|
+
let out = '"';
|
|
84
|
+
for (const ch of text) {
|
|
85
|
+
const code = ch.codePointAt(0)!;
|
|
86
|
+
if (ch === '"') out += '\\"';
|
|
87
|
+
else if (ch === "\\") out += "\\\\";
|
|
88
|
+
else if (ch === "\n") out += "\\n";
|
|
89
|
+
else if (ch === "\r") out += "\\r";
|
|
90
|
+
else if (ch === "\t") out += "\\t";
|
|
91
|
+
else if (code < 0x20) out += `\\u${code.toString(16).padStart(4, "0")}`;
|
|
92
|
+
else out += ch;
|
|
93
|
+
}
|
|
94
|
+
return out + '"';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function boolJson(value: boolean): string {
|
|
98
|
+
return value ? "true" : "false";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function emitContractSidecar(input: ContractInput): string {
|
|
102
|
+
return new ContractEmitter(input).emit();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
class ContractEmitter {
|
|
106
|
+
private readonly tast: TypedAst;
|
|
107
|
+
private readonly table: TypeTable;
|
|
108
|
+
private readonly infer: IntInference;
|
|
109
|
+
private readonly checkResult: CheckResult;
|
|
110
|
+
private readonly files: readonly ts.SourceFile[];
|
|
111
|
+
private readonly entryFile: ts.SourceFile;
|
|
112
|
+
private readonly entry: string;
|
|
113
|
+
|
|
114
|
+
private readonly entries: TableEntry[] = [];
|
|
115
|
+
private readonly listed = new Set<string>();
|
|
116
|
+
private readonly slots: string[] = [];
|
|
117
|
+
/// Origin fragment per table name (`, "origin": ...` [+ privacy]).
|
|
118
|
+
private readonly origins = new Map<string, string>();
|
|
119
|
+
|
|
120
|
+
constructor(input: ContractInput) {
|
|
121
|
+
this.tast = input.tast;
|
|
122
|
+
this.table = input.table;
|
|
123
|
+
this.infer = input.infer;
|
|
124
|
+
this.checkResult = input.checkResult;
|
|
125
|
+
this.files = input.files;
|
|
126
|
+
this.entryFile = input.files[0];
|
|
127
|
+
this.entry = input.entry;
|
|
128
|
+
this.collectOrigins();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ------------------------------------------------------------ reflection
|
|
132
|
+
|
|
133
|
+
/// The reflected type of a slot: numbers class through the inference
|
|
134
|
+
/// result at the slot's own declaration (top level and through one
|
|
135
|
+
/// optional wrapper, exactly the emitter's typeRefWithNumbers rule —
|
|
136
|
+
/// deeper positions stay f64), aliases erase, storage kind resolves.
|
|
137
|
+
private reflect(t: ZType, decl: ts.Node | null, top: boolean): RType {
|
|
138
|
+
switch (t.k) {
|
|
139
|
+
case "number": {
|
|
140
|
+
const cls = top && decl ? (this.infer.classOfDecl(decl) ?? "f64") : "f64";
|
|
141
|
+
return { k: cls };
|
|
142
|
+
}
|
|
143
|
+
case "i64":
|
|
144
|
+
return { k: "i64" };
|
|
145
|
+
case "f64":
|
|
146
|
+
return { k: "f64" };
|
|
147
|
+
case "bool":
|
|
148
|
+
return { k: "bool" };
|
|
149
|
+
case "bytes":
|
|
150
|
+
case "string":
|
|
151
|
+
return { k: "bytes" };
|
|
152
|
+
case "numAlias": {
|
|
153
|
+
if (t.repr === "i64") return { k: "i64" };
|
|
154
|
+
throw new ContractError(
|
|
155
|
+
`the contract schema has no TypeRef form for the u8-repr number alias \`${t.name}\` — use a string-literal union (an enum) or widen the values past 255 so the alias spells i64`,
|
|
156
|
+
this.declOfTableName(t.name) ?? this.entryFile,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
case "enum":
|
|
160
|
+
return { k: "enum", name: t.name };
|
|
161
|
+
case "union":
|
|
162
|
+
return { k: "union", name: t.name };
|
|
163
|
+
case "struct":
|
|
164
|
+
return { k: this.table.isPointerStruct(t.name) ? "node" : "value", name: t.name };
|
|
165
|
+
case "slice":
|
|
166
|
+
return { k: "slice", elem: this.reflect(t.elem, decl, false) };
|
|
167
|
+
case "optional":
|
|
168
|
+
return { k: "optional", inner: this.reflect(t.inner, decl, top) };
|
|
169
|
+
case "void":
|
|
170
|
+
throw new ContractError("a contract slot resolved to no subset type (internal)", this.entryFile);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private declOfTableName(name: string): ts.Node | null {
|
|
175
|
+
return (
|
|
176
|
+
this.table.structs.get(name)?.decl ??
|
|
177
|
+
this.table.unions.get(name)?.decl ??
|
|
178
|
+
this.table.enums.get(name)?.decl ??
|
|
179
|
+
this.table.numAliases.get(name)?.decl ??
|
|
180
|
+
null
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/// Whether a reflected type spells i64 at its own slot (through
|
|
185
|
+
/// optionals) — the extractor's `spellsI64`.
|
|
186
|
+
private spellsI64(rt: RType): boolean {
|
|
187
|
+
if (rt.k === "i64") return true;
|
|
188
|
+
if (rt.k === "optional") return this.spellsI64(rt.inner);
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private typeRefJson(rt: RType): string {
|
|
193
|
+
switch (rt.k) {
|
|
194
|
+
case "bool":
|
|
195
|
+
return '{"kind": "bool"}';
|
|
196
|
+
case "f64":
|
|
197
|
+
return '{"kind": "f64"}';
|
|
198
|
+
case "i64":
|
|
199
|
+
return '{"kind": "i64"}';
|
|
200
|
+
case "bytes":
|
|
201
|
+
return '{"kind": "bytes"}';
|
|
202
|
+
case "optional":
|
|
203
|
+
return `{"kind": "optional", "inner": ${this.typeRefJson(rt.inner)}}`;
|
|
204
|
+
case "slice":
|
|
205
|
+
return `{"kind": "slice", "elem": ${this.typeRefJson(rt.elem)}}`;
|
|
206
|
+
case "node":
|
|
207
|
+
return `{"kind": "node", "name": ${js(rt.name)}}`;
|
|
208
|
+
case "value":
|
|
209
|
+
return `{"kind": "value", "name": ${js(rt.name)}}`;
|
|
210
|
+
case "enum":
|
|
211
|
+
return `{"kind": "enum", "name": ${js(rt.name)}}`;
|
|
212
|
+
case "union":
|
|
213
|
+
return `{"kind": "union", "name": ${js(rt.name)}}`;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private appendSlot(pathSpelling: string): void {
|
|
218
|
+
this.slots.push(`{"slot": ${js(pathSpelling)}, "class": "i64"}`);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// --------------------------------------------------------------- origins
|
|
222
|
+
|
|
223
|
+
/// The declaring module of every named table type: SDK modules spell
|
|
224
|
+
/// their shipped staging path, core modules their
|
|
225
|
+
/// entry-relative POSIX path; a private declaration carries the
|
|
226
|
+
/// additive `"exported": false` marker; synthesized names stay out.
|
|
227
|
+
private collectOrigins(): void {
|
|
228
|
+
const sdkDir = path.resolve(path.dirname(sdkCoreModulePath));
|
|
229
|
+
const entryDir = path.dirname(path.resolve(this.entryFile.fileName));
|
|
230
|
+
for (const file of this.files) {
|
|
231
|
+
for (const stmt of file.statements) {
|
|
232
|
+
if (!ts.isInterfaceDeclaration(stmt) && !ts.isTypeAliasDeclaration(stmt) && !ts.isClassDeclaration(stmt)) continue;
|
|
233
|
+
if (!stmt.name) continue;
|
|
234
|
+
const name = stmt.name.text;
|
|
235
|
+
if (this.origins.has(name)) continue;
|
|
236
|
+
if (!this.table.structs.has(name) && !this.table.enums.has(name) && !this.table.unions.has(name)) continue;
|
|
237
|
+
if (this.table.genericStructTemplates.has(name) || this.table.genericAliasTemplates.has(name)) continue;
|
|
238
|
+
const fileName = path.resolve(file.fileName);
|
|
239
|
+
const origin =
|
|
240
|
+
path.dirname(fileName) === sdkDir
|
|
241
|
+
? `sdk/${path.basename(fileName)}`
|
|
242
|
+
: path.relative(entryDir, fileName).split(path.sep).join("/");
|
|
243
|
+
const exported =
|
|
244
|
+
hasExportModifier(stmt) ||
|
|
245
|
+
file.statements.some(
|
|
246
|
+
(candidate) =>
|
|
247
|
+
ts.isExportDeclaration(candidate) &&
|
|
248
|
+
candidate.moduleSpecifier === undefined &&
|
|
249
|
+
candidate.exportClause !== undefined &&
|
|
250
|
+
ts.isNamedExports(candidate.exportClause) &&
|
|
251
|
+
candidate.exportClause.elements.some(
|
|
252
|
+
(spec) => spec.name.text === name && this.tast.exportSpecifierTarget(spec) === stmt,
|
|
253
|
+
),
|
|
254
|
+
);
|
|
255
|
+
this.origins.set(name, `, "origin": ${js(origin)}${exported ? "" : ', "exported": false'}`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private originFragment(name: string): string {
|
|
261
|
+
return this.origins.get(name) ?? "";
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ------------------------------------------------------------- reach set
|
|
265
|
+
|
|
266
|
+
/// Add every table-worthy type reachable from `t`, in first-visit
|
|
267
|
+
/// order (the extractor's ReachSet.collect). `container`/`member`
|
|
268
|
+
/// name synthesized records at their reference site.
|
|
269
|
+
private collect(t: ZType, decl: ts.Node | null, container: string, member: string): void {
|
|
270
|
+
switch (t.k) {
|
|
271
|
+
case "number":
|
|
272
|
+
case "i64":
|
|
273
|
+
case "f64":
|
|
274
|
+
case "bool":
|
|
275
|
+
case "bytes":
|
|
276
|
+
case "string":
|
|
277
|
+
case "numAlias":
|
|
278
|
+
case "void":
|
|
279
|
+
return;
|
|
280
|
+
case "optional":
|
|
281
|
+
return this.collect(t.inner, decl, container, member);
|
|
282
|
+
case "slice":
|
|
283
|
+
return this.collect(t.elem, decl, container, member);
|
|
284
|
+
case "enum": {
|
|
285
|
+
if (this.listed.has(t.name)) return;
|
|
286
|
+
this.listed.add(t.name);
|
|
287
|
+
const info = this.table.enums.get(t.name);
|
|
288
|
+
this.entries.push({ kind: "enum", name: t.name, members: info?.members ?? t.members });
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
case "struct": {
|
|
292
|
+
if (this.listed.has(t.name)) return;
|
|
293
|
+
this.listed.add(t.name);
|
|
294
|
+
const info = this.table.structs.get(t.name);
|
|
295
|
+
if (!info) throw new ContractError(`the type table carries no record named \`${t.name}\` (internal)`, this.entryFile);
|
|
296
|
+
this.entries.push({
|
|
297
|
+
kind: "struct",
|
|
298
|
+
name: t.name,
|
|
299
|
+
fields: info.fields.map((f) => ({ name: f.tsName, rt: this.reflect(f.type, f.decl, true) })),
|
|
300
|
+
});
|
|
301
|
+
for (const f of info.fields) this.collect(f.type, f.decl, t.name, f.tsName);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
case "union": {
|
|
305
|
+
if (this.listed.has(t.name)) return;
|
|
306
|
+
this.listed.add(t.name);
|
|
307
|
+
const info = this.table.unions.get(t.name);
|
|
308
|
+
if (!info) throw new ContractError(`the type table carries no union named \`${t.name}\` (internal)`, this.entryFile);
|
|
309
|
+
this.entries.push({
|
|
310
|
+
kind: "union",
|
|
311
|
+
name: t.name,
|
|
312
|
+
arms: info.arms.map((arm) => this.tableArm(t.name, arm)),
|
|
313
|
+
});
|
|
314
|
+
for (const arm of info.arms) this.collectArmPayload(t.name, arm);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/// One tabled union arm: bare payloads keep their reflected type, a
|
|
321
|
+
/// multi-field payload references its synthesized record.
|
|
322
|
+
private tableArm(unionName: string, arm: UnionArm): TableArm {
|
|
323
|
+
if (arm.fields.length === 0) return { tag: arm.tag, member: null, payload: null };
|
|
324
|
+
if (arm.fields.length === 1) {
|
|
325
|
+
const f = arm.fields[0];
|
|
326
|
+
return { tag: arm.tag, member: f.tsName, payload: this.reflect(f.type, f.decl, true) };
|
|
327
|
+
}
|
|
328
|
+
return { tag: arm.tag, member: null, payload: { k: "value", name: `${unionName}_${arm.tag}` } };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/// The reach walk through one tabled arm's payload — a synthesized
|
|
332
|
+
/// record entry lands at its first-reference position, exactly where
|
|
333
|
+
/// the extractor's DFS places it.
|
|
334
|
+
private collectArmPayload(unionName: string, arm: UnionArm): void {
|
|
335
|
+
if (arm.fields.length === 0) return;
|
|
336
|
+
if (arm.fields.length === 1) {
|
|
337
|
+
const f = arm.fields[0];
|
|
338
|
+
this.collect(f.type, f.decl, unionName, arm.tag);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
this.collectSynthesized(`${unionName}_${arm.tag}`, arm.fields);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/// Table a synthesized (anonymous in the emitted Zig) record under its
|
|
345
|
+
/// deterministic `<Container>_<member>` name, then walk its fields.
|
|
346
|
+
private collectSynthesized(name: string, fields: readonly ZField[]): void {
|
|
347
|
+
if (this.listed.has(name)) return;
|
|
348
|
+
this.listed.add(name);
|
|
349
|
+
this.entries.push({
|
|
350
|
+
kind: "struct",
|
|
351
|
+
name,
|
|
352
|
+
fields: fields.map((f) => ({ name: f.tsName, rt: this.reflect(f.type, f.decl, true) })),
|
|
353
|
+
});
|
|
354
|
+
for (const f of fields) this.collect(f.type, f.decl, name, f.tsName);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// ------------------------------------------------------- entry-module facts
|
|
358
|
+
|
|
359
|
+
/// Whether a declaration is exported by the modifier or an un-renamed
|
|
360
|
+
/// export list entry (the emitter's isExportedDecl twin).
|
|
361
|
+
private isListExported(decl: ts.Node, file: ts.SourceFile): boolean {
|
|
362
|
+
return exportListBindings(this.tast, file).some((b) => !b.renamed && b.target === decl);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/// The entry module's exported function under `name` — a function
|
|
366
|
+
/// declaration exported directly, or an export-list binding (renamed
|
|
367
|
+
/// included: the emitted alias binds the exported name).
|
|
368
|
+
private entryExportedFunction(name: string): ts.FunctionDeclaration | null {
|
|
369
|
+
for (const stmt of this.entryFile.statements) {
|
|
370
|
+
if (ts.isFunctionDeclaration(stmt) && stmt.name?.text === name && hasExportModifier(stmt)) return stmt;
|
|
371
|
+
}
|
|
372
|
+
for (const b of exportListBindings(this.tast, this.entryFile)) {
|
|
373
|
+
if (b.exportedName === name && b.target && ts.isFunctionDeclaration(b.target)) return b.target;
|
|
374
|
+
}
|
|
375
|
+
for (const stmt of this.entryFile.statements) {
|
|
376
|
+
if (ts.isFunctionDeclaration(stmt) && stmt.name?.text === name && this.isListExported(stmt, this.entryFile)) return stmt;
|
|
377
|
+
}
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/// The entry module's exported const under `name` (modifier or export
|
|
382
|
+
/// list), unwrapped past as/satisfies/parens.
|
|
383
|
+
private entryExportedConst(name: string): ts.Expression | null {
|
|
384
|
+
for (const stmt of this.entryFile.statements) {
|
|
385
|
+
if (!ts.isVariableStatement(stmt)) continue;
|
|
386
|
+
for (const decl of stmt.declarationList.declarations) {
|
|
387
|
+
if (!ts.isIdentifier(decl.name) || decl.name.text !== name || !decl.initializer) continue;
|
|
388
|
+
if (!hasExportModifier(stmt) && !this.isListExported(decl, this.entryFile)) continue;
|
|
389
|
+
let init = decl.initializer;
|
|
390
|
+
while (ts.isParenthesizedExpression(init) || ts.isAsExpression(init) || ts.isSatisfiesExpression(init)) {
|
|
391
|
+
init = init.expression;
|
|
392
|
+
}
|
|
393
|
+
return init;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
for (const b of exportListBindings(this.tast, this.entryFile)) {
|
|
397
|
+
if (b.exportedName !== name || !b.target || !ts.isVariableDeclaration(b.target)) continue;
|
|
398
|
+
if (!b.target.initializer) continue;
|
|
399
|
+
let init = b.target.initializer;
|
|
400
|
+
while (ts.isParenthesizedExpression(init) || ts.isAsExpression(init) || ts.isSatisfiesExpression(init)) {
|
|
401
|
+
init = init.expression;
|
|
402
|
+
}
|
|
403
|
+
return init;
|
|
404
|
+
}
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/// The spec's effectful pair shape on update/initialModel — the
|
|
409
|
+
/// presence fact the sidecar restates as *_returns_cmd.
|
|
410
|
+
private returnsCmdPair(decl: ts.FunctionDeclaration | null): boolean {
|
|
411
|
+
const t = decl?.type;
|
|
412
|
+
if (!t) return false;
|
|
413
|
+
const members = ts.isUnionTypeNode(t) ? t.types : [t];
|
|
414
|
+
const tuples = members.filter((m) => ts.isTupleTypeNode(m));
|
|
415
|
+
if (tuples.length !== 1) return false;
|
|
416
|
+
const tuple = tuples[0] as ts.TupleTypeNode;
|
|
417
|
+
if (tuple.elements.length !== 2) return false;
|
|
418
|
+
const cmdRef = tuple.elements[1];
|
|
419
|
+
return (
|
|
420
|
+
ts.isTypeReferenceNode(cmdRef) &&
|
|
421
|
+
ts.isIdentifier(cmdRef.typeName) &&
|
|
422
|
+
this.checkResult.cmdNames.has(cmdRef.typeName.text)
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/// Whether the declared return ALSO admits the bare model (the mixed
|
|
427
|
+
/// idiom, `Model | [Model, Cmd<Msg>]`) — the additive fact the facade
|
|
428
|
+
/// emitter keys its narrowing wrapper on (*_returns_bare).
|
|
429
|
+
private returnsBareModel(decl: ts.FunctionDeclaration | null): boolean {
|
|
430
|
+
const t = decl?.type;
|
|
431
|
+
if (!t || !ts.isUnionTypeNode(t)) return false;
|
|
432
|
+
return t.types.some((m) => !ts.isTupleTypeNode(m));
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/// The `export const viewUnbound = [...]` opt-out list, split by side
|
|
436
|
+
/// (the emitter's viewUnboundNames twin; shapes are checker-taught, so
|
|
437
|
+
/// unresolvable spellings simply stay off both lists here).
|
|
438
|
+
private viewUnbound(helperNames: readonly string[]): { model: string[]; msg: string[] } {
|
|
439
|
+
const model: string[] = [];
|
|
440
|
+
const msg: string[] = [];
|
|
441
|
+
for (const stmt of this.entryFile.statements) {
|
|
442
|
+
if (!ts.isVariableStatement(stmt)) continue;
|
|
443
|
+
for (const decl of stmt.declarationList.declarations) {
|
|
444
|
+
if (!ts.isIdentifier(decl.name) || decl.name.text !== "viewUnbound" || !decl.initializer) continue;
|
|
445
|
+
let init = decl.initializer;
|
|
446
|
+
while (ts.isParenthesizedExpression(init) || ts.isAsExpression(init) || ts.isSatisfiesExpression(init)) {
|
|
447
|
+
init = init.expression;
|
|
448
|
+
}
|
|
449
|
+
if (!ts.isArrayLiteralExpression(init)) continue;
|
|
450
|
+
const modelFields = this.table.structs.get("Model")?.fields ?? [];
|
|
451
|
+
const msgArms = this.table.unions.get("Msg")?.arms ?? [];
|
|
452
|
+
for (const el of init.elements) {
|
|
453
|
+
if (!ts.isStringLiteral(el)) continue;
|
|
454
|
+
const entry = el.text;
|
|
455
|
+
if (modelFields.some((f) => f.tsName === entry) || helperNames.includes(entry)) model.push(entry);
|
|
456
|
+
else if (msgArms.some((a) => a.tag === entry)) msg.push(entry);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return { model, msg };
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/// `export const envMsgs = [{ env, msg }] as const` — the entries, in
|
|
464
|
+
/// declaration order (shape teaching is the checker/emitter's NS1033).
|
|
465
|
+
private envMsgEntries(): { env: string; msg: string }[] {
|
|
466
|
+
const init = this.entryExportedConst("envMsgs");
|
|
467
|
+
if (!init || !ts.isArrayLiteralExpression(init)) return [];
|
|
468
|
+
const entries: { env: string; msg: string }[] = [];
|
|
469
|
+
for (const el of init.elements) {
|
|
470
|
+
let e: ts.Expression = el;
|
|
471
|
+
while (ts.isParenthesizedExpression(e) || ts.isAsExpression(e) || ts.isSatisfiesExpression(e)) e = e.expression;
|
|
472
|
+
if (!ts.isObjectLiteralExpression(e)) continue;
|
|
473
|
+
let env: string | null = null;
|
|
474
|
+
let msg: string | null = null;
|
|
475
|
+
for (const p of e.properties) {
|
|
476
|
+
if (!ts.isPropertyAssignment(p) || !ts.isIdentifier(p.name) || !ts.isStringLiteral(p.initializer)) continue;
|
|
477
|
+
if (p.name.text === "env") env = p.initializer.text;
|
|
478
|
+
else if (p.name.text === "msg") msg = p.initializer.text;
|
|
479
|
+
}
|
|
480
|
+
if (env !== null && msg !== null) entries.push({ env, msg });
|
|
481
|
+
}
|
|
482
|
+
return entries;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/// An exported string-literal channel const (`appearanceMsg`,
|
|
486
|
+
/// `chromeMsg`), as its JSON value fragment; "null" when absent.
|
|
487
|
+
private channelConstJson(name: string): string {
|
|
488
|
+
const init = this.entryExportedConst(name);
|
|
489
|
+
if (init && ts.isStringLiteral(init)) return js(init.text);
|
|
490
|
+
return "null";
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// ---------------------------------------------------------------- emission
|
|
494
|
+
|
|
495
|
+
emit(): string {
|
|
496
|
+
const modelName = "Model";
|
|
497
|
+
const msgName = "Msg";
|
|
498
|
+
const model = this.table.structs.get(modelName);
|
|
499
|
+
const msg = this.table.unions.get(msgName);
|
|
500
|
+
if (!model || !msg) {
|
|
501
|
+
throw new ContractError("a core exports `Model` (a record) and `Msg` (a discriminated union)", this.entryFile);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
const helperDecls = this.table.modelHelperDecls();
|
|
505
|
+
|
|
506
|
+
// Phase 1: the reach set — Model, then every non-void Msg arm
|
|
507
|
+
// payload the number_bytes family does not absorb, then helper
|
|
508
|
+
// returns (first-visit order; the extractor's root order).
|
|
509
|
+
this.collect({ k: "struct", name: modelName }, null, "", "");
|
|
510
|
+
for (const arm of msg.arms) {
|
|
511
|
+
if (arm.fields.length === 0) continue;
|
|
512
|
+
if (this.isNumberBytesShape(arm)) continue;
|
|
513
|
+
this.collectArmPayloadForMsg(msgName, arm);
|
|
514
|
+
}
|
|
515
|
+
const helperReturns = helperDecls.map((h) => {
|
|
516
|
+
const ret = this.table.resolveTypeNode(h.decl.type!);
|
|
517
|
+
this.collect(ret, h.decl, "helpers", h.name);
|
|
518
|
+
return { name: h.name, decl: h.decl, ret };
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
// Phase 2: sections.
|
|
522
|
+
let structs = "";
|
|
523
|
+
let enums = "";
|
|
524
|
+
let unions = "";
|
|
525
|
+
let structCount = 0;
|
|
526
|
+
let enumCount = 0;
|
|
527
|
+
let unionCount = 0;
|
|
528
|
+
for (const entry of this.entries) {
|
|
529
|
+
if (entry.kind === "struct") {
|
|
530
|
+
let fields = "";
|
|
531
|
+
entry.fields.forEach((f, index) => {
|
|
532
|
+
if (index > 0) fields += ", ";
|
|
533
|
+
fields += `{"name": ${js(f.name)}, "type": ${this.typeRefJson(f.rt)}}`;
|
|
534
|
+
if (this.spellsI64(f.rt)) this.appendSlot(`${entry.name}.${f.name}`);
|
|
535
|
+
});
|
|
536
|
+
if (structCount > 0) structs += ",\n ";
|
|
537
|
+
structs += `{"name": ${js(entry.name)}${this.originFragment(entry.name)}, "fields": [${fields}]}`;
|
|
538
|
+
structCount += 1;
|
|
539
|
+
} else if (entry.kind === "enum") {
|
|
540
|
+
const members = entry.members.map((m) => js(m)).join(", ");
|
|
541
|
+
if (enumCount > 0) enums += ",\n ";
|
|
542
|
+
enums += `{"name": ${js(entry.name)}${this.originFragment(entry.name)}, "members": [${members}]}`;
|
|
543
|
+
enumCount += 1;
|
|
544
|
+
} else {
|
|
545
|
+
let arms = "";
|
|
546
|
+
entry.arms.forEach((arm, index) => {
|
|
547
|
+
if (index > 0) arms += ", ";
|
|
548
|
+
const memberFragment = arm.member !== null ? `, "member": ${js(arm.member)}` : "";
|
|
549
|
+
const payload = arm.payload === null ? '{"kind": "void"}' : this.typeRefJson(arm.payload);
|
|
550
|
+
arms += `{"name": ${js(arm.tag)}${memberFragment}, "payload": ${payload}}`;
|
|
551
|
+
if (arm.payload !== null && this.spellsI64(arm.payload)) this.appendSlot(`${entry.name}.${arm.tag}`);
|
|
552
|
+
});
|
|
553
|
+
if (unionCount > 0) unions += ",\n ";
|
|
554
|
+
unions += `{"name": ${js(entry.name)}${this.originFragment(entry.name)}, "arms": [${arms}]}`;
|
|
555
|
+
unionCount += 1;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// Message arms with payload descriptors.
|
|
560
|
+
let msgArms = "";
|
|
561
|
+
msg.arms.forEach((arm, index) => {
|
|
562
|
+
if (index > 0) msgArms += ",\n ";
|
|
563
|
+
const memberFragment = arm.fields.length === 1 ? `, "member": ${js(arm.fields[0].tsName)}` : "";
|
|
564
|
+
msgArms += `{"name": ${js(arm.tag)}${memberFragment}, "payload": ${this.payloadDescriptor(msgName, arm)}}`;
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
// Helpers, in export (= Model forwarding declaration) order.
|
|
568
|
+
let helpers = "";
|
|
569
|
+
let helperCount = 0;
|
|
570
|
+
for (const h of helperReturns) {
|
|
571
|
+
if (helperCount > 0) helpers += ",\n ";
|
|
572
|
+
const reflected = this.reflect(h.ret, h.decl, true);
|
|
573
|
+
if (this.spellsI64(reflected)) this.appendSlot(`helpers.${h.name}.return`);
|
|
574
|
+
helpers += `{"name": ${js(h.name)}, "params": [], "returns": ${this.typeRefJson(reflected)}, "arena": false}`;
|
|
575
|
+
helperCount += 1;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const unbound = this.viewUnbound(helperDecls.map((h) => h.name));
|
|
579
|
+
const modelUnbound = unbound.model.map((n) => js(n)).join(", ");
|
|
580
|
+
const msgUnbound = unbound.msg.map((n) => js(n)).join(", ");
|
|
581
|
+
|
|
582
|
+
// Channels: export presence IS the wiring decision.
|
|
583
|
+
const hasCommand = this.entryExportedFunction("commandMsg") !== null;
|
|
584
|
+
const hasFrame = this.entryExportedFunction("frameMsg") !== null;
|
|
585
|
+
const hasKey = this.entryExportedFunction("keyMsg") !== null;
|
|
586
|
+
const hasPinch = this.entryExportedFunction("pinchMsg") !== null;
|
|
587
|
+
const envMsgs = this.envMsgEntries()
|
|
588
|
+
.map((e) => `{"env": ${js(e.env)}, "msg": ${js(e.msg)}}`)
|
|
589
|
+
.join(", ");
|
|
590
|
+
const appearance = this.channelConstJson("appearanceMsg");
|
|
591
|
+
const chrome = this.channelConstJson("chromeMsg");
|
|
592
|
+
|
|
593
|
+
// Entry-shape flags, from the declared return annotations — the
|
|
594
|
+
// same pair-shape discrimination the emitter applies.
|
|
595
|
+
const initReturnsCmd = this.returnsCmdPair(this.entryExportedFunction("initialModel"));
|
|
596
|
+
const updateReturnsCmd = this.returnsCmdPair(this.entryExportedFunction("update"));
|
|
597
|
+
// The mixed-idiom facts ride only when true (additive fields; a
|
|
598
|
+
// compiler's co-emitted sidecar never carries them).
|
|
599
|
+
const initReturnsBare = initReturnsCmd && this.returnsBareModel(this.entryExportedFunction("initialModel"));
|
|
600
|
+
const updateReturnsBare = updateReturnsCmd && this.returnsBareModel(this.entryExportedFunction("update"));
|
|
601
|
+
const hasSubscriptions = this.entryExportedFunction("subscriptions") !== null;
|
|
602
|
+
|
|
603
|
+
let abiExports =
|
|
604
|
+
'"abi_version", "build_id", "set_panic_sink", "init", "collect", ' +
|
|
605
|
+
'"frame_reset", "boot_cmd", "dispatch_void", "dispatch_bytes", ' +
|
|
606
|
+
'"dispatch_number", "dispatch_number_bytes", "dispatch_bool", "dispatch_enum", ' +
|
|
607
|
+
'"dispatch_record", "dispatch_text_input", "dispatch_scroll_state", ' +
|
|
608
|
+
'"subscriptions", "model_snapshot", "helper_call"';
|
|
609
|
+
if (hasCommand) abiExports += ', "command_msg"';
|
|
610
|
+
if (hasFrame) abiExports += ', "frame_msg"';
|
|
611
|
+
if (hasKey) abiExports += ', "key_msg"';
|
|
612
|
+
if (hasPinch) abiExports += ', "pinch_msg"';
|
|
613
|
+
|
|
614
|
+
const typesJson =
|
|
615
|
+
`{\n "structs": [\n ${structs}\n ],\n` +
|
|
616
|
+
` "enums": [\n ${enums}\n ],\n` +
|
|
617
|
+
` "unions": [\n ${unions}\n ]\n }`;
|
|
618
|
+
|
|
619
|
+
const slots = this.slots.join(",\n ");
|
|
620
|
+
const slotCount = this.slots.length;
|
|
621
|
+
|
|
622
|
+
// Deterministic synthesized identity, the extractor's construction
|
|
623
|
+
// verbatim: every section behind a label and a NUL separator, so
|
|
624
|
+
// the serialization is injective and re-runs reproduce it exactly.
|
|
625
|
+
const surface =
|
|
626
|
+
`entry=${this.entry}` +
|
|
627
|
+
`\x00model=${modelName}` +
|
|
628
|
+
`\x00msg=${msgName}` +
|
|
629
|
+
`\x00types=${typesJson}` +
|
|
630
|
+
`\x00arms=${msgArms}` +
|
|
631
|
+
`\x00helpers=${helpers}` +
|
|
632
|
+
`\x00model_unbound=${modelUnbound}` +
|
|
633
|
+
`\x00msg_unbound=${msgUnbound}` +
|
|
634
|
+
`\x00env=${envMsgs}` +
|
|
635
|
+
`\x00appearance=${appearance}` +
|
|
636
|
+
`\x00chrome=${chrome}` +
|
|
637
|
+
`\x00exports=${abiExports}` +
|
|
638
|
+
`\x00flags=${boolJson(initReturnsCmd)}${boolJson(updateReturnsCmd)}` +
|
|
639
|
+
`${boolJson(hasSubscriptions)}${boolJson(hasCommand)}${boolJson(hasFrame)}` +
|
|
640
|
+
`${boolJson(hasKey)}${boolJson(hasPinch)}`;
|
|
641
|
+
const surfaceBytes = new TextEncoder().encode(surface);
|
|
642
|
+
const sourceHash = wyhashHex(0x5eedc0den, surfaceBytes);
|
|
643
|
+
const buildId = wyhashHex(0xb11d1d00n, surfaceBytes);
|
|
644
|
+
|
|
645
|
+
return (
|
|
646
|
+
"{\n" +
|
|
647
|
+
' "format": 1,\n' +
|
|
648
|
+
' "wire_version": 3,\n' +
|
|
649
|
+
' "abi_version": 1,\n' +
|
|
650
|
+
' "compiler_version": "0.0.1",\n' +
|
|
651
|
+
` "entry": ${js(this.entry)},\n` +
|
|
652
|
+
` "source_hash": "${sourceHash}",\n` +
|
|
653
|
+
` "build_id": "${buildId}",\n` +
|
|
654
|
+
` "types": ${typesJson},\n` +
|
|
655
|
+
` "model": ${js(modelName)},\n` +
|
|
656
|
+
` "model_helpers": [${helperCount > 0 ? `\n ${helpers}\n ` : ""}],\n` +
|
|
657
|
+
` "model_unbound": [${modelUnbound}],\n` +
|
|
658
|
+
` "msg": {\n "name": ${js(msgName)},\n "arms": [\n ${msgArms}\n ],\n "unbound": [${msgUnbound}]\n },\n` +
|
|
659
|
+
` "init_returns_cmd": ${boolJson(initReturnsCmd)},\n` +
|
|
660
|
+
` "update_returns_cmd": ${boolJson(updateReturnsCmd)},\n` +
|
|
661
|
+
(initReturnsBare ? ' "init_returns_bare": true,\n' : "") +
|
|
662
|
+
(updateReturnsBare ? ' "update_returns_bare": true,\n' : "") +
|
|
663
|
+
` "has_subscriptions": ${boolJson(hasSubscriptions)},\n` +
|
|
664
|
+
' "channels": {\n' +
|
|
665
|
+
` "command_msg": ${boolJson(hasCommand)},\n` +
|
|
666
|
+
` "frame_msg": ${boolJson(hasFrame)},\n` +
|
|
667
|
+
` "key_msg": ${boolJson(hasKey)},\n` +
|
|
668
|
+
` "pinch_msg": ${boolJson(hasPinch)},\n` +
|
|
669
|
+
` "appearance_msg": ${appearance},\n` +
|
|
670
|
+
` "chrome_msg": ${chrome},\n` +
|
|
671
|
+
` "env_msgs": [${envMsgs}]\n` +
|
|
672
|
+
" },\n" +
|
|
673
|
+
` "abi": {\n "prefix": "nsc_core_",\n "exports": [${abiExports}],\n "snapshot_format": 1\n },\n` +
|
|
674
|
+
` "integer_slots": [${slotCount > 0 ? `\n ${slots}\n ` : ""}],\n` +
|
|
675
|
+
' "deterministic": true,\n' +
|
|
676
|
+
' "async_free": true\n' +
|
|
677
|
+
"}\n"
|
|
678
|
+
);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/// The anonymous two-field number-plus-bytes shape, in declaration
|
|
682
|
+
/// order (number first) — the family that never needs a table entry.
|
|
683
|
+
private isNumberBytesShape(arm: UnionArm): boolean {
|
|
684
|
+
if (arm.fields.length !== 2) return false;
|
|
685
|
+
const first = this.reflect(arm.fields[0].type, arm.fields[0].decl, true);
|
|
686
|
+
const second = this.reflect(arm.fields[1].type, arm.fields[1].decl, true);
|
|
687
|
+
return (first.k === "f64" || first.k === "i64") && second.k === "bytes";
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/// The reach walk through one MESSAGE arm's payload (the tabled-union
|
|
691
|
+
/// twin, with the Msg-specific number_bytes exemption already applied
|
|
692
|
+
/// by the caller).
|
|
693
|
+
private collectArmPayloadForMsg(msgName: string, arm: UnionArm): void {
|
|
694
|
+
if (arm.fields.length === 1) {
|
|
695
|
+
const f = arm.fields[0];
|
|
696
|
+
this.collect(f.type, f.decl, msgName, arm.tag);
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
this.collectSynthesized(`${msgName}_${arm.tag}`, arm.fields);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/// The payload descriptor of one Msg arm, collecting integer slots
|
|
703
|
+
/// for the number-carrying families (the extractor's construction).
|
|
704
|
+
private payloadDescriptor(msgName: string, arm: UnionArm): string {
|
|
705
|
+
if (arm.fields.length === 0) return '{"kind": "void"}';
|
|
706
|
+
if (arm.fields.length === 1) {
|
|
707
|
+
const f = arm.fields[0];
|
|
708
|
+
const rt = this.reflect(f.type, f.decl, true);
|
|
709
|
+
switch (rt.k) {
|
|
710
|
+
case "bytes":
|
|
711
|
+
return '{"kind": "bytes"}';
|
|
712
|
+
case "f64":
|
|
713
|
+
return '{"kind": "number", "class": "f64"}';
|
|
714
|
+
case "i64":
|
|
715
|
+
this.appendSlot(`${msgName}.${arm.tag}`);
|
|
716
|
+
return '{"kind": "number", "class": "i64"}';
|
|
717
|
+
case "bool":
|
|
718
|
+
return '{"kind": "scalar", "type": {"kind": "bool"}}';
|
|
719
|
+
case "enum":
|
|
720
|
+
return `{"kind": "enum", "name": ${js(rt.name)}}`;
|
|
721
|
+
case "union":
|
|
722
|
+
return `{"kind": "union", "name": ${js(rt.name)}}`;
|
|
723
|
+
case "value":
|
|
724
|
+
return `{"kind": "record", "name": ${js(rt.name)}}`;
|
|
725
|
+
case "node":
|
|
726
|
+
throw new ContractError(
|
|
727
|
+
`Msg arm \`${arm.tag}\` carries the by-reference record \`${rt.name}\`, which has no schema form in sidecar format 1 — store the record by value (declare it as an object-literal type alias) or carry its fields inline`,
|
|
728
|
+
f.decl,
|
|
729
|
+
);
|
|
730
|
+
case "optional":
|
|
731
|
+
case "slice":
|
|
732
|
+
throw new ContractError(
|
|
733
|
+
`Msg arm \`${arm.tag}\` carries a payload with no sidecar descriptor form (optional and array payloads have none in format 1) — wrap the payload in a named record`,
|
|
734
|
+
f.decl,
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
if (this.isNumberBytesShape(arm)) {
|
|
739
|
+
const numberField = arm.fields[0];
|
|
740
|
+
const cls = this.reflect(numberField.type, numberField.decl, true).k === "i64" ? "i64" : "f64";
|
|
741
|
+
if (cls === "i64") this.appendSlot(`${msgName}.${arm.tag}.${numberField.tsName}`);
|
|
742
|
+
return (
|
|
743
|
+
`{"kind": "number_bytes", "number_field": ${js(numberField.tsName)}` +
|
|
744
|
+
`, "number_class": "${cls}", "bytes_field": ${js(arm.fields[1].tsName)}}`
|
|
745
|
+
);
|
|
746
|
+
}
|
|
747
|
+
return `{"kind": "record", "name": ${js(`${msgName}_${arm.tag}`)}}`;
|
|
748
|
+
}
|
|
749
|
+
}
|