@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,3984 @@
|
|
|
1
|
+
//! core_facade.ts emission: the compiled-core ENTRY MODULE generated
|
|
2
|
+
//! from the same contract sidecar the Zig mirror derives from. One
|
|
3
|
+
//! sidecar, three projections — the host imports core_shim.zig, an
|
|
4
|
+
//! external core compiler consumes this module as its compile entry
|
|
5
|
+
//! (with the generated core_profile.json beside it), and the two sides
|
|
6
|
+
//! can never skew: types, declaration orders, number classes, wire
|
|
7
|
+
//! tags, and the canonical value encoding all derive from one artifact.
|
|
8
|
+
//!
|
|
9
|
+
//! The emitted module is the generated twin of a hand-written adapter:
|
|
10
|
+
//! it imports the AUTHOR'S core module (staged beside it, with its SDK
|
|
11
|
+
//! imports resolved to the staged ./sdk/ copies), owns the committed
|
|
12
|
+
//! model in module state, and exports the profile-declared dispatch
|
|
13
|
+
//! surface:
|
|
14
|
+
//!
|
|
15
|
+
//! - type re-exports for every named contract-table type, each from its
|
|
16
|
+
//! own declaring module (the sidecar's origin facts) — the compiler's
|
|
17
|
+
//! sidecar emitter reads the entry module's exported declarations;
|
|
18
|
+
//! - the exported-const channel conventions (appearanceMsg, chromeMsg,
|
|
19
|
+
//! envMsgs) and the single name-resolved viewUnbound list;
|
|
20
|
+
//! - one exported wrapper per model helper, in declaration order (the
|
|
21
|
+
//! compiled contract's model_helpers table and helper_call's index
|
|
22
|
+
//! space both derive from it), with the inline wholeness proof on
|
|
23
|
+
//! every i64-classed helper return;
|
|
24
|
+
//! - the designated shape-flag entries (init, coreUpdate, and — for a
|
|
25
|
+
//! subscribing contract — coreSubscriptions), whose return shapes
|
|
26
|
+
//! restate init_returns_cmd/update_returns_cmd/has_subscriptions;
|
|
27
|
+
//! - the ABI dispatch surface (boot_cmd, the nine dispatch entries, the
|
|
28
|
+
//! wired channel entries, abi_subscriptions, model_snapshot, helper_call),
|
|
29
|
+
//! decoding inbound payloads with the generated wire codec below and
|
|
30
|
+
//! encoding results byte-identically to the transpiler lane;
|
|
31
|
+
//! - provable ingress everywhere a wire value crosses into an
|
|
32
|
+
//! i64-classed slot: bind to a local, range-guard with ordered
|
|
33
|
+
//! comparisons against +-(2^53 - 1) (which excludes NaN), and state
|
|
34
|
+
//! wholeness with Math.trunc at the write.
|
|
35
|
+
//!
|
|
36
|
+
//! Deterministic: a pure function of the sidecar value, pinned by a
|
|
37
|
+
//! test. Everything module-internal lives in the reserved nscf name
|
|
38
|
+
//! space, which the fences below keep authored names out of.
|
|
39
|
+
|
|
40
|
+
const std = @import("std");
|
|
41
|
+
const sidecar_mod = @import("sidecar.zig");
|
|
42
|
+
const emit_mod = @import("emit.zig");
|
|
43
|
+
|
|
44
|
+
const Sidecar = sidecar_mod.Sidecar;
|
|
45
|
+
const TypeRef = sidecar_mod.TypeRef;
|
|
46
|
+
const IntegerClass = sidecar_mod.IntegerClass;
|
|
47
|
+
|
|
48
|
+
pub const Error = error{ Refused, OutOfMemory };
|
|
49
|
+
|
|
50
|
+
/// The exact-integer window an f64 carries, as the generated guards spell
|
|
51
|
+
/// it. Signed slots use `min_safe`; unsigned slots start at zero.
|
|
52
|
+
const max_safe = "9007199254740991";
|
|
53
|
+
const min_safe = "-9007199254740991";
|
|
54
|
+
|
|
55
|
+
fn integerLowerBound(class: IntegerClass) []const u8 {
|
|
56
|
+
return if (class == .u64) "0" else min_safe;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/// The scroll-state field vocabulary (the mirror's routing twin in
|
|
60
|
+
/// emit.zig): a record arm carrying exactly these eight numeric fields
|
|
61
|
+
/// in either the TypeScript or canvas spelling also answers the dedicated
|
|
62
|
+
/// scroll entry. The ABI parameters use the TypeScript spellings.
|
|
63
|
+
const scroll_state_fields_ts = [_][]const u8{
|
|
64
|
+
"offsetX", "offsetY",
|
|
65
|
+
"velocityX", "velocityY",
|
|
66
|
+
"viewportExtentX", "viewportExtentY",
|
|
67
|
+
"contentExtentX", "contentExtentY",
|
|
68
|
+
};
|
|
69
|
+
const scroll_state_fields_canvas = [_][]const u8{
|
|
70
|
+
"offset_x", "offset_y",
|
|
71
|
+
"velocity_x", "velocity_y",
|
|
72
|
+
"viewport_extent_x", "viewport_extent_y",
|
|
73
|
+
"content_extent_x", "content_extent_y",
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
pub fn emitFacade(arena: std.mem.Allocator, sidecar: Sidecar, diags: *sidecar_mod.Diagnostics) Error![]const u8 {
|
|
77
|
+
var emitter = FacadeEmitter{
|
|
78
|
+
.arena = arena,
|
|
79
|
+
.sidecar = sidecar,
|
|
80
|
+
.diags = diags,
|
|
81
|
+
.out = .empty,
|
|
82
|
+
};
|
|
83
|
+
try emitter.run();
|
|
84
|
+
if (diags.hasErrors()) return error.Refused;
|
|
85
|
+
return emitter.out.items;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/// TypeScript reserved words a declaration may not take (no quoting
|
|
89
|
+
/// escape exists on that side, unlike Zig's @"..." names).
|
|
90
|
+
const ts_reserved_words = [_][]const u8{
|
|
91
|
+
"break", "case", "catch", "class", "const", "continue", "debugger", "default",
|
|
92
|
+
"delete", "do", "else", "enum", "export", "extends", "false", "finally",
|
|
93
|
+
"for", "function", "if", "import", "in", "instanceof", "new", "null",
|
|
94
|
+
"return", "super", "switch", "this", "throw", "true", "try", "typeof",
|
|
95
|
+
"var", "void", "while", "with", "let", "static", "yield", "await",
|
|
96
|
+
"interface", "type", "number", "boolean", "string", "object", "undefined",
|
|
97
|
+
// Intrinsic type keywords: a declaration under one would make every
|
|
98
|
+
// reference bind the built-in, erasing the contract silently.
|
|
99
|
+
"any",
|
|
100
|
+
"unknown", "never", "bigint", "symbol",
|
|
101
|
+
// Strict-mode reservations (modules are always strict).
|
|
102
|
+
"implements", "package", "private", "protected",
|
|
103
|
+
"public", "arguments", "eval",
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/// The value-space names this module itself exports; an authored helper
|
|
107
|
+
/// or type may not take one.
|
|
108
|
+
const fixed_exports = [_][]const u8{
|
|
109
|
+
"init", "coreUpdate", "coreSubscriptions", "boot_cmd",
|
|
110
|
+
"dispatch_void", "dispatch_bytes", "dispatch_number", "dispatch_number_bytes",
|
|
111
|
+
"dispatch_bool", "dispatch_enum", "dispatch_record", "dispatch_text_input",
|
|
112
|
+
"dispatch_scroll_state", "abi_subscriptions", "model_snapshot", "helper_call",
|
|
113
|
+
"abi_command_msg", "abi_frame_msg", "abi_key_msg", "abi_pinch_msg",
|
|
114
|
+
"appearanceMsg", "chromeMsg", "envMsgs", "viewUnbound",
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/// Ambient VALUE bindings generated code calls directly. A model helper is
|
|
118
|
+
/// re-declared as an exported function in this module, so taking one of these
|
|
119
|
+
/// names would shadow the ambient and turn `Math.trunc`, `Buffer.from`, etc.
|
|
120
|
+
/// into member accesses on the wrapper itself.
|
|
121
|
+
const ambient_value_names = [_][]const u8{
|
|
122
|
+
"Math", "Buffer", "Uint8Array", "Error", "String",
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/// The wire-codec snippets, emitted exactly when generated code reaches
|
|
126
|
+
/// them (an unused module function is noise a strict compiler may
|
|
127
|
+
/// refuse). `use` marks dependencies transitively.
|
|
128
|
+
const Codec = enum {
|
|
129
|
+
trap,
|
|
130
|
+
read_u8,
|
|
131
|
+
read_u32,
|
|
132
|
+
read_f64,
|
|
133
|
+
read_i64,
|
|
134
|
+
read_i64_saturating,
|
|
135
|
+
read_u64_saturating,
|
|
136
|
+
read_bool,
|
|
137
|
+
read_bytes_body,
|
|
138
|
+
assert_consumed,
|
|
139
|
+
sink,
|
|
140
|
+
w_u8,
|
|
141
|
+
w_u32,
|
|
142
|
+
w_f64,
|
|
143
|
+
w_i64,
|
|
144
|
+
w_u64,
|
|
145
|
+
w_bool,
|
|
146
|
+
w_bytes,
|
|
147
|
+
short_text,
|
|
148
|
+
trunc_toward_zero,
|
|
149
|
+
enum_index,
|
|
150
|
+
ascii_string,
|
|
151
|
+
cmd_encoder,
|
|
152
|
+
sub_encoder,
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const FacadeEmitter = struct {
|
|
156
|
+
arena: std.mem.Allocator,
|
|
157
|
+
sidecar: Sidecar,
|
|
158
|
+
diags: *sidecar_mod.Diagnostics,
|
|
159
|
+
out: std.ArrayListUnmanaged(u8),
|
|
160
|
+
inlined: []const []const u8 = &.{},
|
|
161
|
+
flattened: []const []const u8 = &.{},
|
|
162
|
+
node_stored: []const []const u8 = &.{},
|
|
163
|
+
used_codec: std.EnumSet(Codec) = .initEmpty(),
|
|
164
|
+
/// Named types the generated code references in type position (the
|
|
165
|
+
/// import list derives from it).
|
|
166
|
+
referenced: std.ArrayListUnmanaged([]const u8) = .empty,
|
|
167
|
+
/// Whether the pinch channel's SDK vocabulary import is needed.
|
|
168
|
+
needs_pinch_phase: bool = false,
|
|
169
|
+
/// Whether the shared member-index teaching is needed.
|
|
170
|
+
needs_member_trap: bool = false,
|
|
171
|
+
/// Monotonic temp-name counter (unique const names inside one
|
|
172
|
+
/// emitted function; reset at each function boundary).
|
|
173
|
+
temp_counter: usize = 0,
|
|
174
|
+
/// Deduped on-demand generation queues, drained by generatedTables.
|
|
175
|
+
needed_enum_tables: std.ArrayListUnmanaged([]const u8) = .empty,
|
|
176
|
+
needed_enum_indexes: std.ArrayListUnmanaged([]const u8) = .empty,
|
|
177
|
+
needed_record_writers: std.ArrayListUnmanaged([]const u8) = .empty,
|
|
178
|
+
needed_union_writers: std.ArrayListUnmanaged([]const u8) = .empty,
|
|
179
|
+
needed_union_decoders: std.ArrayListUnmanaged([]const u8) = .empty,
|
|
180
|
+
|
|
181
|
+
fn print(self: *FacadeEmitter, comptime fmt: []const u8, args: anytype) Error!void {
|
|
182
|
+
const text = try std.fmt.allocPrint(self.arena, fmt, args);
|
|
183
|
+
try self.out.appendSlice(self.arena, text);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
fn raw(self: *FacadeEmitter, text: []const u8) Error!void {
|
|
187
|
+
try self.out.appendSlice(self.arena, text);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
fn use(self: *FacadeEmitter, id: Codec) void {
|
|
191
|
+
if (self.used_codec.contains(id)) return;
|
|
192
|
+
self.used_codec.insert(id);
|
|
193
|
+
// Dependency closure: every snippet marks what its own body
|
|
194
|
+
// calls, so the emitted set is always self-contained.
|
|
195
|
+
const deps: []const Codec = switch (id) {
|
|
196
|
+
.read_i64, .read_i64_saturating, .read_u64_saturating => &.{.read_u32},
|
|
197
|
+
.read_u8, .read_u32, .read_f64, .read_bool, .read_bytes_body, .assert_consumed => &.{.trap},
|
|
198
|
+
.w_u32 => &.{.sink},
|
|
199
|
+
.w_u8, .w_f64, .w_bool => &.{.sink},
|
|
200
|
+
.w_i64 => &.{ .w_u32, .trunc_toward_zero, .trap },
|
|
201
|
+
.w_u64 => &.{ .w_u32, .trunc_toward_zero, .trap },
|
|
202
|
+
.w_bytes => &.{.w_u32},
|
|
203
|
+
.short_text => &.{ .sink, .trap },
|
|
204
|
+
.enum_index => &.{.trap},
|
|
205
|
+
.cmd_encoder => &.{ .w_u8, .w_f64, .w_bytes, .short_text, .enum_index, .trap },
|
|
206
|
+
.sub_encoder => &.{ .w_u8, .w_f64, .short_text },
|
|
207
|
+
else => &.{},
|
|
208
|
+
};
|
|
209
|
+
for (deps) |dep| self.use(dep);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/// Note a named type the generated code spells in type position.
|
|
213
|
+
fn reference(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
214
|
+
// Current sidecars leave generated-only table names originless.
|
|
215
|
+
// They either flatten at their sole reference site or validation
|
|
216
|
+
// refuses them, so importing one would always name a nonexistent
|
|
217
|
+
// authored export (generic monomorphizations are the common case).
|
|
218
|
+
if (self.isGeneratedOnlyType(name)) return;
|
|
219
|
+
if (!self.isExportedType(name)) return;
|
|
220
|
+
if (nameListed(self.referenced.items, name)) return;
|
|
221
|
+
try self.referenced.append(self.arena, name);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ----------------------------------------------------- fact lookup
|
|
225
|
+
|
|
226
|
+
/// The authored member name of a single-payload arm. Validation requires
|
|
227
|
+
/// the fact everywhere emission consumes it; `value` remains only as an
|
|
228
|
+
/// unreachable-safe spelling for bare/multi-field branches that compute
|
|
229
|
+
/// this local before switching on their payload shape.
|
|
230
|
+
fn memberOf(_: *FacadeEmitter, member: ?[]const u8) []const u8 {
|
|
231
|
+
return member orelse "value";
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/// The declaring module of a table type. Facade validation requires an
|
|
235
|
+
/// origin for every authored table; the entry fallback is only for Msg,
|
|
236
|
+
/// whose root designation is not duplicated in the type tables.
|
|
237
|
+
fn originOf(self: *FacadeEmitter, name: []const u8) []const u8 {
|
|
238
|
+
for (self.sidecar.types.structs) |entry| {
|
|
239
|
+
if (std.mem.eql(u8, entry.name, name)) return entry.origin orelse self.entryBasename();
|
|
240
|
+
}
|
|
241
|
+
for (self.sidecar.types.enums) |entry| {
|
|
242
|
+
if (std.mem.eql(u8, entry.name, name)) return entry.origin orelse self.entryBasename();
|
|
243
|
+
}
|
|
244
|
+
for (self.sidecar.types.unions) |entry| {
|
|
245
|
+
if (std.mem.eql(u8, entry.name, name)) return entry.origin orelse self.entryBasename();
|
|
246
|
+
}
|
|
247
|
+
return self.entryBasename();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
fn entryBasename(self: *FacadeEmitter) []const u8 {
|
|
251
|
+
return std.fs.path.basenamePosix(self.sidecar.entry);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/// A table name synthesized by the transpiler rather than declared in
|
|
255
|
+
/// an authored module. These names flatten at their one legal reference
|
|
256
|
+
/// site or refuse; they are never imports or re-exports.
|
|
257
|
+
fn isGeneratedOnlyType(self: *FacadeEmitter, name: []const u8) bool {
|
|
258
|
+
if (std.mem.eql(u8, name, self.sidecar.model) or std.mem.eql(u8, name, self.sidecar.msg.name)) return false;
|
|
259
|
+
for (self.sidecar.types.structs) |entry| {
|
|
260
|
+
if (std.mem.eql(u8, entry.name, name)) return entry.origin == null;
|
|
261
|
+
}
|
|
262
|
+
for (self.sidecar.types.enums) |entry| {
|
|
263
|
+
if (std.mem.eql(u8, entry.name, name)) return entry.origin == null;
|
|
264
|
+
}
|
|
265
|
+
for (self.sidecar.types.unions) |entry| {
|
|
266
|
+
if (std.mem.eql(u8, entry.name, name)) return entry.origin == null;
|
|
267
|
+
}
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
fn isExportedType(self: *FacadeEmitter, name: []const u8) bool {
|
|
272
|
+
if (std.mem.eql(u8, name, self.sidecar.model) or std.mem.eql(u8, name, self.sidecar.msg.name)) return true;
|
|
273
|
+
for (self.sidecar.types.structs) |entry| {
|
|
274
|
+
if (std.mem.eql(u8, entry.name, name)) return entry.exported;
|
|
275
|
+
}
|
|
276
|
+
for (self.sidecar.types.enums) |entry| {
|
|
277
|
+
if (std.mem.eql(u8, entry.name, name)) return entry.exported;
|
|
278
|
+
}
|
|
279
|
+
for (self.sidecar.types.unions) |entry| {
|
|
280
|
+
if (std.mem.eql(u8, entry.name, name)) return entry.exported;
|
|
281
|
+
}
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
fn slotClassAt(self: *FacadeEmitter, path: []const u8) ?IntegerClass {
|
|
286
|
+
for (self.sidecar.integer_slots) |entry| {
|
|
287
|
+
if (std.mem.eql(u8, entry.slot, path)) return entry.class;
|
|
288
|
+
}
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
fn slotClass(self: *FacadeEmitter, container: []const u8, member: []const u8) ?IntegerClass {
|
|
293
|
+
const path = std.fmt.allocPrint(self.arena, "{s}.{s}", .{ container, member }) catch return null;
|
|
294
|
+
return self.slotClassAt(path);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
fn nestedSlotClass(self: *FacadeEmitter, container: []const u8, member: []const u8, field: []const u8) ?IntegerClass {
|
|
298
|
+
const path = std.fmt.allocPrint(self.arena, "{s}.{s}.{s}", .{ container, member, field }) catch return null;
|
|
299
|
+
return self.slotClassAt(path);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/// The integer writer for a slot: unsigned twin for u64-attested
|
|
303
|
+
/// slots, two's complement otherwise.
|
|
304
|
+
fn intWriter(self: *FacadeEmitter, container: []const u8, member: []const u8) []const u8 {
|
|
305
|
+
if (self.slotClass(container, member)) |class| {
|
|
306
|
+
if (class == .u64) {
|
|
307
|
+
self.use(.w_u64);
|
|
308
|
+
return "nscfWU64";
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
self.use(.w_i64);
|
|
312
|
+
return "nscfWI64";
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ------------------------------------------------------------- run
|
|
316
|
+
|
|
317
|
+
fn run(self: *FacadeEmitter) Error!void {
|
|
318
|
+
self.inlined = try emit_mod.inlinedTableNames(self.arena, self.sidecar);
|
|
319
|
+
self.flattened = try self.flattenedTableNames();
|
|
320
|
+
self.node_stored = try self.nodeStoredTableNames();
|
|
321
|
+
self.validateFacadeFacts();
|
|
322
|
+
try self.validateNames();
|
|
323
|
+
self.validateOptionalDepth();
|
|
324
|
+
if (self.diags.hasErrors()) return;
|
|
325
|
+
|
|
326
|
+
// The body emits first into its own buffer so the import lists
|
|
327
|
+
// (referenced types, used codec snippets) can assemble after the
|
|
328
|
+
// walk; the final module is header + imports + body.
|
|
329
|
+
var body_emitter = FacadeEmitter{
|
|
330
|
+
.arena = self.arena,
|
|
331
|
+
.sidecar = self.sidecar,
|
|
332
|
+
.diags = self.diags,
|
|
333
|
+
.out = .empty,
|
|
334
|
+
.inlined = self.inlined,
|
|
335
|
+
.flattened = self.flattened,
|
|
336
|
+
.node_stored = self.node_stored,
|
|
337
|
+
};
|
|
338
|
+
const body = &body_emitter;
|
|
339
|
+
try body.privateTypeDeclarations();
|
|
340
|
+
try body.channelConsts();
|
|
341
|
+
try body.unboundDecl();
|
|
342
|
+
try body.helperWrappers();
|
|
343
|
+
try body.entryPoints();
|
|
344
|
+
try body.tagTable();
|
|
345
|
+
try body.dispatchSurface();
|
|
346
|
+
try body.channelEntries();
|
|
347
|
+
try body.postCycle();
|
|
348
|
+
try body.helperCall();
|
|
349
|
+
try body.generatedTables();
|
|
350
|
+
try body.codecSection();
|
|
351
|
+
if (self.diags.hasErrors()) return;
|
|
352
|
+
|
|
353
|
+
try self.header();
|
|
354
|
+
try self.imports(body);
|
|
355
|
+
try self.reexports();
|
|
356
|
+
try self.out.appendSlice(self.arena, body.out.items);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// ------------------------------------------------------- fencing
|
|
360
|
+
|
|
361
|
+
/// Facade/profile emission needs facts that format-1 readers keep
|
|
362
|
+
/// optional for backward-compatible MIRROR generation. An older sidecar
|
|
363
|
+
/// still generates core_shim.zig, but it cannot truthfully reconstruct
|
|
364
|
+
/// authored module paths or payload property names; refuse instead of
|
|
365
|
+
/// guessing an entry-module origin or a `value` member.
|
|
366
|
+
fn validateFacadeFacts(self: *FacadeEmitter) void {
|
|
367
|
+
const model = sidecar_mod.findStruct(self.sidecar.types, self.sidecar.model);
|
|
368
|
+
if (model == null or model.?.origin == null) {
|
|
369
|
+
self.diags.flag("types", "the model declaration carries no authored type-origin fact — this sidecar predates facade metadata; regenerate it with the current compiler before requesting --facade, --profile, or --check", .{});
|
|
370
|
+
}
|
|
371
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
372
|
+
if (self.msgArmNeedsMember(arm) and arm.member == null) {
|
|
373
|
+
self.diags.flag("msg.arms", "arm \"{s}\" carries one payload but no authored member-name fact — this sidecar predates facade metadata; regenerate it with the current compiler", .{arm.name});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
for (self.sidecar.types.unions) |entry| {
|
|
377
|
+
for (entry.arms) |arm| {
|
|
378
|
+
if (arm.payload == .void or self.synthesizedRecordOf(arm.payload, entry.name, arm.name) != null) continue;
|
|
379
|
+
if (arm.member == null) {
|
|
380
|
+
self.diags.flag("types.unions", "arm \"{s}\" of \"{s}\" carries one payload but no authored member-name fact — this sidecar predates facade metadata; regenerate it with the current compiler", .{ arm.name, entry.name });
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
fn msgArmNeedsMember(self: *FacadeEmitter, arm: sidecar_mod.MsgArm) bool {
|
|
387
|
+
return switch (arm.payload) {
|
|
388
|
+
.void, .number_bytes => false,
|
|
389
|
+
.record => self.synthesizedRecordOf(recordPayloadRef(arm.payload), self.sidecar.msg.name, arm.name) == null,
|
|
390
|
+
else => true,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
fn validateNames(self: *FacadeEmitter) Error!void {
|
|
395
|
+
// Declaration names (type-table entries and the message union)
|
|
396
|
+
// must be plain TypeScript identifiers: TypeScript has no
|
|
397
|
+
// quoted-declaration escape, and re-export lists take
|
|
398
|
+
// identifiers verbatim.
|
|
399
|
+
var names: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
400
|
+
for (self.sidecar.types.structs) |entry| try names.append(self.arena, entry.name);
|
|
401
|
+
for (self.sidecar.types.enums) |entry| try names.append(self.arena, entry.name);
|
|
402
|
+
for (self.sidecar.types.unions) |entry| try names.append(self.arena, entry.name);
|
|
403
|
+
try names.append(self.arena, self.sidecar.msg.name);
|
|
404
|
+
try names.append(self.arena, self.sidecar.model);
|
|
405
|
+
|
|
406
|
+
for (names.items) |name| {
|
|
407
|
+
if (pipelineIdentifierIssue(name, .declaration)) |issue| {
|
|
408
|
+
self.diags.flag("types", "type name \"{s}\" {s} — the facade must stay declarable end to end (TypeScript source, then the compiled module, which takes identifiers verbatim); rename it in the core source", .{ name, issue });
|
|
409
|
+
}
|
|
410
|
+
// Ambient globals the generated module leans on: a
|
|
411
|
+
// re-exported declaration under one would shadow them out
|
|
412
|
+
// from under the codec.
|
|
413
|
+
if (std.mem.eql(u8, name, "Uint8Array")) {
|
|
414
|
+
self.diags.flag("types", "\"Uint8Array\" shadows the ambient byte type every encoder in the generated facade uses; rename the type in the core source", .{});
|
|
415
|
+
}
|
|
416
|
+
if (std.mem.eql(u8, name, "Buffer")) {
|
|
417
|
+
self.diags.flag("types", "\"Buffer\" shadows the ambient buffer type the generated f64 codec uses; rename the type in the core source", .{});
|
|
418
|
+
}
|
|
419
|
+
if (self.sidecar.channels.pinch_msg and std.mem.eql(u8, name, "PinchPhase")) {
|
|
420
|
+
self.diags.flag("types", "\"PinchPhase\" collides with the SDK vocabulary the wired pinch channel imports; rename the type in the core source", .{});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
// Arm, member, and field names become the compiled module's
|
|
424
|
+
// union members and struct fields verbatim.
|
|
425
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
426
|
+
if (pipelineIdentifierIssue(arm.name, .member)) |issue| {
|
|
427
|
+
self.diags.flag("msg.arms", "arm \"{s}\" {s} — the facade must stay declarable end to end; rename it in the core source", .{ arm.name, issue });
|
|
428
|
+
}
|
|
429
|
+
if (arm.member) |member| {
|
|
430
|
+
if (std.mem.eql(u8, member, "kind")) {
|
|
431
|
+
self.diags.flag("msg.arms", "arm \"{s}\" names its payload member \"kind\", the discriminator's own spelling — the constructed arm object would declare it twice; rename the member in the core source", .{arm.name});
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
for (self.sidecar.types.unions) |entry| {
|
|
436
|
+
for (entry.arms) |arm| {
|
|
437
|
+
if (pipelineIdentifierIssue(arm.name, .member)) |issue| {
|
|
438
|
+
self.diags.flag("types.unions", "arm \"{s}\" of \"{s}\" {s} — the facade must stay declarable end to end; rename it in the core source", .{ arm.name, entry.name, issue });
|
|
439
|
+
}
|
|
440
|
+
if (arm.member) |member| {
|
|
441
|
+
if (std.mem.eql(u8, member, "kind")) {
|
|
442
|
+
self.diags.flag("types.unions", "arm \"{s}\" of \"{s}\" names its payload member \"kind\", the discriminator's own spelling; rename the member in the core source", .{ arm.name, entry.name });
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
for (self.sidecar.types.enums) |entry| {
|
|
448
|
+
for (entry.members) |member| {
|
|
449
|
+
if (pipelineIdentifierIssue(member, .member)) |issue| {
|
|
450
|
+
self.diags.flag("types.enums", "member \"{s}\" of \"{s}\" {s} — the facade must stay declarable end to end; rename it in the core source", .{ member, entry.name, issue });
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (entry.members.len < 2) {
|
|
454
|
+
self.diags.flag("types", "enum \"{s}\" has one member — a single string literal is not a union in the projected subset, so no source can author it; give the state a second member or fold it away in the core source", .{entry.name});
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
for (self.sidecar.types.structs) |entry| {
|
|
458
|
+
for (entry.fields) |field| {
|
|
459
|
+
if (pipelineIdentifierIssue(field.name, .member)) |issue| {
|
|
460
|
+
self.diags.flag("types", "field \"{s}\" {s} — the facade must stay declarable end to end; rename it in the core source", .{ field.name, issue });
|
|
461
|
+
}
|
|
462
|
+
if (std.mem.startsWith(u8, field.name, "nsc_core_") or std.mem.startsWith(u8, field.name, "nscf")) {
|
|
463
|
+
self.diags.flag("types", "field \"{s}\" takes the facade's reserved nsc name space; rename it in the core source", .{field.name});
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
468
|
+
switch (arm.payload) {
|
|
469
|
+
.number_bytes => |desc| {
|
|
470
|
+
for ([_][]const u8{ desc.number_field, desc.bytes_field }) |field_name| {
|
|
471
|
+
if (pipelineIdentifierIssue(field_name, .member)) |issue| {
|
|
472
|
+
self.diags.flag("msg.arms", "field \"{s}\" {s} — the facade must stay declarable end to end; rename it in the core source", .{ field_name, issue });
|
|
473
|
+
}
|
|
474
|
+
if (std.mem.eql(u8, field_name, "kind")) {
|
|
475
|
+
self.diags.flag("msg.arms", "arm \"{s}\" flattens a field spelled \"kind\" beside the message discriminator of the same name; rename the field in the core source", .{arm.name});
|
|
476
|
+
}
|
|
477
|
+
if (std.mem.startsWith(u8, field_name, "nsc_core_") or std.mem.startsWith(u8, field_name, "nscf")) {
|
|
478
|
+
self.diags.flag("msg.arms", "field \"{s}\" takes the facade's reserved nsc name space; rename it in the core source", .{field_name});
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
},
|
|
482
|
+
.record => {
|
|
483
|
+
if (self.synthesizedRecordOf(recordPayloadRef(arm.payload), self.sidecar.msg.name, arm.name)) |record| {
|
|
484
|
+
for (record.fields) |field| {
|
|
485
|
+
if (std.mem.eql(u8, field.name, "kind")) {
|
|
486
|
+
self.diags.flag("msg.arms", "arm \"{s}\" flattens a field spelled \"kind\" beside the message discriminator of the same name; rename the field in the core source", .{arm.name});
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
},
|
|
491
|
+
else => {},
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
for (self.sidecar.types.unions) |entry| {
|
|
495
|
+
for (entry.arms) |arm| {
|
|
496
|
+
if (arm.payload == .void) continue;
|
|
497
|
+
if (self.synthesizedRecordOf(arm.payload, entry.name, arm.name)) |record| {
|
|
498
|
+
for (record.fields) |field| {
|
|
499
|
+
if (std.mem.eql(u8, field.name, "kind")) {
|
|
500
|
+
self.diags.flag("types.unions", "arm \"{s}\" of \"{s}\" flattens a field spelled \"kind\" beside the arm discriminator of the same name; rename the field in the core source", .{ arm.name, entry.name });
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
// Helper names become this module's own exported wrapper
|
|
507
|
+
// functions, and their aliases live in the nscf import space.
|
|
508
|
+
for (self.sidecar.model_helpers) |helper| {
|
|
509
|
+
if (!isTsIdentifier(helper.name) or pipelineIdentifierIssue(helper.name, .declaration) != null) {
|
|
510
|
+
self.diags.flag("model_helpers", "helper \"{s}\" is not declarable as an exported function in both pipeline languages; rename it in the core source", .{helper.name});
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
if (std.mem.startsWith(u8, helper.name, "nsc_core_") or std.mem.startsWith(u8, helper.name, "nscf")) {
|
|
514
|
+
self.diags.flag("model_helpers", "helper \"{s}\" takes the facade's reserved nsc name space; rename it in the core source", .{helper.name});
|
|
515
|
+
}
|
|
516
|
+
for (fixed_exports) |fixed| {
|
|
517
|
+
if (std.mem.eql(u8, helper.name, fixed)) {
|
|
518
|
+
self.diags.flag("model_helpers", "helper \"{s}\" collides with a declaration the generated facade itself must export; rename it in the core source", .{helper.name});
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
for (ambient_value_names) |ambient| {
|
|
522
|
+
if (std.mem.eql(u8, helper.name, ambient)) {
|
|
523
|
+
self.diags.flag("model_helpers", "helper \"{s}\" shadows an ambient value the generated facade calls; rename it in the core source", .{helper.name});
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
if (helper.params.len > 0) {
|
|
527
|
+
self.diags.flag("model_helpers", "helper \"{s}\" declares extra parameters — the generated dispatch surface carries model-only helpers (no producer emits parameters today); drop them in the core source", .{helper.name});
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
for (self.sidecar.types.structs) |entry| try self.fenceDecl(entry.name);
|
|
531
|
+
for (self.sidecar.types.enums) |entry| try self.fenceDecl(entry.name);
|
|
532
|
+
for (self.sidecar.types.unions) |entry| try self.fenceDecl(entry.name);
|
|
533
|
+
try self.fenceDecl(self.sidecar.msg.name);
|
|
534
|
+
|
|
535
|
+
// An originless table name in a current sidecar is compiler-created,
|
|
536
|
+
// not a TypeScript export the facade can import. Single-use inline
|
|
537
|
+
// records disappear back into their arm literals; every other such
|
|
538
|
+
// name (most notably a monomorphized generic like `Box__Item`) would
|
|
539
|
+
// make the facade invalid, and the external library compiler cannot
|
|
540
|
+
// project the authored generic contract surface either. Refuse at the
|
|
541
|
+
// generator boundary instead of emitting a downstream failure.
|
|
542
|
+
for (self.sidecar.types.structs) |entry| {
|
|
543
|
+
if (self.isGeneratedOnlyType(entry.name) and !nameListed(self.flattened, entry.name)) {
|
|
544
|
+
self.diags.flag("types", "compiler-generated table type \"{s}\" has no authored TypeScript declaration the facade can name — generic instantiations cannot cross the compiled-core contract surface yet; replace the contract-facing generic with an authored concrete record", .{entry.name});
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
for (self.sidecar.types.enums) |entry| {
|
|
548
|
+
if (self.isGeneratedOnlyType(entry.name)) {
|
|
549
|
+
self.diags.flag("types", "compiler-generated table type \"{s}\" has no authored TypeScript declaration the facade can name — generic instantiations cannot cross the compiled-core contract surface yet; replace the contract-facing generic with an authored concrete type", .{entry.name});
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
for (self.sidecar.types.unions) |entry| {
|
|
553
|
+
if (self.isGeneratedOnlyType(entry.name)) {
|
|
554
|
+
self.diags.flag("types", "compiler-generated table type \"{s}\" has no authored TypeScript declaration the facade can name — generic instantiations cannot cross the compiled-core contract surface yet; replace the contract-facing generic with an authored concrete union", .{entry.name});
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// Declaration form spells storage ONCE per record, so a record
|
|
559
|
+
// referenced both by node and by value has no projection — one
|
|
560
|
+
// declaration cannot say both. (The transpiled lane decides
|
|
561
|
+
// storage per TYPE, so its contracts never mix; a hand contract
|
|
562
|
+
// that does must split the type.)
|
|
563
|
+
var value_refs: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
564
|
+
for (self.sidecar.types.structs) |entry| {
|
|
565
|
+
for (entry.fields) |field| try noteValueRefs(&value_refs, self.arena, field.type);
|
|
566
|
+
}
|
|
567
|
+
for (self.sidecar.types.unions) |entry| {
|
|
568
|
+
for (entry.arms) |arm| try noteValueRefs(&value_refs, self.arena, arm.payload);
|
|
569
|
+
}
|
|
570
|
+
for (self.sidecar.model_helpers) |helper| {
|
|
571
|
+
try noteValueRefs(&value_refs, self.arena, helper.returns);
|
|
572
|
+
for (helper.params) |param| try noteValueRefs(&value_refs, self.arena, param);
|
|
573
|
+
}
|
|
574
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
575
|
+
switch (arm.payload) {
|
|
576
|
+
.scalar => |ref| try noteValueRefs(&value_refs, self.arena, ref),
|
|
577
|
+
else => {},
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
for (value_refs.items) |name| {
|
|
581
|
+
if (nameListed(self.node_stored, name)) {
|
|
582
|
+
self.diags.flag("types", "\"{s}\" is stored by reference at one site and by value at another — the projection states storage once per declaration, so one record cannot say both; split the type in the core source", .{name});
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// Value records the MODEL keeps carry scalar fields only, and
|
|
587
|
+
// model sequences carry reference-stored records: the compiled
|
|
588
|
+
// core commits by-value records shallowly, so heap-backed
|
|
589
|
+
// fields would dangle across frames and by-value arrays have no
|
|
590
|
+
// commit walk. Refuse here, where the teaching can name the
|
|
591
|
+
// record, instead of emitting a facade its compiler refuses.
|
|
592
|
+
try self.validateModelValueRecords();
|
|
593
|
+
|
|
594
|
+
// The host-constructed channels build their event record's
|
|
595
|
+
// fields DIRECTLY on the named arm, so the arm's record must
|
|
596
|
+
// flatten into the arm literal (the single-use synthesized
|
|
597
|
+
// shape). A shared named record would project as a nested
|
|
598
|
+
// member no host construction can fill.
|
|
599
|
+
if (self.sidecar.channels.appearance_msg) |arm_name| {
|
|
600
|
+
try self.requireFlattenedChannelArm("channels.appearance_msg", arm_name);
|
|
601
|
+
}
|
|
602
|
+
if (self.sidecar.channels.chrome_msg) |arm_name| {
|
|
603
|
+
try self.requireFlattenedChannelArm("channels.chrome_msg", arm_name);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/// Walk the model tree and refuse the value-record shapes the
|
|
608
|
+
/// compiled core cannot carry: a model-kept value record with a
|
|
609
|
+
/// non-scalar field, and a model sequence of value records.
|
|
610
|
+
fn validateModelValueRecords(self: *FacadeEmitter) Error!void {
|
|
611
|
+
var visited: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
612
|
+
const model = sidecar_mod.findStruct(self.sidecar.types, self.sidecar.model) orelse return;
|
|
613
|
+
for (model.fields) |field| {
|
|
614
|
+
try self.visitModelRef(&visited, field.type);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
fn visitModelRef(self: *FacadeEmitter, visited: *std.ArrayListUnmanaged([]const u8), ref: TypeRef) Error!void {
|
|
619
|
+
switch (ref) {
|
|
620
|
+
.value => |name| {
|
|
621
|
+
if (nameListed(visited.items, name)) return;
|
|
622
|
+
try visited.append(self.arena, name);
|
|
623
|
+
const entry = sidecar_mod.findStruct(self.sidecar.types, name) orelse return;
|
|
624
|
+
for (entry.fields) |field| {
|
|
625
|
+
const scalar = switch (field.type) {
|
|
626
|
+
.f64, .i64, .bool, .enum_ref => true,
|
|
627
|
+
else => false,
|
|
628
|
+
};
|
|
629
|
+
if (!scalar) {
|
|
630
|
+
self.diags.flag("types", "\"{s}\" is a value-stored record the model keeps, but field \"{s}\" is not a scalar — the compiled projection commits value records shallowly, so heap-backed fields would dangle across frames; store \"{s}\" by reference in the core source", .{ name, field.name, name });
|
|
631
|
+
break;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
.node => |name| {
|
|
636
|
+
if (nameListed(visited.items, name)) return;
|
|
637
|
+
try visited.append(self.arena, name);
|
|
638
|
+
const entry = sidecar_mod.findStruct(self.sidecar.types, name) orelse return;
|
|
639
|
+
for (entry.fields) |field| {
|
|
640
|
+
try self.visitModelRef(visited, field.type);
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
.union_ref => |name| {
|
|
644
|
+
if (nameListed(visited.items, name)) return;
|
|
645
|
+
try visited.append(self.arena, name);
|
|
646
|
+
const entry = sidecar_mod.findUnion(self.sidecar.types, name) orelse return;
|
|
647
|
+
for (entry.arms) |arm| {
|
|
648
|
+
try self.visitModelRef(visited, arm.payload);
|
|
649
|
+
}
|
|
650
|
+
},
|
|
651
|
+
.slice => |elem| {
|
|
652
|
+
const element = if (elem.* == .optional) elem.optional.* else elem.*;
|
|
653
|
+
if (element == .value) {
|
|
654
|
+
self.diags.flag("types", "a model sequence holds \"{s}\" by value — sequences the model keeps carry reference-stored records (the compiled projection has no by-value sequence commit); store \"{s}\" by reference in the core source", .{ element.value, element.value });
|
|
655
|
+
}
|
|
656
|
+
try self.visitModelRef(visited, elem.*);
|
|
657
|
+
},
|
|
658
|
+
.optional => |inner| try self.visitModelRef(visited, inner.*),
|
|
659
|
+
else => {},
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/// Refuse a host-constructed channel arm whose record does not
|
|
664
|
+
/// flatten into its arm literal.
|
|
665
|
+
fn requireFlattenedChannelArm(self: *FacadeEmitter, at: []const u8, arm_name: []const u8) Error!void {
|
|
666
|
+
const arm = sidecar_mod.findArm(self.sidecar.msg, arm_name) orelse return;
|
|
667
|
+
switch (arm.payload) {
|
|
668
|
+
.record => |name| {
|
|
669
|
+
if (nameListed(self.flattened, name)) return;
|
|
670
|
+
self.diags.flag(at, "arm \"{s}\" carries the named record \"{s}\", which the projection cannot flatten into the arm (the host fills the event's fields directly on the arm) — declare the event's fields inline on the arm in the core source", .{ arm_name, name });
|
|
671
|
+
},
|
|
672
|
+
else => {},
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
fn fenceDecl(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
677
|
+
if (name.len == 0) return;
|
|
678
|
+
if (std.mem.startsWith(u8, name, "nsc_core_") or std.mem.startsWith(u8, name, "nscf")) {
|
|
679
|
+
self.diags.flag("types", "\"{s}\" collides with the facade's reserved nsc name space; rename it in the core source", .{name});
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
for (fixed_exports) |decl| {
|
|
683
|
+
if (std.mem.eql(u8, name, decl)) {
|
|
684
|
+
self.diags.flag("types", "\"{s}\" collides with a declaration the generated facade itself must make; rename it in the core source", .{name});
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/// TypeScript's null carries exactly one absence level, so a nested
|
|
690
|
+
/// optional has no faithful projection (its own source language
|
|
691
|
+
/// cannot author one either: `T | null | null` collapses).
|
|
692
|
+
fn validateOptionalDepth(self: *FacadeEmitter) void {
|
|
693
|
+
for (self.sidecar.types.structs, 0..) |entry, index| {
|
|
694
|
+
for (entry.fields, 0..) |field, field_index| {
|
|
695
|
+
self.flagNestedOptional(field.type, false, pathText(self.arena, "types.structs[{d}].fields[{d}].type", .{ index, field_index }));
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
for (self.sidecar.types.unions, 0..) |entry, index| {
|
|
699
|
+
for (entry.arms, 0..) |arm, arm_index| {
|
|
700
|
+
self.flagNestedOptional(arm.payload, false, pathText(self.arena, "types.unions[{d}].arms[{d}].payload", .{ index, arm_index }));
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
for (self.sidecar.model_helpers, 0..) |helper, index| {
|
|
704
|
+
self.flagNestedOptional(helper.returns, false, pathText(self.arena, "model_helpers[{d}].returns", .{index}));
|
|
705
|
+
for (helper.params, 0..) |param, param_index| {
|
|
706
|
+
self.flagNestedOptional(param, false, pathText(self.arena, "model_helpers[{d}].params[{d}]", .{ index, param_index }));
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
for (self.sidecar.msg.arms, 0..) |arm, index| {
|
|
710
|
+
switch (arm.payload) {
|
|
711
|
+
.scalar => |ref| self.flagNestedOptional(ref, false, pathText(self.arena, "msg.arms[{d}].payload.type", .{index})),
|
|
712
|
+
else => {},
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
fn flagNestedOptional(self: *FacadeEmitter, ref: TypeRef, inside_optional: bool, at: []const u8) void {
|
|
718
|
+
switch (ref) {
|
|
719
|
+
.optional => |inner| {
|
|
720
|
+
if (inside_optional) {
|
|
721
|
+
self.diags.flag(at, "a nested optional has no TypeScript projection — one null carries one absence level, and the source language cannot author a second; flatten the state in the core source", .{});
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
self.flagNestedOptional(inner.*, true, at);
|
|
725
|
+
},
|
|
726
|
+
.slice => |elem| self.flagNestedOptional(elem.*, false, at),
|
|
727
|
+
else => {},
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// ------------------------------------------- header and imports
|
|
732
|
+
|
|
733
|
+
fn header(self: *FacadeEmitter) Error!void {
|
|
734
|
+
try self.print(
|
|
735
|
+
\\// Generated by corewire from this app's core.contract.json — the
|
|
736
|
+
\\// compiled-core entry module. It imports the author's core module
|
|
737
|
+
\\// (staged beside this file with its SDK imports resolved locally),
|
|
738
|
+
\\// owns the committed model in module state, and exports the
|
|
739
|
+
\\// profile-declared dispatch surface. Inbound payloads decode
|
|
740
|
+
\\// through the generated wire codec below; returned Cmd/Sub data
|
|
741
|
+
\\// encodes through the same codec's v3 wires — byte-identical to
|
|
742
|
+
\\// the transpiler lane's output. The same sidecar generates the
|
|
743
|
+
\\// host's Zig mirror (core_shim.zig), so the two sides carry one
|
|
744
|
+
\\// set of types, wire tags, field orders, and byte encodings by
|
|
745
|
+
\\// construction. Do not edit; regenerate from the sidecar.
|
|
746
|
+
\\//
|
|
747
|
+
\\// Contract identity: entry {s}, compiler {s}, build_id
|
|
748
|
+
\\// {x:0>16}.
|
|
749
|
+
\\
|
|
750
|
+
, .{ try commentText(self.arena, self.sidecar.entry), try commentText(self.arena, self.sidecar.compiler_version), self.sidecar.build_id });
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/// The import block: the author's behavioral exports under nscf
|
|
754
|
+
/// aliases, the referenced contract types from their declaring
|
|
755
|
+
/// modules, and the SDK effect types the codec spells.
|
|
756
|
+
fn imports(self: *FacadeEmitter, body: *FacadeEmitter) Error!void {
|
|
757
|
+
const entry_module = try self.importPath(self.entryBasename());
|
|
758
|
+
// Behavioral value imports, all from the entry module.
|
|
759
|
+
var values: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
760
|
+
try values.append(self.arena, "initialModel as nscfInitialModel");
|
|
761
|
+
try values.append(self.arena, "update as nscfUpdate");
|
|
762
|
+
if (self.sidecar.has_subscriptions) try values.append(self.arena, "subscriptions as nscfSubscriptions");
|
|
763
|
+
if (self.sidecar.channels.command_msg) try values.append(self.arena, "commandMsg as nscfChanCommandMsg");
|
|
764
|
+
if (self.sidecar.channels.frame_msg) try values.append(self.arena, "frameMsg as nscfChanFrameMsg");
|
|
765
|
+
if (self.sidecar.channels.key_msg) try values.append(self.arena, "keyMsg as nscfChanKeyMsg");
|
|
766
|
+
if (self.sidecar.channels.pinch_msg) try values.append(self.arena, "pinchMsg as nscfChanPinchMsg");
|
|
767
|
+
for (self.sidecar.model_helpers) |helper| {
|
|
768
|
+
try values.append(self.arena, try std.fmt.allocPrint(self.arena, "{s} as nscfH_{s}", .{ helper.name, helper.name }));
|
|
769
|
+
}
|
|
770
|
+
try self.raw("\nimport {\n");
|
|
771
|
+
for (values.items) |item| try self.print(" {s},\n", .{item});
|
|
772
|
+
try self.print("}} from {s};\n", .{entry_module});
|
|
773
|
+
|
|
774
|
+
// Referenced contract types, grouped by declaring module in
|
|
775
|
+
// first-reference order (Model and Msg lead).
|
|
776
|
+
var groups: std.ArrayListUnmanaged(struct { origin: []const u8, names: std.ArrayListUnmanaged([]const u8) }) = .empty;
|
|
777
|
+
var ordered: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
778
|
+
try ordered.append(self.arena, self.sidecar.model);
|
|
779
|
+
try ordered.append(self.arena, self.sidecar.msg.name);
|
|
780
|
+
for (body.referenced.items) |name| {
|
|
781
|
+
if (!nameListed(ordered.items, name)) try ordered.append(self.arena, name);
|
|
782
|
+
}
|
|
783
|
+
for (ordered.items) |name| {
|
|
784
|
+
const origin = self.originOf(name);
|
|
785
|
+
const group = for (groups.items) |*candidate| {
|
|
786
|
+
if (std.mem.eql(u8, candidate.origin, origin)) break candidate;
|
|
787
|
+
} else blk: {
|
|
788
|
+
try groups.append(self.arena, .{ .origin = origin, .names = .empty });
|
|
789
|
+
break :blk &groups.items[groups.items.len - 1];
|
|
790
|
+
};
|
|
791
|
+
try group.names.append(self.arena, name);
|
|
792
|
+
}
|
|
793
|
+
for (groups.items) |group| {
|
|
794
|
+
try self.raw("import type { ");
|
|
795
|
+
for (group.names.items, 0..) |name, index| {
|
|
796
|
+
try self.print("{s}{s}", .{ if (index == 0) "" else ", ", name });
|
|
797
|
+
}
|
|
798
|
+
try self.print(" }} from {s};\n", .{try self.importPath(group.origin)});
|
|
799
|
+
}
|
|
800
|
+
// The SDK effect vocabulary the codec spells.
|
|
801
|
+
const needs_cmd = body.used_codec.contains(.cmd_encoder);
|
|
802
|
+
const needs_sub = body.used_codec.contains(.sub_encoder);
|
|
803
|
+
if (needs_cmd and needs_sub) {
|
|
804
|
+
try self.raw("import type { Cmd as nscfCmd, Sub as nscfSub } from \"./sdk/core.ts\";\n");
|
|
805
|
+
} else if (needs_cmd) {
|
|
806
|
+
try self.raw("import type { Cmd as nscfCmd } from \"./sdk/core.ts\";\n");
|
|
807
|
+
} else if (needs_sub) {
|
|
808
|
+
try self.raw("import type { Sub as nscfSub } from \"./sdk/core.ts\";\n");
|
|
809
|
+
}
|
|
810
|
+
// The pinch channel's SDK vocabulary (declared in the SDK's
|
|
811
|
+
// events module, outside the contract tables).
|
|
812
|
+
if (body.needs_pinch_phase) {
|
|
813
|
+
try self.raw("import type { PinchPhase } from \"./sdk/events.ts\";\n");
|
|
814
|
+
}
|
|
815
|
+
self.needs_pinch_phase = body.needs_pinch_phase;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
fn importPath(self: *FacadeEmitter, origin: []const u8) Error![]const u8 {
|
|
819
|
+
return std.fmt.allocPrint(self.arena, "\"./{s}\"", .{try tsString(self.arena, origin)});
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
/// Type re-exports: the contract type designations the compiler's
|
|
823
|
+
/// sidecar section names — every named (non-synthesized) table type,
|
|
824
|
+
/// re-exported verbatim from its own declaring module.
|
|
825
|
+
fn reexports(self: *FacadeEmitter) Error!void {
|
|
826
|
+
var ordered: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
827
|
+
try ordered.append(self.arena, self.sidecar.model);
|
|
828
|
+
try ordered.append(self.arena, self.sidecar.msg.name);
|
|
829
|
+
for (self.sidecar.types.structs) |entry| {
|
|
830
|
+
if (nameListed(self.inlined, entry.name)) continue;
|
|
831
|
+
if (self.isGeneratedOnlyType(entry.name)) continue;
|
|
832
|
+
if (!entry.exported) continue;
|
|
833
|
+
if (!nameListed(ordered.items, entry.name)) try ordered.append(self.arena, entry.name);
|
|
834
|
+
}
|
|
835
|
+
for (self.sidecar.types.enums) |entry| {
|
|
836
|
+
if (self.isGeneratedOnlyType(entry.name)) continue;
|
|
837
|
+
if (!entry.exported) continue;
|
|
838
|
+
if (!nameListed(ordered.items, entry.name)) try ordered.append(self.arena, entry.name);
|
|
839
|
+
}
|
|
840
|
+
for (self.sidecar.types.unions) |entry| {
|
|
841
|
+
if (self.isGeneratedOnlyType(entry.name)) continue;
|
|
842
|
+
if (!entry.exported) continue;
|
|
843
|
+
if (!nameListed(ordered.items, entry.name)) try ordered.append(self.arena, entry.name);
|
|
844
|
+
}
|
|
845
|
+
var groups: std.ArrayListUnmanaged(struct { origin: []const u8, names: std.ArrayListUnmanaged([]const u8) }) = .empty;
|
|
846
|
+
for (ordered.items) |name| {
|
|
847
|
+
const origin = self.originOf(name);
|
|
848
|
+
const group = for (groups.items) |*candidate| {
|
|
849
|
+
if (std.mem.eql(u8, candidate.origin, origin)) break candidate;
|
|
850
|
+
} else blk: {
|
|
851
|
+
try groups.append(self.arena, .{ .origin = origin, .names = .empty });
|
|
852
|
+
break :blk &groups.items[groups.items.len - 1];
|
|
853
|
+
};
|
|
854
|
+
try group.names.append(self.arena, name);
|
|
855
|
+
}
|
|
856
|
+
try self.raw(
|
|
857
|
+
\\
|
|
858
|
+
\\// The contract type designations the sidecar section names — the
|
|
859
|
+
\\// author's exported declarations re-exported verbatim. Private
|
|
860
|
+
\\// reachable declarations have structural twins below instead: their
|
|
861
|
+
\\// authored modules expose no name this facade can import.
|
|
862
|
+
\\
|
|
863
|
+
);
|
|
864
|
+
for (groups.items) |group| {
|
|
865
|
+
try self.raw("export type { ");
|
|
866
|
+
for (group.names.items, 0..) |name, index| {
|
|
867
|
+
try self.print("{s}{s}", .{ if (index == 0) "" else ", ", name });
|
|
868
|
+
}
|
|
869
|
+
try self.print(" }} from {s};\n", .{try self.importPath(group.origin)});
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/// Structural twins for authored table types their declaring modules do
|
|
874
|
+
/// not export. They stay module-private too: generated readers/writers can
|
|
875
|
+
/// name the contract shape without inventing a public authoring surface.
|
|
876
|
+
fn privateTypeDeclarations(self: *FacadeEmitter) Error!void {
|
|
877
|
+
var any = false;
|
|
878
|
+
for (self.sidecar.types.structs) |entry| if (!entry.exported) {
|
|
879
|
+
any = true;
|
|
880
|
+
break;
|
|
881
|
+
};
|
|
882
|
+
if (!any) for (self.sidecar.types.enums) |entry| if (!entry.exported) {
|
|
883
|
+
any = true;
|
|
884
|
+
break;
|
|
885
|
+
};
|
|
886
|
+
if (!any) for (self.sidecar.types.unions) |entry| if (!entry.exported) {
|
|
887
|
+
any = true;
|
|
888
|
+
break;
|
|
889
|
+
};
|
|
890
|
+
if (!any) return;
|
|
891
|
+
|
|
892
|
+
try self.raw(
|
|
893
|
+
\\
|
|
894
|
+
\\// Private contract-table declarations, reconstructed structurally
|
|
895
|
+
\\// because their authored modules expose no importable designation.
|
|
896
|
+
\\
|
|
897
|
+
);
|
|
898
|
+
for (self.sidecar.types.structs) |entry| {
|
|
899
|
+
if (entry.exported) continue;
|
|
900
|
+
try self.print("type {s} = {{\n", .{entry.name});
|
|
901
|
+
for (entry.fields) |field| {
|
|
902
|
+
try self.print(" readonly {s}: {s};\n", .{ try tsProp(self.arena, field.name), try self.spellRef(field.type) });
|
|
903
|
+
}
|
|
904
|
+
try self.raw("};\n\n");
|
|
905
|
+
}
|
|
906
|
+
for (self.sidecar.types.enums) |entry| {
|
|
907
|
+
if (entry.exported) continue;
|
|
908
|
+
try self.print("type {s} = ", .{entry.name});
|
|
909
|
+
for (entry.members, 0..) |member, index| {
|
|
910
|
+
try self.print("{s}\"{s}\"", .{ if (index == 0) "" else " | ", try tsString(self.arena, member) });
|
|
911
|
+
}
|
|
912
|
+
try self.raw(";\n\n");
|
|
913
|
+
}
|
|
914
|
+
for (self.sidecar.types.unions) |entry| {
|
|
915
|
+
if (entry.exported) continue;
|
|
916
|
+
try self.print("type {s} =\n", .{entry.name});
|
|
917
|
+
for (entry.arms) |arm| {
|
|
918
|
+
try self.print(" | {{ readonly kind: \"{s}\"", .{try tsString(self.arena, arm.name)});
|
|
919
|
+
if (arm.payload != .void) {
|
|
920
|
+
if (self.synthesizedRecordOf(arm.payload, entry.name, arm.name)) |record| {
|
|
921
|
+
for (record.fields) |field| {
|
|
922
|
+
try self.print("; readonly {s}: {s}", .{ try tsProp(self.arena, field.name), try self.spellRef(field.type) });
|
|
923
|
+
}
|
|
924
|
+
} else {
|
|
925
|
+
try self.print("; readonly {s}: {s}", .{ try tsProp(self.arena, self.memberOf(arm.member)), try self.spellRef(arm.payload) });
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
try self.raw(" }\n");
|
|
929
|
+
}
|
|
930
|
+
try self.raw(";\n\n");
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
// -------------------------------------- channel consts and unbound
|
|
935
|
+
|
|
936
|
+
fn channelConsts(self: *FacadeEmitter) Error!void {
|
|
937
|
+
if (self.sidecar.channels.appearance_msg == null and self.sidecar.channels.chrome_msg == null and self.sidecar.channels.env_msgs.len == 0) return;
|
|
938
|
+
try self.raw(
|
|
939
|
+
\\
|
|
940
|
+
\\// The exported-const channel conventions must be DECLARED here (the
|
|
941
|
+
\\// sidecar emitter reads the entry module's own declarations;
|
|
942
|
+
\\// re-exports do not join its tables).
|
|
943
|
+
\\
|
|
944
|
+
);
|
|
945
|
+
if (self.sidecar.channels.appearance_msg) |arm_name| {
|
|
946
|
+
try self.print("\nexport const appearanceMsg = \"{s}\";\n", .{try tsString(self.arena, arm_name)});
|
|
947
|
+
}
|
|
948
|
+
if (self.sidecar.channels.chrome_msg) |arm_name| {
|
|
949
|
+
try self.print("\nexport const chromeMsg = \"{s}\";\n", .{try tsString(self.arena, arm_name)});
|
|
950
|
+
}
|
|
951
|
+
if (self.sidecar.channels.env_msgs.len > 0) {
|
|
952
|
+
try self.raw("\nexport const envMsgs = [\n");
|
|
953
|
+
for (self.sidecar.channels.env_msgs) |entry| {
|
|
954
|
+
try self.print(" {{ env: \"{s}\", msg: \"{s}\" }},\n", .{ try tsString(self.arena, entry.env), try tsString(self.arena, entry.msg) });
|
|
955
|
+
}
|
|
956
|
+
try self.raw("];\n");
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
fn unboundDecl(self: *FacadeEmitter) Error!void {
|
|
961
|
+
const model = sidecar_mod.findStruct(self.sidecar.types, self.sidecar.model).?;
|
|
962
|
+
var field_unbound: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
963
|
+
for (self.sidecar.model_unbound) |name| {
|
|
964
|
+
for (model.fields) |field| {
|
|
965
|
+
if (std.mem.eql(u8, field.name, name)) {
|
|
966
|
+
try field_unbound.append(self.arena, name);
|
|
967
|
+
break;
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
// Helper entries stay sidecar facts resolved by the compiler's
|
|
972
|
+
// own name resolution: a helper name in the authored list
|
|
973
|
+
// resolves against this module's exported wrappers.
|
|
974
|
+
var helper_unbound: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
975
|
+
for (self.sidecar.model_unbound) |name| {
|
|
976
|
+
for (self.sidecar.model_helpers) |helper| {
|
|
977
|
+
if (std.mem.eql(u8, helper.name, name)) {
|
|
978
|
+
try helper_unbound.append(self.arena, name);
|
|
979
|
+
break;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
// The authoring surface is ONE list resolved with the compiler's
|
|
984
|
+
// Model-first precedence: fields, then helpers, then message arms.
|
|
985
|
+
// A model-side homonym therefore projects faithfully, but an
|
|
986
|
+
// unbound message arm shadowed by either model binding cannot be
|
|
987
|
+
// named by this list. Refuse that direction instead of silently
|
|
988
|
+
// marking the model binding and leaving the arm bound.
|
|
989
|
+
for (self.sidecar.msg.unbound) |name| {
|
|
990
|
+
const also_field = for (model.fields) |field| {
|
|
991
|
+
if (std.mem.eql(u8, field.name, name)) break true;
|
|
992
|
+
} else false;
|
|
993
|
+
const also_helper = for (self.sidecar.model_helpers) |helper| {
|
|
994
|
+
if (std.mem.eql(u8, helper.name, name)) break true;
|
|
995
|
+
} else false;
|
|
996
|
+
if (also_field or also_helper) {
|
|
997
|
+
self.diags.flag("msg.unbound", "\"{s}\" is an unbound message arm shadowed by a homonymous Model field or exported helper — the compiler resolves Model fields and helpers before message arms, so the projection's single unbound list cannot mark this arm; rename one side in the core source", .{name});
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
if (self.diags.hasErrors()) return;
|
|
1001
|
+
if (field_unbound.items.len == 0 and helper_unbound.items.len == 0 and self.sidecar.msg.unbound.len == 0) return;
|
|
1002
|
+
try self.raw(
|
|
1003
|
+
\\
|
|
1004
|
+
\\// The unbound-list declaration, carried by the generator from the
|
|
1005
|
+
\\// author's own markings in the core module: message arms only the
|
|
1006
|
+
\\// host fires, and model fields and helpers only update logic reads.
|
|
1007
|
+
\\export const viewUnbound = [
|
|
1008
|
+
\\
|
|
1009
|
+
);
|
|
1010
|
+
for (self.sidecar.msg.unbound) |name| {
|
|
1011
|
+
try self.print(" \"{s}\",\n", .{try tsString(self.arena, name)});
|
|
1012
|
+
}
|
|
1013
|
+
for (field_unbound.items) |name| {
|
|
1014
|
+
try self.print(" \"{s}\",\n", .{try tsString(self.arena, name)});
|
|
1015
|
+
}
|
|
1016
|
+
for (helper_unbound.items) |name| {
|
|
1017
|
+
try self.print(" \"{s}\",\n", .{try tsString(self.arena, name)});
|
|
1018
|
+
}
|
|
1019
|
+
try self.raw("];\n");
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
// ------------------------------------------------- helper wrappers
|
|
1023
|
+
|
|
1024
|
+
fn helperWrappers(self: *FacadeEmitter) Error!void {
|
|
1025
|
+
if (self.sidecar.model_helpers.len == 0) return;
|
|
1026
|
+
try self.raw(
|
|
1027
|
+
\\
|
|
1028
|
+
\\// The model helpers must be DECLARED here too, in declaration order
|
|
1029
|
+
\\// — the compiled contract's model_helpers table and helper_call's
|
|
1030
|
+
\\// index space both derive from it. Classed helper returns prove at
|
|
1031
|
+
\\// this boundary: bind the helper's value, range-guard it (an
|
|
1032
|
+
\\// ordered comparison excludes NaN), and state wholeness with
|
|
1033
|
+
\\// Math.trunc at the return.
|
|
1034
|
+
\\
|
|
1035
|
+
);
|
|
1036
|
+
for (self.sidecar.model_helpers) |helper| {
|
|
1037
|
+
const return_type = try self.spellRef(helper.returns);
|
|
1038
|
+
const is_int = helper.returns == .i64;
|
|
1039
|
+
const is_optional_int = helper.returns == .optional and helper.returns.optional.* == .i64;
|
|
1040
|
+
if (is_int or is_optional_int) {
|
|
1041
|
+
const class = self.nestedSlotClass("helpers", helper.name, "return") orelse .i64;
|
|
1042
|
+
if (is_optional_int) {
|
|
1043
|
+
try self.print(
|
|
1044
|
+
\\
|
|
1045
|
+
\\export function {s}(model: {s}): {s} {{
|
|
1046
|
+
\\ const nscfValue = nscfH_{s}(model);
|
|
1047
|
+
\\ if (nscfValue === null) return null;
|
|
1048
|
+
\\ if (nscfValue >= {s} && nscfValue <= {s}) return Math.trunc(nscfValue);
|
|
1049
|
+
\\ nscfTrap("a helper return is NaN or outside its attested exact-integer range — the integer slot has no honest value for it");
|
|
1050
|
+
\\}}
|
|
1051
|
+
\\
|
|
1052
|
+
, .{ helper.name, self.sidecar.model, return_type, helper.name, integerLowerBound(class), max_safe });
|
|
1053
|
+
} else {
|
|
1054
|
+
try self.print(
|
|
1055
|
+
\\
|
|
1056
|
+
\\export function {s}(model: {s}): number {{
|
|
1057
|
+
\\ const nscfValue = nscfH_{s}(model);
|
|
1058
|
+
\\ if (nscfValue >= {s} && nscfValue <= {s}) return Math.trunc(nscfValue);
|
|
1059
|
+
\\ nscfTrap("a helper return is NaN or outside its attested exact-integer range — the integer slot has no honest value for it");
|
|
1060
|
+
\\}}
|
|
1061
|
+
\\
|
|
1062
|
+
, .{ helper.name, self.sidecar.model, helper.name, integerLowerBound(class), max_safe });
|
|
1063
|
+
}
|
|
1064
|
+
self.use(.trap);
|
|
1065
|
+
} else if (helper.returns == .slice) {
|
|
1066
|
+
try self.print(
|
|
1067
|
+
\\
|
|
1068
|
+
\\export function {s}(model: {s}): {s} {{
|
|
1069
|
+
\\ return nscfH_{s}(model) as {s};
|
|
1070
|
+
\\}}
|
|
1071
|
+
\\
|
|
1072
|
+
, .{ helper.name, self.sidecar.model, return_type, helper.name, return_type });
|
|
1073
|
+
} else {
|
|
1074
|
+
try self.print(
|
|
1075
|
+
\\
|
|
1076
|
+
\\export function {s}(model: {s}): {s} {{
|
|
1077
|
+
\\ return nscfH_{s}(model);
|
|
1078
|
+
\\}}
|
|
1079
|
+
\\
|
|
1080
|
+
, .{ helper.name, self.sidecar.model, return_type, helper.name });
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
// ---------------------------------------------------- entry points
|
|
1086
|
+
|
|
1087
|
+
fn entryPoints(self: *FacadeEmitter) Error!void {
|
|
1088
|
+
try self.raw(
|
|
1089
|
+
\\
|
|
1090
|
+
\\// The designated shape-flag exports: the profile names init and
|
|
1091
|
+
\\// coreUpdate, and their return shapes restate the contract's
|
|
1092
|
+
\\// init_returns_cmd/update_returns_cmd flags.
|
|
1093
|
+
\\
|
|
1094
|
+
);
|
|
1095
|
+
if (self.sidecar.init_returns_cmd and self.sidecar.init_returns_bare) {
|
|
1096
|
+
self.use(.cmd_encoder);
|
|
1097
|
+
// The mixed author shape (`Model | [Model, Cmd<Msg>]`, the
|
|
1098
|
+
// documented idiom): the wrapper narrows — a tuple carries
|
|
1099
|
+
// its command, a bare model the empty command buffer.
|
|
1100
|
+
try self.print(
|
|
1101
|
+
\\
|
|
1102
|
+
\\export function init(): [{s}, Uint8Array] {{
|
|
1103
|
+
\\ const out = nscfInitialModel();
|
|
1104
|
+
\\ if (Array.isArray(out)) return [out[0], nscfCmdBytes(out[1])];
|
|
1105
|
+
\\ return [out, new Uint8Array(0)];
|
|
1106
|
+
\\}}
|
|
1107
|
+
\\
|
|
1108
|
+
, .{self.sidecar.model});
|
|
1109
|
+
} else if (self.sidecar.init_returns_cmd) {
|
|
1110
|
+
self.use(.cmd_encoder);
|
|
1111
|
+
try self.print(
|
|
1112
|
+
\\
|
|
1113
|
+
\\export function init(): [{s}, Uint8Array] {{
|
|
1114
|
+
\\ const pair = nscfInitialModel();
|
|
1115
|
+
\\ return [pair[0], nscfCmdBytes(pair[1])];
|
|
1116
|
+
\\}}
|
|
1117
|
+
\\
|
|
1118
|
+
, .{self.sidecar.model});
|
|
1119
|
+
} else {
|
|
1120
|
+
try self.print(
|
|
1121
|
+
\\
|
|
1122
|
+
\\export function init(): {s} {{
|
|
1123
|
+
\\ return nscfInitialModel();
|
|
1124
|
+
\\}}
|
|
1125
|
+
\\
|
|
1126
|
+
, .{self.sidecar.model});
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
fn tagTable(self: *FacadeEmitter) Error!void {
|
|
1131
|
+
try self.raw(
|
|
1132
|
+
\\
|
|
1133
|
+
\\// Declaration-order wire tags (the contract sidecar is the tag
|
|
1134
|
+
\\// authority; a skewed table cannot survive the paired battery's
|
|
1135
|
+
\\// byte comparison or the mirror's boot fence).
|
|
1136
|
+
\\const nscfArmNames = [
|
|
1137
|
+
\\
|
|
1138
|
+
);
|
|
1139
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1140
|
+
try self.print(" \"{s}\",\n", .{try tsString(self.arena, arm.name)});
|
|
1141
|
+
}
|
|
1142
|
+
try self.raw("];\n\n");
|
|
1143
|
+
for (self.sidecar.msg.arms, 0..) |arm, tag| {
|
|
1144
|
+
try self.print("const nscfTag_{s} = {d};\n", .{ arm.name, tag });
|
|
1145
|
+
}
|
|
1146
|
+
self.use(.trap);
|
|
1147
|
+
try self.raw(
|
|
1148
|
+
\\
|
|
1149
|
+
\\function nscfTagOf(kind: string): number {
|
|
1150
|
+
\\ for (let i = 0; i < nscfArmNames.length; i++) {
|
|
1151
|
+
\\ if (nscfArmNames[i] === kind) return i;
|
|
1152
|
+
\\ }
|
|
1153
|
+
\\ nscfTrap("a command routes the unknown arm " + kind + " — the author module and this generated module disagree");
|
|
1154
|
+
\\}
|
|
1155
|
+
\\
|
|
1156
|
+
);
|
|
1157
|
+
if (self.sidecar.update_returns_cmd and self.sidecar.update_returns_bare) {
|
|
1158
|
+
self.use(.cmd_encoder);
|
|
1159
|
+
// The mixed author shape (`Model | [Model, Cmd<Msg>]`, the
|
|
1160
|
+
// documented idiom): the wrapper narrows — a tuple carries
|
|
1161
|
+
// its command, a bare model the empty command buffer.
|
|
1162
|
+
try self.print(
|
|
1163
|
+
\\
|
|
1164
|
+
\\export function coreUpdate(model: {s}, msg: {s}): [{s}, Uint8Array] {{
|
|
1165
|
+
\\ const out = nscfUpdate(model, msg);
|
|
1166
|
+
\\ if (Array.isArray(out)) return [out[0], nscfCmdBytes(out[1])];
|
|
1167
|
+
\\ return [out, new Uint8Array(0)];
|
|
1168
|
+
\\}}
|
|
1169
|
+
\\
|
|
1170
|
+
, .{ self.sidecar.model, self.sidecar.msg.name, self.sidecar.model });
|
|
1171
|
+
} else if (self.sidecar.update_returns_cmd) {
|
|
1172
|
+
self.use(.cmd_encoder);
|
|
1173
|
+
try self.print(
|
|
1174
|
+
\\
|
|
1175
|
+
\\export function coreUpdate(model: {s}, msg: {s}): [{s}, Uint8Array] {{
|
|
1176
|
+
\\ const pair = nscfUpdate(model, msg);
|
|
1177
|
+
\\ return [pair[0], nscfCmdBytes(pair[1])];
|
|
1178
|
+
\\}}
|
|
1179
|
+
\\
|
|
1180
|
+
, .{ self.sidecar.model, self.sidecar.msg.name, self.sidecar.model });
|
|
1181
|
+
} else {
|
|
1182
|
+
try self.print(
|
|
1183
|
+
\\
|
|
1184
|
+
\\export function coreUpdate(model: {s}, msg: {s}): {s} {{
|
|
1185
|
+
\\ return nscfUpdate(model, msg);
|
|
1186
|
+
\\}}
|
|
1187
|
+
\\
|
|
1188
|
+
, .{ self.sidecar.model, self.sidecar.msg.name, self.sidecar.model });
|
|
1189
|
+
}
|
|
1190
|
+
if (self.sidecar.has_subscriptions) {
|
|
1191
|
+
self.use(.sub_encoder);
|
|
1192
|
+
try self.print(
|
|
1193
|
+
\\
|
|
1194
|
+
\\export function coreSubscriptions(model: {s}): Uint8Array {{
|
|
1195
|
+
\\ return nscfSubBytes(nscfSubscriptions(model));
|
|
1196
|
+
\\}}
|
|
1197
|
+
\\
|
|
1198
|
+
, .{self.sidecar.model});
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
// ------------------------------------------------ dispatch surface
|
|
1203
|
+
|
|
1204
|
+
fn dispatchSurface(self: *FacadeEmitter) Error!void {
|
|
1205
|
+
self.use(.trap);
|
|
1206
|
+
try self.raw(
|
|
1207
|
+
\\
|
|
1208
|
+
\\// ------------------------------------------------ the dispatch surface
|
|
1209
|
+
\\// One committed model in module state; every dispatch entry runs one
|
|
1210
|
+
\\// update+commit and returns the cycle's command bytes.
|
|
1211
|
+
\\
|
|
1212
|
+
\\
|
|
1213
|
+
);
|
|
1214
|
+
if (self.sidecar.init_returns_cmd) {
|
|
1215
|
+
// Boot through the normalized init wrapper: the boot pair's
|
|
1216
|
+
// command already rides as bytes.
|
|
1217
|
+
try self.print("const nscfBootPair = init();\nlet nscfCommitted: {s} = nscfBootPair[0];\n", .{self.sidecar.model});
|
|
1218
|
+
} else {
|
|
1219
|
+
try self.print("let nscfCommitted: {s} = nscfInitialModel();\n", .{self.sidecar.model});
|
|
1220
|
+
}
|
|
1221
|
+
if (self.sidecar.update_returns_cmd) {
|
|
1222
|
+
try self.print(
|
|
1223
|
+
\\
|
|
1224
|
+
\\function nscfCommit(out: [{s}, Uint8Array]): Uint8Array {{
|
|
1225
|
+
\\ nscfCommitted = out[0];
|
|
1226
|
+
\\ return out[1];
|
|
1227
|
+
\\}}
|
|
1228
|
+
\\
|
|
1229
|
+
, .{self.sidecar.model});
|
|
1230
|
+
} else {
|
|
1231
|
+
try self.print(
|
|
1232
|
+
\\
|
|
1233
|
+
\\function nscfCommit(out: {s}): Uint8Array {{
|
|
1234
|
+
\\ nscfCommitted = out;
|
|
1235
|
+
\\ return new Uint8Array(0);
|
|
1236
|
+
\\}}
|
|
1237
|
+
\\
|
|
1238
|
+
, .{self.sidecar.model});
|
|
1239
|
+
}
|
|
1240
|
+
try self.raw(
|
|
1241
|
+
\\
|
|
1242
|
+
\\function nscfUnknownTag(entry: string, tag: number): never {
|
|
1243
|
+
\\ nscfTrap("tag " + tag + " does not name a " + entry + " message arm of this core — the host and this core disagree about the contract");
|
|
1244
|
+
\\}
|
|
1245
|
+
\\
|
|
1246
|
+
);
|
|
1247
|
+
if (self.sidecar.init_returns_cmd) {
|
|
1248
|
+
try self.raw(
|
|
1249
|
+
\\
|
|
1250
|
+
\\export function boot_cmd(): Uint8Array {
|
|
1251
|
+
\\ return nscfBootPair[1];
|
|
1252
|
+
\\}
|
|
1253
|
+
\\
|
|
1254
|
+
);
|
|
1255
|
+
} else {
|
|
1256
|
+
try self.raw(
|
|
1257
|
+
\\
|
|
1258
|
+
\\export function boot_cmd(): Uint8Array {
|
|
1259
|
+
\\ // init_returns_cmd is false for this contract: no boot command.
|
|
1260
|
+
\\ return new Uint8Array(0);
|
|
1261
|
+
\\}
|
|
1262
|
+
\\
|
|
1263
|
+
);
|
|
1264
|
+
}
|
|
1265
|
+
try self.dispatchVoid();
|
|
1266
|
+
try self.dispatchBytes();
|
|
1267
|
+
try self.dispatchNumber();
|
|
1268
|
+
try self.dispatchNumberBytes();
|
|
1269
|
+
try self.dispatchBool();
|
|
1270
|
+
try self.dispatchEnum();
|
|
1271
|
+
try self.dispatchRecord();
|
|
1272
|
+
try self.dispatchTextInput();
|
|
1273
|
+
try self.dispatchScrollState();
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
/// The dispatch expression committing one update cycle for a
|
|
1277
|
+
/// constructed arm object.
|
|
1278
|
+
fn commitLine(self: *FacadeEmitter, arm_object: []const u8) Error![]const u8 {
|
|
1279
|
+
return std.fmt.allocPrint(self.arena, "return nscfCommit(coreUpdate(nscfCommitted, {s}));", .{arm_object});
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
fn dispatchVoid(self: *FacadeEmitter) Error!void {
|
|
1283
|
+
try self.raw("\nexport function dispatch_void(tag: number): Uint8Array {\n");
|
|
1284
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1285
|
+
if (arm.payload != .void) continue;
|
|
1286
|
+
try self.print(" if (tag === nscfTag_{s}) {s}\n", .{ arm.name, try self.commitLine(try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\" }}", .{try tsString(self.arena, arm.name)})) });
|
|
1287
|
+
}
|
|
1288
|
+
try self.raw(" nscfUnknownTag(\"bare\", tag);\n}\n");
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
fn dispatchBytes(self: *FacadeEmitter) Error!void {
|
|
1292
|
+
try self.raw("\nexport function dispatch_bytes(tag: number, payload: Uint8Array): Uint8Array {\n");
|
|
1293
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1294
|
+
const carries_bytes = switch (arm.payload) {
|
|
1295
|
+
.bytes => true,
|
|
1296
|
+
.scalar => |ref| ref == .bytes,
|
|
1297
|
+
else => false,
|
|
1298
|
+
};
|
|
1299
|
+
if (!carries_bytes) continue;
|
|
1300
|
+
const object = try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\", {s}: payload }}", .{ try tsString(self.arena, arm.name), try tsProp(self.arena, self.memberOf(arm.member)) });
|
|
1301
|
+
try self.print(" if (tag === nscfTag_{s}) {s}\n", .{ arm.name, try self.commitLine(object) });
|
|
1302
|
+
}
|
|
1303
|
+
try self.raw(" nscfUnknownTag(\"bytes\", tag);\n}\n");
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
fn dispatchNumber(self: *FacadeEmitter) Error!void {
|
|
1307
|
+
try self.raw("\nexport function dispatch_number(tag: number, value: number): Uint8Array {\n");
|
|
1308
|
+
var f64_count: usize = 0;
|
|
1309
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1310
|
+
if (payloadNumberClass(arm.payload)) |class| {
|
|
1311
|
+
if (class == .f64) f64_count += 1;
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
if (f64_count > 0) {
|
|
1315
|
+
try self.raw(" // These arms remain f64-classed in the contract: preserve the value\n // exactly instead of routing it through the integer proof below.\n");
|
|
1316
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1317
|
+
if (payloadNumberClass(arm.payload) != .f64) continue;
|
|
1318
|
+
const object = try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\", {s}: value }}", .{ try tsString(self.arena, arm.name), try tsProp(self.arena, self.memberOf(arm.member)) });
|
|
1319
|
+
try self.print(" if (tag === nscfTag_{s}) {s}\n", .{ arm.name, try self.commitLine(object) });
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1323
|
+
if (payloadNumberClass(arm.payload) != .i64) continue;
|
|
1324
|
+
const class = self.slotClass(self.sidecar.msg.name, arm.name) orelse .i64;
|
|
1325
|
+
const object = try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\", {s}: whole }}", .{ try tsString(self.arena, arm.name), try tsProp(self.arena, self.memberOf(arm.member)) });
|
|
1326
|
+
try self.print(
|
|
1327
|
+
\\ // This integer-classed arm proves in place: its attestation
|
|
1328
|
+
\\ // selects the signed or unsigned lower bound, the ordered
|
|
1329
|
+
\\ // comparisons exclude NaN, and Math.trunc states wholeness.
|
|
1330
|
+
\\ if (tag === nscfTag_{s}) {{
|
|
1331
|
+
\\ if (value >= {s} && value <= {s}) {{
|
|
1332
|
+
\\ const whole = Math.trunc(value);
|
|
1333
|
+
\\ return nscfCommit(coreUpdate(nscfCommitted, {s}));
|
|
1334
|
+
\\ }}
|
|
1335
|
+
\\ nscfTrap("a numeric dispatch value is NaN or outside its attested exact-integer range — the integer slot has no honest value for it");
|
|
1336
|
+
\\ }}
|
|
1337
|
+
\\
|
|
1338
|
+
, .{ arm.name, integerLowerBound(class), max_safe, object });
|
|
1339
|
+
}
|
|
1340
|
+
try self.raw(" nscfUnknownTag(\"number\", tag);\n}\n");
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
fn dispatchNumberBytes(self: *FacadeEmitter) Error!void {
|
|
1344
|
+
try self.raw("\nexport function dispatch_number_bytes(tag: number, value: number, payload: Uint8Array): Uint8Array {\n");
|
|
1345
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1346
|
+
switch (arm.payload) {
|
|
1347
|
+
.number_bytes => |desc| {
|
|
1348
|
+
const kind = try tsString(self.arena, arm.name);
|
|
1349
|
+
const number_prop = try tsProp(self.arena, desc.number_field);
|
|
1350
|
+
const bytes_prop = try tsProp(self.arena, desc.bytes_field);
|
|
1351
|
+
if (desc.number_class == .i64) {
|
|
1352
|
+
const class = self.nestedSlotClass(self.sidecar.msg.name, arm.name, desc.number_field) orelse .i64;
|
|
1353
|
+
try self.print(
|
|
1354
|
+
\\ if (tag === nscfTag_{s}) {{
|
|
1355
|
+
\\ // The number field is i64-classed: range-guard the raw f64 (an
|
|
1356
|
+
\\ // ordered comparison excludes NaN) and state wholeness with
|
|
1357
|
+
\\ // Math.trunc at the write.
|
|
1358
|
+
\\ if (value >= {s} && value <= {s}) {{
|
|
1359
|
+
\\ return nscfCommit(coreUpdate(nscfCommitted, {{ kind: "{s}", {s}: Math.trunc(value), {s}: payload }}));
|
|
1360
|
+
\\ }}
|
|
1361
|
+
\\ nscfTrap("a numeric dispatch value is NaN or outside its attested exact-integer range — the integer slot has no honest value for it");
|
|
1362
|
+
\\ }}
|
|
1363
|
+
\\
|
|
1364
|
+
, .{ arm.name, integerLowerBound(class), max_safe, kind, number_prop, bytes_prop });
|
|
1365
|
+
} else {
|
|
1366
|
+
try self.print(" if (tag === nscfTag_{s}) return nscfCommit(coreUpdate(nscfCommitted, {{ kind: \"{s}\", {s}: value, {s}: payload }}));\n", .{ arm.name, kind, number_prop, bytes_prop });
|
|
1367
|
+
}
|
|
1368
|
+
},
|
|
1369
|
+
else => {},
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
try self.raw(" nscfUnknownTag(\"number-with-bytes\", tag);\n}\n");
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
fn dispatchBool(self: *FacadeEmitter) Error!void {
|
|
1376
|
+
try self.raw("\nexport function dispatch_bool(tag: number, value: number): Uint8Array {\n");
|
|
1377
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1378
|
+
switch (arm.payload) {
|
|
1379
|
+
.scalar => |ref| {
|
|
1380
|
+
if (ref != .bool) continue;
|
|
1381
|
+
const object = try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\", {s}: value !== 0 }}", .{ try tsString(self.arena, arm.name), try tsProp(self.arena, self.memberOf(arm.member)) });
|
|
1382
|
+
try self.print(" if (tag === nscfTag_{s}) {s}\n", .{ arm.name, try self.commitLine(object) });
|
|
1383
|
+
},
|
|
1384
|
+
else => {},
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
try self.raw(" nscfUnknownTag(\"boolean\", tag);\n}\n");
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
fn dispatchEnum(self: *FacadeEmitter) Error!void {
|
|
1391
|
+
try self.raw("\nexport function dispatch_enum(tag: number, member: number): Uint8Array {\n");
|
|
1392
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1393
|
+
switch (arm.payload) {
|
|
1394
|
+
.enum_ref => |name| {
|
|
1395
|
+
try self.needEnumTable(name);
|
|
1396
|
+
const object = try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\", {s}: nscfMembers{s}[member]! }}", .{ try tsString(self.arena, arm.name), try tsProp(self.arena, self.memberOf(arm.member)), name });
|
|
1397
|
+
try self.print(
|
|
1398
|
+
\\ if (tag === nscfTag_{s}) {{
|
|
1399
|
+
\\ if (member >= nscfMembers{s}.length) nscfMember("{s}", member);
|
|
1400
|
+
\\ {s}
|
|
1401
|
+
\\ }}
|
|
1402
|
+
\\
|
|
1403
|
+
, .{ arm.name, name, try tsString(self.arena, name), try self.commitLine(object) });
|
|
1404
|
+
self.needs_member_trap = true;
|
|
1405
|
+
},
|
|
1406
|
+
else => {},
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
try self.raw(" nscfUnknownTag(\"enum\", tag);\n}\n");
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
fn dispatchRecord(self: *FacadeEmitter) Error!void {
|
|
1413
|
+
try self.raw("\nexport function dispatch_record(tag: number, fields: Uint8Array): Uint8Array {\n");
|
|
1414
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1415
|
+
switch (arm.payload) {
|
|
1416
|
+
.record => |name| {
|
|
1417
|
+
const record = sidecar_mod.findStruct(self.sidecar.types, name) orelse continue;
|
|
1418
|
+
try self.print(" if (tag === nscfTag_{s}) {{\n", .{arm.name});
|
|
1419
|
+
if (self.synthesizedRecordOf(recordPayloadRef(arm.payload), self.sidecar.msg.name, arm.name) != null) {
|
|
1420
|
+
try self.recordDecodeCommit(record, arm.name, null);
|
|
1421
|
+
} else {
|
|
1422
|
+
try self.recordDecodeCommit(record, arm.name, self.memberOf(arm.member));
|
|
1423
|
+
}
|
|
1424
|
+
try self.raw(" }\n");
|
|
1425
|
+
},
|
|
1426
|
+
.union_ref => |name| {
|
|
1427
|
+
// The emitted contract may store this union's record
|
|
1428
|
+
// payloads by reference, in which case the mirror
|
|
1429
|
+
// routes the arm through the generic record entry;
|
|
1430
|
+
// the canonical union encoding is the same either
|
|
1431
|
+
// way.
|
|
1432
|
+
try self.needUnionDecoder(name);
|
|
1433
|
+
try self.print(" if (tag === nscfTag_{s}) return nscfCommit(coreUpdate(nscfCommitted, {{ kind: \"{s}\", {s}: nscfDecode{s}(fields) }}));\n", .{ arm.name, try tsString(self.arena, arm.name), try tsProp(self.arena, self.memberOf(arm.member)), name });
|
|
1434
|
+
},
|
|
1435
|
+
else => {},
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
try self.raw(" nscfUnknownTag(\"record\", tag);\n}\n");
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
fn dispatchTextInput(self: *FacadeEmitter) Error!void {
|
|
1442
|
+
try self.raw("\nexport function dispatch_text_input(tag: number, event: Uint8Array): Uint8Array {\n");
|
|
1443
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1444
|
+
switch (arm.payload) {
|
|
1445
|
+
.union_ref => |name| {
|
|
1446
|
+
try self.needUnionDecoder(name);
|
|
1447
|
+
try self.print(" if (tag === nscfTag_{s}) return nscfCommit(coreUpdate(nscfCommitted, {{ kind: \"{s}\", {s}: nscfDecode{s}(event) }}));\n", .{ arm.name, try tsString(self.arena, arm.name), try tsProp(self.arena, self.memberOf(arm.member)), name });
|
|
1448
|
+
},
|
|
1449
|
+
else => {},
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
try self.raw(" nscfUnknownTag(\"text-input\", tag);\n}\n");
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
fn dispatchScrollState(self: *FacadeEmitter) Error!void {
|
|
1456
|
+
try self.raw(
|
|
1457
|
+
\\
|
|
1458
|
+
\\export function dispatch_scroll_state(
|
|
1459
|
+
\\ tag: number,
|
|
1460
|
+
\\ offsetX: number,
|
|
1461
|
+
\\ offsetY: number,
|
|
1462
|
+
\\ velocityX: number,
|
|
1463
|
+
\\ velocityY: number,
|
|
1464
|
+
\\ viewportExtentX: number,
|
|
1465
|
+
\\ viewportExtentY: number,
|
|
1466
|
+
\\ contentExtentX: number,
|
|
1467
|
+
\\ contentExtentY: number,
|
|
1468
|
+
\\): Uint8Array {
|
|
1469
|
+
\\
|
|
1470
|
+
);
|
|
1471
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1472
|
+
switch (arm.payload) {
|
|
1473
|
+
.record => |name| {
|
|
1474
|
+
const record = self.scrollShapedRecord(name) orelse continue;
|
|
1475
|
+
// Construct the scroll record with the author's own
|
|
1476
|
+
// field spellings, each fed by its axis parameter. An
|
|
1477
|
+
// integer-attested axis proves against its own signed or
|
|
1478
|
+
// unsigned range before Math.trunc states wholeness.
|
|
1479
|
+
var fields_text: std.ArrayListUnmanaged(u8) = .empty;
|
|
1480
|
+
var guards: std.ArrayListUnmanaged(u8) = .empty;
|
|
1481
|
+
for (record.fields, 0..) |field, index| {
|
|
1482
|
+
const param = scrollParamFor(field.name) orelse field.name;
|
|
1483
|
+
const value = if (field.type == .i64) blk: {
|
|
1484
|
+
const class = self.slotClass(record.name, field.name) orelse .i64;
|
|
1485
|
+
if (guards.items.len > 0) try guards.appendSlice(self.arena, " && ");
|
|
1486
|
+
try guards.appendSlice(self.arena, try std.fmt.allocPrint(self.arena, "{s} >= {s} && {s} <= {s}", .{ param, integerLowerBound(class), param, max_safe }));
|
|
1487
|
+
break :blk try std.fmt.allocPrint(self.arena, "Math.trunc({s})", .{param});
|
|
1488
|
+
} else param;
|
|
1489
|
+
if (index > 0) try fields_text.appendSlice(self.arena, ", ");
|
|
1490
|
+
try fields_text.appendSlice(self.arena, try std.fmt.allocPrint(self.arena, "{s}: {s}", .{ try tsProp(self.arena, field.name), value }));
|
|
1491
|
+
}
|
|
1492
|
+
const object = if (self.synthesizedRecordOf(recordPayloadRef(arm.payload), self.sidecar.msg.name, arm.name) != null)
|
|
1493
|
+
// Inline message-arm records have no authored payload
|
|
1494
|
+
// member: their fields sit beside `kind`, exactly as
|
|
1495
|
+
// the generic record dispatch constructs them.
|
|
1496
|
+
try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\", {s} }}", .{ try tsString(self.arena, arm.name), fields_text.items })
|
|
1497
|
+
else
|
|
1498
|
+
try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\", {s}: {{ {s} }} }}", .{ try tsString(self.arena, arm.name), try tsProp(self.arena, self.memberOf(arm.member)), fields_text.items });
|
|
1499
|
+
if (guards.items.len == 0) {
|
|
1500
|
+
try self.print(" if (tag === nscfTag_{s}) {s}\n", .{ arm.name, try self.commitLine(object) });
|
|
1501
|
+
} else {
|
|
1502
|
+
try self.print(
|
|
1503
|
+
\\ if (tag === nscfTag_{s}) {{
|
|
1504
|
+
\\ if ({s}) return nscfCommit(coreUpdate(nscfCommitted, {s}));
|
|
1505
|
+
\\ nscfTrap("a scroll dispatch value is NaN or outside its attested exact-integer range — the integer slot has no honest value for it");
|
|
1506
|
+
\\ }}
|
|
1507
|
+
\\
|
|
1508
|
+
, .{ arm.name, guards.items, object });
|
|
1509
|
+
self.use(.trap);
|
|
1510
|
+
}
|
|
1511
|
+
},
|
|
1512
|
+
else => {},
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
try self.raw(" nscfUnknownTag(\"scroll-state\", tag);\n}\n");
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
/// The record behind a scroll-shaped arm payload: eight numeric
|
|
1519
|
+
/// fields spelling the two-axis vocabulary (the mirror's routing
|
|
1520
|
+
/// rule restated over sidecar data).
|
|
1521
|
+
fn scrollShapedRecord(self: *FacadeEmitter, name: []const u8) ?*const sidecar_mod.Struct {
|
|
1522
|
+
const entry = sidecar_mod.findStruct(self.sidecar.types, name) orelse return null;
|
|
1523
|
+
if (entry.fields.len != 8) return null;
|
|
1524
|
+
const spellings = [_][8][]const u8{ scroll_state_fields_ts, scroll_state_fields_canvas };
|
|
1525
|
+
for (spellings) |names| {
|
|
1526
|
+
var all_found = true;
|
|
1527
|
+
for (names) |expected| {
|
|
1528
|
+
for (entry.fields) |field| {
|
|
1529
|
+
const numeric = field.type == .f64 or field.type == .i64;
|
|
1530
|
+
if (std.mem.eql(u8, field.name, expected) and numeric) break;
|
|
1531
|
+
} else {
|
|
1532
|
+
all_found = false;
|
|
1533
|
+
break;
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
if (all_found) return entry;
|
|
1537
|
+
}
|
|
1538
|
+
return null;
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
// ------------------------------------------------- channel entries
|
|
1542
|
+
|
|
1543
|
+
fn channelEntries(self: *FacadeEmitter) Error!void {
|
|
1544
|
+
const chan = self.sidecar.channels;
|
|
1545
|
+
if (!(chan.command_msg or chan.frame_msg or chan.key_msg or chan.pinch_msg)) return;
|
|
1546
|
+
self.use(.sink);
|
|
1547
|
+
self.use(.trap);
|
|
1548
|
+
try self.print(
|
|
1549
|
+
\\
|
|
1550
|
+
\\// --------------------------------------------- the ABI channel entries
|
|
1551
|
+
\\// Each answers the channel bytes envelope: [produced u8][tag u8]
|
|
1552
|
+
\\// [payload in the arm's canonical value encoding]. Byte 0 is 0
|
|
1553
|
+
\\// (nothing produced; the envelope is exactly two bytes) or 1.
|
|
1554
|
+
\\
|
|
1555
|
+
\\function nscfPackMsg(produced: {s} | null): Uint8Array {{
|
|
1556
|
+
\\ if (produced === null) return new Uint8Array(2);
|
|
1557
|
+
\\ const sink = nscfNewSink();
|
|
1558
|
+
\\ nscfMsgPayload(sink, produced);
|
|
1559
|
+
\\ const payload = nscfFinish(sink);
|
|
1560
|
+
\\ const out = new Uint8Array(2 + payload.length);
|
|
1561
|
+
\\ out[0] = 1;
|
|
1562
|
+
\\ out[1] = nscfTagOf(produced.kind);
|
|
1563
|
+
\\ for (let i = 0; i < payload.length; i++) out[2 + i] = payload[i]!;
|
|
1564
|
+
\\ return out;
|
|
1565
|
+
\\}}
|
|
1566
|
+
\\
|
|
1567
|
+
, .{self.sidecar.msg.name});
|
|
1568
|
+
try self.msgPayloadWriter();
|
|
1569
|
+
if (chan.key_msg or chan.command_msg) {
|
|
1570
|
+
self.use(.ascii_string);
|
|
1571
|
+
}
|
|
1572
|
+
if (chan.command_msg) {
|
|
1573
|
+
try self.raw(
|
|
1574
|
+
\\
|
|
1575
|
+
\\export function abi_command_msg(name: Uint8Array): Uint8Array {
|
|
1576
|
+
\\ return nscfPackMsg(nscfChanCommandMsg(nscfAsciiString(name)));
|
|
1577
|
+
\\}
|
|
1578
|
+
\\
|
|
1579
|
+
);
|
|
1580
|
+
}
|
|
1581
|
+
if (chan.frame_msg) {
|
|
1582
|
+
try self.raw(
|
|
1583
|
+
\\
|
|
1584
|
+
\\export function abi_frame_msg(width: number, height: number, timestampMs: number, intervalMs: number): Uint8Array {
|
|
1585
|
+
\\ return nscfPackMsg(nscfChanFrameMsg(nscfCommitted, {
|
|
1586
|
+
\\ width: width,
|
|
1587
|
+
\\ height: height,
|
|
1588
|
+
\\ timestampMs: timestampMs,
|
|
1589
|
+
\\ intervalMs: intervalMs,
|
|
1590
|
+
\\ }));
|
|
1591
|
+
\\}
|
|
1592
|
+
\\
|
|
1593
|
+
);
|
|
1594
|
+
}
|
|
1595
|
+
if (chan.key_msg) {
|
|
1596
|
+
try self.raw(
|
|
1597
|
+
\\
|
|
1598
|
+
\\export function abi_key_msg(
|
|
1599
|
+
\\ key: Uint8Array,
|
|
1600
|
+
\\ shift: number,
|
|
1601
|
+
\\ control: number,
|
|
1602
|
+
\\ alt: number,
|
|
1603
|
+
\\ superMod: number,
|
|
1604
|
+
\\): Uint8Array {
|
|
1605
|
+
\\ return nscfPackMsg(nscfChanKeyMsg({
|
|
1606
|
+
\\ key: nscfAsciiString(key),
|
|
1607
|
+
\\ shift: shift !== 0,
|
|
1608
|
+
\\ control: control !== 0,
|
|
1609
|
+
\\ alt: alt !== 0,
|
|
1610
|
+
\\ super: superMod !== 0,
|
|
1611
|
+
\\ }));
|
|
1612
|
+
\\}
|
|
1613
|
+
\\
|
|
1614
|
+
);
|
|
1615
|
+
}
|
|
1616
|
+
if (chan.pinch_msg) {
|
|
1617
|
+
self.needs_pinch_phase = true;
|
|
1618
|
+
self.use(.ascii_string);
|
|
1619
|
+
try self.raw(
|
|
1620
|
+
\\
|
|
1621
|
+
\\// The pinch phase table the channel's u32 member index reads back
|
|
1622
|
+
\\// through (the SDK's declared PinchPhase members, in order).
|
|
1623
|
+
\\const nscfPinchPhases: PinchPhase[] = ["begin", "change", "end"];
|
|
1624
|
+
\\
|
|
1625
|
+
\\export function abi_pinch_msg(
|
|
1626
|
+
\\ windowId: number,
|
|
1627
|
+
\\ label: Uint8Array,
|
|
1628
|
+
\\ phase: number,
|
|
1629
|
+
\\ scale: number,
|
|
1630
|
+
\\ x: number,
|
|
1631
|
+
\\ y: number,
|
|
1632
|
+
\\): Uint8Array {
|
|
1633
|
+
\\ if (phase >= nscfPinchPhases.length) nscfMember("PinchPhase", phase);
|
|
1634
|
+
\\ return nscfPackMsg(nscfChanPinchMsg({
|
|
1635
|
+
\\ windowId: windowId,
|
|
1636
|
+
\\ label: nscfAsciiString(label),
|
|
1637
|
+
\\ phase: nscfPinchPhases[phase]!,
|
|
1638
|
+
\\ scale: scale,
|
|
1639
|
+
\\ x: x,
|
|
1640
|
+
\\ y: y,
|
|
1641
|
+
\\ }));
|
|
1642
|
+
\\}
|
|
1643
|
+
\\
|
|
1644
|
+
);
|
|
1645
|
+
self.needs_member_trap = true;
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
/// The canonical payload encoding of any produced message, off the
|
|
1650
|
+
/// narrowed arm value — the envelope's tail must decode against the
|
|
1651
|
+
/// arm's mirror payload type, so field order and number classes
|
|
1652
|
+
/// follow the sidecar exactly.
|
|
1653
|
+
fn msgPayloadWriter(self: *FacadeEmitter) Error!void {
|
|
1654
|
+
try self.print("\nfunction nscfMsgPayload(sink: nscfSink, value: {s}): void {{\n", .{self.sidecar.msg.name});
|
|
1655
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
1656
|
+
try self.print(" if (value.kind === \"{s}\") {{\n", .{try tsString(self.arena, arm.name)});
|
|
1657
|
+
const member = self.memberOf(arm.member);
|
|
1658
|
+
switch (arm.payload) {
|
|
1659
|
+
.void => {},
|
|
1660
|
+
.bytes => {
|
|
1661
|
+
self.use(.w_bytes);
|
|
1662
|
+
try self.print(" nscfWBytes(sink, {s});\n", .{try tsAccess(self.arena, "value", member)});
|
|
1663
|
+
},
|
|
1664
|
+
.number => |class| {
|
|
1665
|
+
const access = try tsAccess(self.arena, "value", member);
|
|
1666
|
+
if (class == .i64) {
|
|
1667
|
+
try self.print(" {s}(sink, {s});\n", .{ self.intWriter(self.sidecar.msg.name, arm.name), access });
|
|
1668
|
+
} else {
|
|
1669
|
+
self.use(.w_f64);
|
|
1670
|
+
try self.print(" nscfWF64(sink, {s});\n", .{access});
|
|
1671
|
+
}
|
|
1672
|
+
},
|
|
1673
|
+
.number_bytes => |desc| {
|
|
1674
|
+
// The mirror declares the number field first, so the
|
|
1675
|
+
// number's bytes lead.
|
|
1676
|
+
const number_access = try tsAccess(self.arena, "value", desc.number_field);
|
|
1677
|
+
if (desc.number_class == .i64) {
|
|
1678
|
+
const slot_member = try std.fmt.allocPrint(self.arena, "{s}.{s}", .{ arm.name, desc.number_field });
|
|
1679
|
+
try self.print(" {s}(sink, {s});\n", .{ self.intWriter(self.sidecar.msg.name, slot_member), number_access });
|
|
1680
|
+
} else {
|
|
1681
|
+
self.use(.w_f64);
|
|
1682
|
+
try self.print(" nscfWF64(sink, {s});\n", .{number_access});
|
|
1683
|
+
}
|
|
1684
|
+
self.use(.w_bytes);
|
|
1685
|
+
try self.print(" nscfWBytes(sink, {s});\n", .{try tsAccess(self.arena, "value", desc.bytes_field)});
|
|
1686
|
+
},
|
|
1687
|
+
.record => |name| {
|
|
1688
|
+
const record = sidecar_mod.findStruct(self.sidecar.types, name).?;
|
|
1689
|
+
if (self.synthesizedRecordOf(recordPayloadRef(arm.payload), self.sidecar.msg.name, arm.name) != null) {
|
|
1690
|
+
// Flattened beside kind: encode the record's
|
|
1691
|
+
// fields off the narrowed arm in declaration
|
|
1692
|
+
// order.
|
|
1693
|
+
for (record.fields) |field| {
|
|
1694
|
+
try self.fieldWriteStatements(field.type, try tsAccess(self.arena, "value", field.name), name, field.name, 2);
|
|
1695
|
+
}
|
|
1696
|
+
} else {
|
|
1697
|
+
try self.needRecordWriter(name);
|
|
1698
|
+
try self.print(" nscfWrite{s}(sink, {s});\n", .{ name, try tsAccess(self.arena, "value", member) });
|
|
1699
|
+
}
|
|
1700
|
+
},
|
|
1701
|
+
.union_ref => |name| {
|
|
1702
|
+
try self.needUnionWriter(name);
|
|
1703
|
+
try self.print(" nscfWrite{s}(sink, {s});\n", .{ name, try tsAccess(self.arena, "value", member) });
|
|
1704
|
+
},
|
|
1705
|
+
.enum_ref => |name| {
|
|
1706
|
+
try self.needEnumIndex(name);
|
|
1707
|
+
self.use(.w_u32);
|
|
1708
|
+
try self.print(" nscfWU32(sink, nscfIndex{s}({s}));\n", .{ name, try tsAccess(self.arena, "value", member) });
|
|
1709
|
+
},
|
|
1710
|
+
.scalar => |ref| {
|
|
1711
|
+
const access = try tsAccess(self.arena, "value", member);
|
|
1712
|
+
switch (ref) {
|
|
1713
|
+
.bool => {
|
|
1714
|
+
self.use(.w_bool);
|
|
1715
|
+
try self.print(" nscfWBool(sink, {s});\n", .{access});
|
|
1716
|
+
},
|
|
1717
|
+
.f64 => {
|
|
1718
|
+
self.use(.w_f64);
|
|
1719
|
+
try self.print(" nscfWF64(sink, {s});\n", .{access});
|
|
1720
|
+
},
|
|
1721
|
+
.i64 => try self.print(" {s}(sink, {s});\n", .{ self.intWriter(self.sidecar.msg.name, arm.name), access }),
|
|
1722
|
+
.bytes => {
|
|
1723
|
+
self.use(.w_bytes);
|
|
1724
|
+
try self.print(" nscfWBytes(sink, {s});\n", .{access});
|
|
1725
|
+
},
|
|
1726
|
+
else => self.diags.flag("msg.arms", "arm \"{s}\" carries a scalar payload outside the generated channel encoding (only bool, number, and bytes scalars have ABI entries); extend the generator before wiring it", .{arm.name}),
|
|
1727
|
+
}
|
|
1728
|
+
},
|
|
1729
|
+
}
|
|
1730
|
+
try self.raw(" return;\n }\n");
|
|
1731
|
+
}
|
|
1732
|
+
try self.raw(" nscfTrap(\"a channel produced a message outside the declared union — the value and the contract disagree\");\n}\n");
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
// ------------------------------------------------------ post-cycle
|
|
1736
|
+
|
|
1737
|
+
fn postCycle(self: *FacadeEmitter) Error!void {
|
|
1738
|
+
try self.raw("\n// --------------------------------------------------------- post-cycle\n");
|
|
1739
|
+
if (self.sidecar.has_subscriptions) {
|
|
1740
|
+
try self.raw(
|
|
1741
|
+
\\
|
|
1742
|
+
\\export function abi_subscriptions(): Uint8Array {
|
|
1743
|
+
\\ return coreSubscriptions(nscfCommitted);
|
|
1744
|
+
\\}
|
|
1745
|
+
\\
|
|
1746
|
+
);
|
|
1747
|
+
} else {
|
|
1748
|
+
try self.raw(
|
|
1749
|
+
\\
|
|
1750
|
+
\\export function abi_subscriptions(): Uint8Array {
|
|
1751
|
+
\\ // has_subscriptions is false for this contract: always empty.
|
|
1752
|
+
\\ return new Uint8Array(0);
|
|
1753
|
+
\\}
|
|
1754
|
+
\\
|
|
1755
|
+
);
|
|
1756
|
+
}
|
|
1757
|
+
self.use(.sink);
|
|
1758
|
+
try self.needRecordWriter(self.sidecar.model);
|
|
1759
|
+
try self.print(
|
|
1760
|
+
\\
|
|
1761
|
+
\\// The canonical committed-model encoding (snapshot format {d}): the
|
|
1762
|
+
\\// model record's fields in declaration order, enums as u32 member
|
|
1763
|
+
\\// indices, optionals as a one-byte present flag plus the inner value
|
|
1764
|
+
\\// when present, records inline, slices as u32 count + elements — the
|
|
1765
|
+
\\// same bytes the transpiler lane's canonical encoder emits for this
|
|
1766
|
+
\\// model.
|
|
1767
|
+
\\export function model_snapshot(): Uint8Array {{
|
|
1768
|
+
\\ const sink = nscfNewSink();
|
|
1769
|
+
\\ nscfWrite{s}(sink, nscfCommitted);
|
|
1770
|
+
\\ return nscfFinish(sink);
|
|
1771
|
+
\\}}
|
|
1772
|
+
\\
|
|
1773
|
+
, .{ self.sidecar.abi.snapshot_format, self.sidecar.model });
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
fn helperCall(self: *FacadeEmitter) Error!void {
|
|
1777
|
+
self.use(.trap);
|
|
1778
|
+
if (self.sidecar.model_helpers.len == 0) {
|
|
1779
|
+
self.use(.assert_consumed);
|
|
1780
|
+
try self.raw(
|
|
1781
|
+
\\
|
|
1782
|
+
\\// This core declares no model helpers; the entry stays for the ABI's
|
|
1783
|
+
\\// fixed export set.
|
|
1784
|
+
\\export function helper_call(helper: number, args: Uint8Array): Uint8Array {
|
|
1785
|
+
\\ nscfAssertConsumed(args, 0);
|
|
1786
|
+
\\ nscfTrap("helper index " + helper + " does not name an exported model helper of this core — the host and this core disagree about the contract");
|
|
1787
|
+
\\}
|
|
1788
|
+
\\
|
|
1789
|
+
);
|
|
1790
|
+
return;
|
|
1791
|
+
}
|
|
1792
|
+
self.use(.assert_consumed);
|
|
1793
|
+
self.use(.sink);
|
|
1794
|
+
try self.raw(
|
|
1795
|
+
\\
|
|
1796
|
+
\\// ------------------------------------------------------- model helpers
|
|
1797
|
+
\\// Indexed by the contract's model_helpers order; results ride the
|
|
1798
|
+
\\// canonical value encoding of each helper's declared return type.
|
|
1799
|
+
\\export function helper_call(helper: number, args: Uint8Array): Uint8Array {
|
|
1800
|
+
\\ nscfAssertConsumed(args, 0);
|
|
1801
|
+
\\
|
|
1802
|
+
);
|
|
1803
|
+
for (self.sidecar.model_helpers, 0..) |helper, index| {
|
|
1804
|
+
try self.print(" if (helper === {d}) {{\n const sink = nscfNewSink();\n const nscfValue = {s}(nscfCommitted);\n", .{ index, helper.name });
|
|
1805
|
+
const slot_member = try std.fmt.allocPrint(self.arena, "{s}.return", .{helper.name});
|
|
1806
|
+
try self.fieldWriteStatements(helper.returns, "nscfValue", "helpers", slot_member, 2);
|
|
1807
|
+
try self.raw(" return nscfFinish(sink);\n }\n");
|
|
1808
|
+
}
|
|
1809
|
+
try self.raw(" nscfTrap(\"helper index \" + helper + \" does not name an exported model helper of this core — the host and this core disagree about the contract\");\n}\n");
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
// ------------------------------------- generated writers/decoders
|
|
1813
|
+
|
|
1814
|
+
fn needEnumTable(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
1815
|
+
if (nameListed(self.needed_enum_tables.items, name)) return;
|
|
1816
|
+
try self.needed_enum_tables.append(self.arena, name);
|
|
1817
|
+
try self.reference(name);
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1820
|
+
fn needEnumIndex(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
1821
|
+
try self.needEnumTable(name);
|
|
1822
|
+
if (nameListed(self.needed_enum_indexes.items, name)) return;
|
|
1823
|
+
try self.needed_enum_indexes.append(self.arena, name);
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1826
|
+
fn needRecordWriter(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
1827
|
+
if (nameListed(self.needed_record_writers.items, name)) return;
|
|
1828
|
+
try self.needed_record_writers.append(self.arena, name);
|
|
1829
|
+
try self.reference(name);
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
fn needUnionWriter(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
1833
|
+
if (nameListed(self.needed_union_writers.items, name)) return;
|
|
1834
|
+
try self.needed_union_writers.append(self.arena, name);
|
|
1835
|
+
try self.reference(name);
|
|
1836
|
+
}
|
|
1837
|
+
|
|
1838
|
+
fn needUnionDecoder(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
1839
|
+
if (nameListed(self.needed_union_decoders.items, name)) return;
|
|
1840
|
+
try self.needed_union_decoders.append(self.arena, name);
|
|
1841
|
+
try self.reference(name);
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
fn generatedTables(self: *FacadeEmitter) Error!void {
|
|
1845
|
+
var emitted_header = false;
|
|
1846
|
+
// Draining loop: a writer may need another writer or table.
|
|
1847
|
+
var enum_at: usize = 0;
|
|
1848
|
+
var enum_index_at: usize = 0;
|
|
1849
|
+
var record_at: usize = 0;
|
|
1850
|
+
var union_w_at: usize = 0;
|
|
1851
|
+
var union_d_at: usize = 0;
|
|
1852
|
+
while (enum_at < self.needed_enum_tables.items.len or
|
|
1853
|
+
enum_index_at < self.needed_enum_indexes.items.len or
|
|
1854
|
+
record_at < self.needed_record_writers.items.len or
|
|
1855
|
+
union_w_at < self.needed_union_writers.items.len or
|
|
1856
|
+
union_d_at < self.needed_union_decoders.items.len)
|
|
1857
|
+
{
|
|
1858
|
+
if (!emitted_header) {
|
|
1859
|
+
emitted_header = true;
|
|
1860
|
+
try self.raw(
|
|
1861
|
+
\\
|
|
1862
|
+
\\// ----------------------------------- generated tables and codecs
|
|
1863
|
+
\\// Enum member tables (u32 member indices read back through them),
|
|
1864
|
+
\\// canonical record/union writers, and standalone union decoders.
|
|
1865
|
+
\\
|
|
1866
|
+
);
|
|
1867
|
+
}
|
|
1868
|
+
while (enum_at < self.needed_enum_tables.items.len) : (enum_at += 1) {
|
|
1869
|
+
try self.enumTable(self.needed_enum_tables.items[enum_at]);
|
|
1870
|
+
}
|
|
1871
|
+
while (enum_index_at < self.needed_enum_indexes.items.len) : (enum_index_at += 1) {
|
|
1872
|
+
try self.enumIndexFn(self.needed_enum_indexes.items[enum_index_at]);
|
|
1873
|
+
}
|
|
1874
|
+
while (record_at < self.needed_record_writers.items.len) : (record_at += 1) {
|
|
1875
|
+
try self.recordWriter(self.needed_record_writers.items[record_at]);
|
|
1876
|
+
}
|
|
1877
|
+
while (union_w_at < self.needed_union_writers.items.len) : (union_w_at += 1) {
|
|
1878
|
+
try self.unionWriter(self.needed_union_writers.items[union_w_at]);
|
|
1879
|
+
}
|
|
1880
|
+
while (union_d_at < self.needed_union_decoders.items.len) : (union_d_at += 1) {
|
|
1881
|
+
try self.unionDecoder(self.needed_union_decoders.items[union_d_at]);
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
if (self.needs_member_trap) {
|
|
1885
|
+
self.use(.trap);
|
|
1886
|
+
try self.raw(
|
|
1887
|
+
\\
|
|
1888
|
+
\\function nscfMember(enumName: string, member: number): never {
|
|
1889
|
+
\\ nscfTrap("member index " + member + " does not name a " + enumName + " member — the host and this core disagree about the contract");
|
|
1890
|
+
\\}
|
|
1891
|
+
\\
|
|
1892
|
+
);
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
fn enumTable(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
1897
|
+
const entry = sidecar_mod.findEnum(self.sidecar.types, name).?;
|
|
1898
|
+
try self.print("\nconst nscfMembers{s}: {s}[] = [", .{ name, name });
|
|
1899
|
+
for (entry.members, 0..) |member, index| {
|
|
1900
|
+
try self.print("{s}\"{s}\"", .{ if (index == 0) "" else ", ", try tsString(self.arena, member) });
|
|
1901
|
+
}
|
|
1902
|
+
try self.raw("];\n");
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1905
|
+
fn enumIndexFn(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
1906
|
+
self.use(.trap);
|
|
1907
|
+
try self.print(
|
|
1908
|
+
\\
|
|
1909
|
+
\\function nscfIndex{s}(value: {s}): number {{
|
|
1910
|
+
\\ for (let i = 0; i < nscfMembers{s}.length; i++) {{
|
|
1911
|
+
\\ if (nscfMembers{s}[i] === value) return i;
|
|
1912
|
+
\\ }}
|
|
1913
|
+
\\ nscfTrap("an enum slot carries an undeclared member — the author module and this generated module disagree");
|
|
1914
|
+
\\}}
|
|
1915
|
+
\\
|
|
1916
|
+
, .{ name, name, name, name });
|
|
1917
|
+
}
|
|
1918
|
+
|
|
1919
|
+
/// Statements appending one value's canonical encoding to `sink`.
|
|
1920
|
+
/// `container`/`member` name the slot for the attested integer
|
|
1921
|
+
/// class; depth controls indentation and temp naming.
|
|
1922
|
+
fn fieldWriteStatements(self: *FacadeEmitter, ref: TypeRef, expr: []const u8, container: []const u8, member: []const u8, depth: usize) Error!void {
|
|
1923
|
+
const pad = try self.indentText(depth);
|
|
1924
|
+
switch (ref) {
|
|
1925
|
+
.bool => {
|
|
1926
|
+
self.use(.w_bool);
|
|
1927
|
+
try self.print("{s}nscfWBool(sink, {s});\n", .{ pad, expr });
|
|
1928
|
+
},
|
|
1929
|
+
.i64 => try self.print("{s}{s}(sink, {s});\n", .{ pad, self.intWriter(container, member), expr }),
|
|
1930
|
+
.f64 => {
|
|
1931
|
+
self.use(.w_f64);
|
|
1932
|
+
try self.print("{s}nscfWF64(sink, {s});\n", .{ pad, expr });
|
|
1933
|
+
},
|
|
1934
|
+
.bytes => {
|
|
1935
|
+
self.use(.w_bytes);
|
|
1936
|
+
try self.print("{s}nscfWBytes(sink, {s});\n", .{ pad, expr });
|
|
1937
|
+
},
|
|
1938
|
+
.void => {},
|
|
1939
|
+
.optional => |inner| {
|
|
1940
|
+
self.use(.w_bool);
|
|
1941
|
+
const temp = try std.fmt.allocPrint(self.arena, "nscfOpt{d}", .{self.temp_counter});
|
|
1942
|
+
self.temp_counter += 1;
|
|
1943
|
+
try self.print("{s}const {s} = {s};\n{s}if ({s} === null) {{\n{s} nscfWBool(sink, false);\n{s}}} else {{\n{s} nscfWBool(sink, true);\n", .{ pad, temp, expr, pad, temp, pad, pad, pad });
|
|
1944
|
+
try self.fieldWriteStatements(inner.*, temp, container, member, depth + 1);
|
|
1945
|
+
try self.print("{s}}}\n", .{pad});
|
|
1946
|
+
},
|
|
1947
|
+
.slice => |elem| {
|
|
1948
|
+
self.use(.w_u32);
|
|
1949
|
+
const index = try std.fmt.allocPrint(self.arena, "nscfIdx{d}", .{self.temp_counter});
|
|
1950
|
+
self.temp_counter += 1;
|
|
1951
|
+
try self.print("{s}nscfWU32(sink, {s}.length);\n{s}for (let {s} = 0; {s} < {s}.length; {s}++) {{\n", .{ pad, expr, pad, index, index, expr, index });
|
|
1952
|
+
try self.fieldWriteStatements(elem.*, try std.fmt.allocPrint(self.arena, "{s}[{s}]!", .{ expr, index }), container, member, depth + 1);
|
|
1953
|
+
try self.print("{s}}}\n", .{pad});
|
|
1954
|
+
},
|
|
1955
|
+
.node, .value => |name| {
|
|
1956
|
+
if (nameListed(self.flattened, name)) {
|
|
1957
|
+
// A synthesized single-use record has no importable
|
|
1958
|
+
// declaration: inline its fields at the site.
|
|
1959
|
+
const record = sidecar_mod.findStruct(self.sidecar.types, name).?;
|
|
1960
|
+
for (record.fields) |field| {
|
|
1961
|
+
try self.fieldWriteStatements(field.type, try std.fmt.allocPrint(self.arena, "{s}.{s}", .{ expr, field.name }), name, field.name, depth);
|
|
1962
|
+
}
|
|
1963
|
+
} else {
|
|
1964
|
+
try self.needRecordWriter(name);
|
|
1965
|
+
try self.print("{s}nscfWrite{s}(sink, {s});\n", .{ pad, name, expr });
|
|
1966
|
+
}
|
|
1967
|
+
},
|
|
1968
|
+
.enum_ref => |name| {
|
|
1969
|
+
try self.needEnumIndex(name);
|
|
1970
|
+
self.use(.w_u32);
|
|
1971
|
+
try self.print("{s}nscfWU32(sink, nscfIndex{s}({s}));\n", .{ pad, name, expr });
|
|
1972
|
+
},
|
|
1973
|
+
.union_ref => |name| {
|
|
1974
|
+
try self.needUnionWriter(name);
|
|
1975
|
+
try self.print("{s}nscfWrite{s}(sink, {s});\n", .{ pad, name, expr });
|
|
1976
|
+
},
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
|
|
1980
|
+
fn recordWriter(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
1981
|
+
const entry = sidecar_mod.findStruct(self.sidecar.types, name).?;
|
|
1982
|
+
self.use(.sink);
|
|
1983
|
+
self.temp_counter = 0;
|
|
1984
|
+
try self.print("\nfunction nscfWrite{s}(sink: nscfSink, value: {s}): void {{\n", .{ name, name });
|
|
1985
|
+
for (entry.fields) |field| {
|
|
1986
|
+
try self.fieldWriteStatements(field.type, try tsAccess(self.arena, "value", field.name), name, field.name, 1);
|
|
1987
|
+
}
|
|
1988
|
+
try self.raw("}\n");
|
|
1989
|
+
}
|
|
1990
|
+
|
|
1991
|
+
fn unionWriter(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
1992
|
+
const entry = sidecar_mod.findUnion(self.sidecar.types, name).?;
|
|
1993
|
+
self.use(.sink);
|
|
1994
|
+
self.use(.w_u8);
|
|
1995
|
+
self.use(.trap);
|
|
1996
|
+
self.temp_counter = 0;
|
|
1997
|
+
try self.print("\nfunction nscfWrite{s}(sink: nscfSink, value: {s}): void {{\n", .{ name, name });
|
|
1998
|
+
for (entry.arms, 0..) |arm, index| {
|
|
1999
|
+
try self.print(" if (value.kind === \"{s}\") {{\n nscfWU8(sink, {d});\n", .{ try tsString(self.arena, arm.name), index });
|
|
2000
|
+
if (arm.payload != .void) {
|
|
2001
|
+
if (self.synthesizedRecordOf(arm.payload, entry.name, arm.name)) |record| {
|
|
2002
|
+
for (record.fields) |field| {
|
|
2003
|
+
try self.fieldWriteStatements(field.type, try tsAccess(self.arena, "value", field.name), record.name, field.name, 2);
|
|
2004
|
+
}
|
|
2005
|
+
} else {
|
|
2006
|
+
try self.fieldWriteStatements(arm.payload, try tsAccess(self.arena, "value", self.memberOf(arm.member)), entry.name, arm.name, 2);
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
try self.raw(" return;\n }\n");
|
|
2010
|
+
}
|
|
2011
|
+
try self.print(" nscfTrap(\"{s} carries an arm outside its declared union — the value and the contract disagree\");\n}}\n", .{try tsString(self.arena, name)});
|
|
2012
|
+
}
|
|
2013
|
+
|
|
2014
|
+
// -------------------------------------------------------- decoding
|
|
2015
|
+
|
|
2016
|
+
/// Locals-and-construction decode of one record from `bytes` at a
|
|
2017
|
+
/// running offset, committed as an arm of the message union.
|
|
2018
|
+
/// `member` is null for a flattened (synthesized) record whose
|
|
2019
|
+
/// fields construct beside `kind`, and the authored member spelling
|
|
2020
|
+
/// otherwise.
|
|
2021
|
+
fn recordDecodeCommit(self: *FacadeEmitter, record: *const sidecar_mod.Struct, arm_name: []const u8, member: ?[]const u8) Error!void {
|
|
2022
|
+
var decode = RecordDecode{ .emitter = self, .buf = "fields", .indent = 2 };
|
|
2023
|
+
try decode.run(record);
|
|
2024
|
+
self.use(.assert_consumed);
|
|
2025
|
+
try self.print(" nscfAssertConsumed(fields, {s});\n", .{decode.offsetText()});
|
|
2026
|
+
const construction = try decode.constructionText(record);
|
|
2027
|
+
const object = if (member) |m|
|
|
2028
|
+
try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\", {s}: {s} }}", .{ try tsString(self.arena, arm_name), try tsProp(self.arena, m), construction })
|
|
2029
|
+
else blk: {
|
|
2030
|
+
// Flattened: splice the record's fields beside kind.
|
|
2031
|
+
const inner = construction[1 .. construction.len - 1];
|
|
2032
|
+
break :blk try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\",{s}}}", .{ try tsString(self.arena, arm_name), inner });
|
|
2033
|
+
};
|
|
2034
|
+
if (decode.guards.items.len > 0) {
|
|
2035
|
+
try self.print(" if ({s}) {{\n return nscfCommit(coreUpdate(nscfCommitted, {s}));\n }}\n nscfTrap(\"a decoded integer value is NaN or outside its attested exact-integer range — the integer slot has no honest value for it\");\n", .{ decode.guards.items, object });
|
|
2036
|
+
self.use(.trap);
|
|
2037
|
+
} else {
|
|
2038
|
+
try self.print(" return nscfCommit(coreUpdate(nscfCommitted, {s}));\n", .{object});
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
/// A standalone decoder for a union payload arriving whole on a
|
|
2043
|
+
/// dispatch entry (`[arm u8][payload]`, fully consumed).
|
|
2044
|
+
fn unionDecoder(self: *FacadeEmitter, name: []const u8) Error!void {
|
|
2045
|
+
const entry = sidecar_mod.findUnion(self.sidecar.types, name).?;
|
|
2046
|
+
self.use(.trap);
|
|
2047
|
+
self.use(.assert_consumed);
|
|
2048
|
+
try self.print(
|
|
2049
|
+
\\
|
|
2050
|
+
\\function nscfDecode{s}(bytes: Uint8Array): {s} {{
|
|
2051
|
+
\\ if (bytes.length === 0) {{
|
|
2052
|
+
\\ nscfTrap("a dispatched union payload carries no arm byte — the host and this core disagree about the contract");
|
|
2053
|
+
\\ }}
|
|
2054
|
+
\\ const arm = bytes[0]!;
|
|
2055
|
+
\\
|
|
2056
|
+
, .{ name, name });
|
|
2057
|
+
for (entry.arms, 0..) |arm, index| {
|
|
2058
|
+
const kind = try tsString(self.arena, arm.name);
|
|
2059
|
+
try self.print(" if (arm === {d}) {{\n", .{index});
|
|
2060
|
+
if (arm.payload == .void) {
|
|
2061
|
+
try self.print(" nscfAssertConsumed(bytes, 1);\n return {{ kind: \"{s}\" }};\n", .{kind});
|
|
2062
|
+
} else {
|
|
2063
|
+
var decode = RecordDecode{
|
|
2064
|
+
.emitter = self,
|
|
2065
|
+
.buf = "bytes",
|
|
2066
|
+
.indent = 2,
|
|
2067
|
+
.start = "1",
|
|
2068
|
+
// Selection is a role of the COMPLETE text-input
|
|
2069
|
+
// protocol, not an arm-name convention. Ordinary unions
|
|
2070
|
+
// with a homonymous arm retain the normal exact-integer
|
|
2071
|
+
// refusal instead of silently clamping their payload.
|
|
2072
|
+
.saturating_selection = emit_mod.isTextInputUnion(self.sidecar, name) and std.mem.eql(u8, arm.name, "set_selection"),
|
|
2073
|
+
};
|
|
2074
|
+
var construction: []const u8 = undefined;
|
|
2075
|
+
if (self.synthesizedRecordOf(arm.payload, entry.name, arm.name)) |record| {
|
|
2076
|
+
try decode.run(record);
|
|
2077
|
+
const inner = try decode.constructionText(record);
|
|
2078
|
+
construction = try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\",{s}}}", .{ kind, inner[1 .. inner.len - 1] });
|
|
2079
|
+
} else {
|
|
2080
|
+
const field = sidecar_mod.Field{ .name = self.memberOf(arm.member), .type = arm.payload };
|
|
2081
|
+
try decode.fieldLocal(field, entry.name, arm.name);
|
|
2082
|
+
construction = try std.fmt.allocPrint(self.arena, "{{ kind: \"{s}\", {s}: {s} }}", .{ kind, try tsProp(self.arena, self.memberOf(arm.member)), decode.exprs.items[0].text });
|
|
2083
|
+
}
|
|
2084
|
+
try self.print(" nscfAssertConsumed(bytes, {s});\n", .{decode.offsetText()});
|
|
2085
|
+
if (decode.guards.items.len > 0) {
|
|
2086
|
+
try self.print(" if ({s}) {{\n return {s};\n }}\n nscfTrap(\"a decoded integer value is NaN or outside its attested exact-integer range — the integer slot has no honest value for it\");\n", .{ decode.guards.items, construction });
|
|
2087
|
+
} else {
|
|
2088
|
+
try self.print(" return {s};\n", .{construction});
|
|
2089
|
+
}
|
|
2090
|
+
}
|
|
2091
|
+
try self.raw(" }\n");
|
|
2092
|
+
}
|
|
2093
|
+
try self.raw(" nscfTrap(\"a dispatched union payload carries a union arm index past the declared arms — the host and this core disagree about the contract\");\n}\n");
|
|
2094
|
+
}
|
|
2095
|
+
|
|
2096
|
+
// -------------------------------------------------- codec section
|
|
2097
|
+
|
|
2098
|
+
fn codecSection(self: *FacadeEmitter) Error!void {
|
|
2099
|
+
// Emitted in fixed dependency order; each snippet appears only
|
|
2100
|
+
// when generated code above reaches it.
|
|
2101
|
+
if (self.used_codec.count() == 0) return;
|
|
2102
|
+
try self.raw(
|
|
2103
|
+
\\
|
|
2104
|
+
\\// -------------------------------------------------- the wire codec
|
|
2105
|
+
\\// The canonical value encoding (little-endian, headerless,
|
|
2106
|
+
\\// schema-driven) plus the v3 command/subscription wire format the
|
|
2107
|
+
\\// host consumes — byte-identical to what the transpiler lane emits,
|
|
2108
|
+
\\// proven by the paired e2e batteries.
|
|
2109
|
+
\\
|
|
2110
|
+
);
|
|
2111
|
+
if (self.used_codec.contains(.trap)) {
|
|
2112
|
+
try self.raw(
|
|
2113
|
+
\\
|
|
2114
|
+
\\function nscfTrap(teaching: string): never {
|
|
2115
|
+
\\ throw new Error(teaching);
|
|
2116
|
+
\\}
|
|
2117
|
+
\\
|
|
2118
|
+
);
|
|
2119
|
+
}
|
|
2120
|
+
if (self.used_codec.contains(.read_u8)) {
|
|
2121
|
+
try self.raw(
|
|
2122
|
+
\\
|
|
2123
|
+
\\function nscfReadU8(bytes: Uint8Array, at: number): number {
|
|
2124
|
+
\\ if (at >= bytes.length) {
|
|
2125
|
+
\\ nscfTrap("a dispatch payload ended mid-value — the host and this core disagree about a type's layout");
|
|
2126
|
+
\\ }
|
|
2127
|
+
\\ return bytes[at]!;
|
|
2128
|
+
\\}
|
|
2129
|
+
\\
|
|
2130
|
+
);
|
|
2131
|
+
}
|
|
2132
|
+
if (self.used_codec.contains(.read_u32)) {
|
|
2133
|
+
try self.raw(
|
|
2134
|
+
\\
|
|
2135
|
+
\\function nscfReadU32(bytes: Uint8Array, at: number): number {
|
|
2136
|
+
\\ if (at + 4 > bytes.length) {
|
|
2137
|
+
\\ nscfTrap("a dispatch payload ended mid-value — the host and this core disagree about a type's layout");
|
|
2138
|
+
\\ }
|
|
2139
|
+
\\ return bytes[at]! + bytes[at + 1]! * 256 + bytes[at + 2]! * 65536 + bytes[at + 3]! * 16777216;
|
|
2140
|
+
\\}
|
|
2141
|
+
\\
|
|
2142
|
+
);
|
|
2143
|
+
}
|
|
2144
|
+
if (self.used_codec.contains(.read_f64)) {
|
|
2145
|
+
try self.raw(
|
|
2146
|
+
\\
|
|
2147
|
+
\\function nscfReadF64(bytes: Uint8Array, at: number): number {
|
|
2148
|
+
\\ if (at + 8 > bytes.length) {
|
|
2149
|
+
\\ nscfTrap("a dispatch payload ended mid-value — the host and this core disagree about a type's layout");
|
|
2150
|
+
\\ }
|
|
2151
|
+
\\ const buf = Buffer.alloc(8);
|
|
2152
|
+
\\ for (let i = 0; i < 8; i++) {
|
|
2153
|
+
\\ buf[i] = bytes[at + i]!;
|
|
2154
|
+
\\ }
|
|
2155
|
+
\\ return buf.readDoubleLE(0);
|
|
2156
|
+
\\}
|
|
2157
|
+
\\
|
|
2158
|
+
);
|
|
2159
|
+
}
|
|
2160
|
+
if (self.used_codec.contains(.read_i64)) {
|
|
2161
|
+
try self.raw(
|
|
2162
|
+
\\
|
|
2163
|
+
\\function nscfReadI64(bytes: Uint8Array, at: number): number {
|
|
2164
|
+
\\ const lo = nscfReadU32(bytes, at);
|
|
2165
|
+
\\ const hi = nscfReadU32(bytes, at + 4);
|
|
2166
|
+
\\ const hiSigned = hi >= 2147483648 ? hi - 4294967296 : hi;
|
|
2167
|
+
\\ const value = hiSigned * 4294967296 + lo;
|
|
2168
|
+
\\ if (value > 9007199254740991 || value < -9007199254740991) {
|
|
2169
|
+
\\ nscfTrap("an integer payload is at or past 2^53 — the f64 number model has no honest value for it");
|
|
2170
|
+
\\ }
|
|
2171
|
+
\\ return value;
|
|
2172
|
+
\\}
|
|
2173
|
+
\\
|
|
2174
|
+
);
|
|
2175
|
+
}
|
|
2176
|
+
if (self.used_codec.contains(.read_i64_saturating)) {
|
|
2177
|
+
try self.raw(
|
|
2178
|
+
\\
|
|
2179
|
+
\\// An i64 payload saturated into the f64-exact window: values at or
|
|
2180
|
+
\\// past +-2^53 clamp to +-(2^53 - 1). Selection offsets ride this
|
|
2181
|
+
\\// reader because the host's select-all synthesizes the maxInt "to
|
|
2182
|
+
\\// the end" sentinel, which every consumer snaps to the text's
|
|
2183
|
+
\\// length.
|
|
2184
|
+
\\function nscfReadI64Saturating(bytes: Uint8Array, at: number): number {
|
|
2185
|
+
\\ const lo = nscfReadU32(bytes, at);
|
|
2186
|
+
\\ const hi = nscfReadU32(bytes, at + 4);
|
|
2187
|
+
\\ const hiSigned = hi >= 2147483648 ? hi - 4294967296 : hi;
|
|
2188
|
+
\\ const value = hiSigned * 4294967296 + lo;
|
|
2189
|
+
\\ if (value > 9007199254740991) return 9007199254740991;
|
|
2190
|
+
\\ if (value < -9007199254740991) return -9007199254740991;
|
|
2191
|
+
\\ return value;
|
|
2192
|
+
\\}
|
|
2193
|
+
\\
|
|
2194
|
+
);
|
|
2195
|
+
}
|
|
2196
|
+
if (self.used_codec.contains(.read_u64_saturating)) {
|
|
2197
|
+
try self.raw(
|
|
2198
|
+
\\
|
|
2199
|
+
\\// A u64 payload saturated into the nonnegative f64-exact window.
|
|
2200
|
+
\\// Selection offsets use this twin when their sidecar slot is
|
|
2201
|
+
\\// unsigned: maxInt(usize) is all-one bits, not signed -1.
|
|
2202
|
+
\\function nscfReadU64Saturating(bytes: Uint8Array, at: number): number {
|
|
2203
|
+
\\ const lo = nscfReadU32(bytes, at);
|
|
2204
|
+
\\ const hi = nscfReadU32(bytes, at + 4);
|
|
2205
|
+
\\ if (hi > 2097151) return 9007199254740991;
|
|
2206
|
+
\\ return hi * 4294967296 + lo;
|
|
2207
|
+
\\}
|
|
2208
|
+
\\
|
|
2209
|
+
);
|
|
2210
|
+
}
|
|
2211
|
+
if (self.used_codec.contains(.read_bool)) {
|
|
2212
|
+
try self.raw(
|
|
2213
|
+
\\
|
|
2214
|
+
\\function nscfReadBool(bytes: Uint8Array, at: number): boolean {
|
|
2215
|
+
\\ if (at >= bytes.length) {
|
|
2216
|
+
\\ nscfTrap("a dispatch payload ended mid-value — the host and this core disagree about a type's layout");
|
|
2217
|
+
\\ }
|
|
2218
|
+
\\ const raw = bytes[at]!;
|
|
2219
|
+
\\ if (raw > 1) {
|
|
2220
|
+
\\ nscfTrap("a dispatch payload carries a boolean discriminant past 1 — the host and this core disagree about a type's layout");
|
|
2221
|
+
\\ }
|
|
2222
|
+
\\ return raw === 1;
|
|
2223
|
+
\\}
|
|
2224
|
+
\\
|
|
2225
|
+
);
|
|
2226
|
+
}
|
|
2227
|
+
if (self.used_codec.contains(.read_bytes_body)) {
|
|
2228
|
+
try self.raw(
|
|
2229
|
+
\\
|
|
2230
|
+
\\function nscfReadBytesBody(bytes: Uint8Array, at: number, len: number): Uint8Array {
|
|
2231
|
+
\\ if (at + len > bytes.length) {
|
|
2232
|
+
\\ nscfTrap("a dispatch payload ended mid-value — the host and this core disagree about a type's layout");
|
|
2233
|
+
\\ }
|
|
2234
|
+
\\ const out = new Uint8Array(len);
|
|
2235
|
+
\\ for (let i = 0; i < len; i++) {
|
|
2236
|
+
\\ out[i] = bytes[at + i]!;
|
|
2237
|
+
\\ }
|
|
2238
|
+
\\ return out;
|
|
2239
|
+
\\}
|
|
2240
|
+
\\
|
|
2241
|
+
);
|
|
2242
|
+
}
|
|
2243
|
+
if (self.used_codec.contains(.assert_consumed)) {
|
|
2244
|
+
try self.raw(
|
|
2245
|
+
\\
|
|
2246
|
+
\\function nscfAssertConsumed(bytes: Uint8Array, at: number): void {
|
|
2247
|
+
\\ if (at !== bytes.length) {
|
|
2248
|
+
\\ nscfTrap("a dispatch payload carries bytes past the decoded value — the host and this core disagree about a type's layout");
|
|
2249
|
+
\\ }
|
|
2250
|
+
\\}
|
|
2251
|
+
\\
|
|
2252
|
+
);
|
|
2253
|
+
}
|
|
2254
|
+
if (self.used_codec.contains(.trunc_toward_zero)) {
|
|
2255
|
+
try self.raw(
|
|
2256
|
+
\\
|
|
2257
|
+
\\// An i64-classed slot narrows core-side by truncation toward zero
|
|
2258
|
+
\\// (producers guarantee integer-classed values are exact below 2^53).
|
|
2259
|
+
\\function nscfTruncTowardZero(value: number): number {
|
|
2260
|
+
\\ return value < 0 ? -Math.floor(-value) : Math.floor(value);
|
|
2261
|
+
\\}
|
|
2262
|
+
\\
|
|
2263
|
+
);
|
|
2264
|
+
}
|
|
2265
|
+
if (self.used_codec.contains(.sink)) {
|
|
2266
|
+
try self.raw(
|
|
2267
|
+
\\
|
|
2268
|
+
\\// A sink is a plain byte accumulator; nscfFinish snapshots it.
|
|
2269
|
+
\\type nscfSink = number[];
|
|
2270
|
+
\\
|
|
2271
|
+
\\function nscfNewSink(): nscfSink {
|
|
2272
|
+
\\ return [];
|
|
2273
|
+
\\}
|
|
2274
|
+
\\
|
|
2275
|
+
\\function nscfFinish(sink: nscfSink): Uint8Array {
|
|
2276
|
+
\\ const out = new Uint8Array(sink.length);
|
|
2277
|
+
\\ for (let i = 0; i < sink.length; i++) {
|
|
2278
|
+
\\ out[i] = sink[i]!;
|
|
2279
|
+
\\ }
|
|
2280
|
+
\\ return out;
|
|
2281
|
+
\\}
|
|
2282
|
+
\\
|
|
2283
|
+
);
|
|
2284
|
+
}
|
|
2285
|
+
if (self.used_codec.contains(.w_u8)) {
|
|
2286
|
+
try self.raw(
|
|
2287
|
+
\\
|
|
2288
|
+
\\function nscfWU8(sink: nscfSink, value: number): void {
|
|
2289
|
+
\\ sink.push(value);
|
|
2290
|
+
\\}
|
|
2291
|
+
\\
|
|
2292
|
+
);
|
|
2293
|
+
}
|
|
2294
|
+
if (self.used_codec.contains(.w_u32)) {
|
|
2295
|
+
try self.raw(
|
|
2296
|
+
\\
|
|
2297
|
+
\\function nscfWU32(sink: nscfSink, value: number): void {
|
|
2298
|
+
\\ sink.push(value % 256);
|
|
2299
|
+
\\ sink.push(Math.floor(value / 256) % 256);
|
|
2300
|
+
\\ sink.push(Math.floor(value / 65536) % 256);
|
|
2301
|
+
\\ sink.push(Math.floor(value / 16777216) % 256);
|
|
2302
|
+
\\}
|
|
2303
|
+
\\
|
|
2304
|
+
);
|
|
2305
|
+
}
|
|
2306
|
+
if (self.used_codec.contains(.w_f64)) {
|
|
2307
|
+
try self.raw(
|
|
2308
|
+
\\
|
|
2309
|
+
\\function nscfWF64(sink: nscfSink, value: number): void {
|
|
2310
|
+
\\ const buf = Buffer.alloc(8);
|
|
2311
|
+
\\ buf.writeDoubleLE(value, 0);
|
|
2312
|
+
\\ for (let i = 0; i < 8; i++) {
|
|
2313
|
+
\\ sink.push(buf[i]!);
|
|
2314
|
+
\\ }
|
|
2315
|
+
\\}
|
|
2316
|
+
\\
|
|
2317
|
+
);
|
|
2318
|
+
}
|
|
2319
|
+
if (self.used_codec.contains(.w_i64)) {
|
|
2320
|
+
try self.raw(
|
|
2321
|
+
\\
|
|
2322
|
+
\\// Two's-complement i64, exact for every integer within +-(2^53 - 1).
|
|
2323
|
+
\\function nscfWI64(sink: nscfSink, value: number): void {
|
|
2324
|
+
\\ if (value > 9007199254740991 || value < -9007199254740991 || value !== nscfTruncTowardZero(value)) {
|
|
2325
|
+
\\ nscfTrap("an integer slot carries a non-integer or out-of-range value — the i64 encoding has no honest bytes for it");
|
|
2326
|
+
\\ }
|
|
2327
|
+
\\ const hi = Math.floor(value / 4294967296);
|
|
2328
|
+
\\ const lo = value - hi * 4294967296;
|
|
2329
|
+
\\ const hiWire = hi < 0 ? hi + 4294967296 : hi;
|
|
2330
|
+
\\ nscfWU32(sink, lo);
|
|
2331
|
+
\\ nscfWU32(sink, hiWire);
|
|
2332
|
+
\\}
|
|
2333
|
+
\\
|
|
2334
|
+
);
|
|
2335
|
+
}
|
|
2336
|
+
if (self.used_codec.contains(.w_u64)) {
|
|
2337
|
+
try self.raw(
|
|
2338
|
+
\\
|
|
2339
|
+
\\// The unsigned twin for u64-attested slots: 8-byte unsigned LE,
|
|
2340
|
+
\\// refusing negatives (a negative value has no honest unsigned
|
|
2341
|
+
\\// bytes).
|
|
2342
|
+
\\function nscfWU64(sink: nscfSink, value: number): void {
|
|
2343
|
+
\\ if (value > 9007199254740991 || value < 0 || value !== nscfTruncTowardZero(value)) {
|
|
2344
|
+
\\ nscfTrap("an unsigned integer slot carries a non-integer, negative, or out-of-range value — the u64 encoding has no honest bytes for it");
|
|
2345
|
+
\\ }
|
|
2346
|
+
\\ const hi = Math.floor(value / 4294967296);
|
|
2347
|
+
\\ const lo = value - hi * 4294967296;
|
|
2348
|
+
\\ nscfWU32(sink, lo);
|
|
2349
|
+
\\ nscfWU32(sink, hi);
|
|
2350
|
+
\\}
|
|
2351
|
+
\\
|
|
2352
|
+
);
|
|
2353
|
+
}
|
|
2354
|
+
if (self.used_codec.contains(.w_bool)) {
|
|
2355
|
+
try self.raw(
|
|
2356
|
+
\\
|
|
2357
|
+
\\function nscfWBool(sink: nscfSink, value: boolean): void {
|
|
2358
|
+
\\ sink.push(value ? 1 : 0);
|
|
2359
|
+
\\}
|
|
2360
|
+
\\
|
|
2361
|
+
);
|
|
2362
|
+
}
|
|
2363
|
+
if (self.used_codec.contains(.w_bytes)) {
|
|
2364
|
+
try self.raw(
|
|
2365
|
+
\\
|
|
2366
|
+
\\// A u32-length-prefixed bytes value (the canonical bytes encoding,
|
|
2367
|
+
\\// and the wire format's long-bytes field).
|
|
2368
|
+
\\function nscfWBytes(sink: nscfSink, bytes: Uint8Array): void {
|
|
2369
|
+
\\ nscfWU32(sink, bytes.length);
|
|
2370
|
+
\\ for (let i = 0; i < bytes.length; i++) {
|
|
2371
|
+
\\ sink.push(bytes[i]!);
|
|
2372
|
+
\\ }
|
|
2373
|
+
\\}
|
|
2374
|
+
\\
|
|
2375
|
+
);
|
|
2376
|
+
}
|
|
2377
|
+
if (self.used_codec.contains(.short_text)) {
|
|
2378
|
+
try self.raw(
|
|
2379
|
+
\\
|
|
2380
|
+
\\function nscfTextBytes(text: string): Uint8Array {
|
|
2381
|
+
\\ const out = new Uint8Array(text.length);
|
|
2382
|
+
\\ for (let i = 0; i < text.length; i++) {
|
|
2383
|
+
\\ out[i] = text.charCodeAt(i);
|
|
2384
|
+
\\ }
|
|
2385
|
+
\\ return out;
|
|
2386
|
+
\\}
|
|
2387
|
+
\\
|
|
2388
|
+
\\// A u8-length-prefixed short text field (names, keys, labels).
|
|
2389
|
+
\\function nscfWShortText(sink: nscfSink, text: string): void {
|
|
2390
|
+
\\ const bytes = nscfTextBytes(text);
|
|
2391
|
+
\\ if (bytes.length > 255) {
|
|
2392
|
+
\\ nscfTrap("a command name or key is over 255 bytes — the wire's short-text fields cannot carry it");
|
|
2393
|
+
\\ }
|
|
2394
|
+
\\ sink.push(bytes.length);
|
|
2395
|
+
\\ for (let i = 0; i < bytes.length; i++) {
|
|
2396
|
+
\\ sink.push(bytes[i]!);
|
|
2397
|
+
\\ }
|
|
2398
|
+
\\}
|
|
2399
|
+
\\
|
|
2400
|
+
);
|
|
2401
|
+
}
|
|
2402
|
+
if (self.used_codec.contains(.ascii_string)) {
|
|
2403
|
+
try self.raw(
|
|
2404
|
+
\\
|
|
2405
|
+
\\function nscfAsciiString(bytes: Uint8Array): string {
|
|
2406
|
+
\\ let out = "";
|
|
2407
|
+
\\ for (let i = 0; i < bytes.length; i++) out = out + String.fromCharCode(bytes[i]!);
|
|
2408
|
+
\\ return out;
|
|
2409
|
+
\\}
|
|
2410
|
+
\\
|
|
2411
|
+
);
|
|
2412
|
+
}
|
|
2413
|
+
if (self.used_codec.contains(.enum_index)) {
|
|
2414
|
+
try self.raw(
|
|
2415
|
+
\\
|
|
2416
|
+
\\function nscfMemberIndex(members: string[], value: string, what: string): number {
|
|
2417
|
+
\\ for (let i = 0; i < members.length; i++) {
|
|
2418
|
+
\\ if (members[i] === value) return i;
|
|
2419
|
+
\\ }
|
|
2420
|
+
\\ nscfTrap("a command carries an unknown " + what + " member — the SDK and this generated module disagree");
|
|
2421
|
+
\\}
|
|
2422
|
+
\\
|
|
2423
|
+
);
|
|
2424
|
+
}
|
|
2425
|
+
if (self.used_codec.contains(.cmd_encoder)) {
|
|
2426
|
+
try self.cmdEncoder();
|
|
2427
|
+
}
|
|
2428
|
+
if (self.used_codec.contains(.sub_encoder)) {
|
|
2429
|
+
try self.subEncoder();
|
|
2430
|
+
}
|
|
2431
|
+
}
|
|
2432
|
+
|
|
2433
|
+
/// The v3 command wire encoder — byte-for-byte the layouts the
|
|
2434
|
+
/// host's rt builds (wire.ts's encodeCmd, per-module).
|
|
2435
|
+
fn cmdEncoder(self: *FacadeEmitter) Error!void {
|
|
2436
|
+
const msg = self.sidecar.msg.name;
|
|
2437
|
+
try self.print(
|
|
2438
|
+
\\
|
|
2439
|
+
\\// ---------------------------------------------------- the cmd wire
|
|
2440
|
+
\\// Encoder for the inert Cmd data the author's update returns —
|
|
2441
|
+
\\// byte-for-byte the layouts the host's command decoder expects
|
|
2442
|
+
\\// (cmd_format_version 3). nscfTagOf maps a Msg arm name onto its
|
|
2443
|
+
\\// declaration-order wire tag.
|
|
2444
|
+
\\
|
|
2445
|
+
\\const nscfFetchMethods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"];
|
|
2446
|
+
\\const nscfAudioVerbs = ["pause", "resume", "stop", "seek", "volume"];
|
|
2447
|
+
\\const nscfVideoVerbs = ["play", "pause", "stop", "seek", "volume", "muted", "loop"];
|
|
2448
|
+
\\
|
|
2449
|
+
\\function nscfEncodeCmd(sink: nscfSink, cmd: nscfCmd<{s}>): void {{
|
|
2450
|
+
\\
|
|
2451
|
+
, .{msg});
|
|
2452
|
+
try self.raw(
|
|
2453
|
+
\\ switch (cmd.op) {
|
|
2454
|
+
\\ case "none":
|
|
2455
|
+
\\ return;
|
|
2456
|
+
\\ case "persist":
|
|
2457
|
+
\\ nscfWU8(sink, 0x01);
|
|
2458
|
+
\\ return;
|
|
2459
|
+
\\ case "now":
|
|
2460
|
+
\\ nscfWU8(sink, 0x02);
|
|
2461
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.msgKind));
|
|
2462
|
+
\\ return;
|
|
2463
|
+
\\ case "host": {
|
|
2464
|
+
\\ nscfWU8(sink, 0x03);
|
|
2465
|
+
\\ nscfWShortText(sink, cmd.name);
|
|
2466
|
+
\\ if (cmd.args.length > 255) {
|
|
2467
|
+
\\ nscfTrap("a host command carries over 255 scalar args — the wire's arg block cannot carry it");
|
|
2468
|
+
\\ }
|
|
2469
|
+
\\ nscfWU8(sink, cmd.args.length);
|
|
2470
|
+
\\ for (let i = 0; i < cmd.args.length; i++) {
|
|
2471
|
+
\\ nscfWF64(sink, cmd.args[i]!);
|
|
2472
|
+
\\ }
|
|
2473
|
+
\\ return;
|
|
2474
|
+
\\ }
|
|
2475
|
+
\\ case "host_bytes":
|
|
2476
|
+
\\ nscfWU8(sink, 0x04);
|
|
2477
|
+
\\ nscfWShortText(sink, cmd.name);
|
|
2478
|
+
\\ nscfWBytes(sink, cmd.payload);
|
|
2479
|
+
\\ return;
|
|
2480
|
+
\\ case "request":
|
|
2481
|
+
\\ nscfWU8(sink, 0x05);
|
|
2482
|
+
\\ nscfWShortText(sink, cmd.name);
|
|
2483
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2484
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.okKind));
|
|
2485
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.errKind));
|
|
2486
|
+
\\ nscfWBytes(sink, cmd.payload);
|
|
2487
|
+
\\ return;
|
|
2488
|
+
\\ case "cancel":
|
|
2489
|
+
\\ nscfWU8(sink, 0x06);
|
|
2490
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2491
|
+
\\ return;
|
|
2492
|
+
\\ case "read_file":
|
|
2493
|
+
\\ nscfWU8(sink, 0x07);
|
|
2494
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2495
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.okKind));
|
|
2496
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.errKind));
|
|
2497
|
+
\\ nscfWBytes(sink, cmd.path);
|
|
2498
|
+
\\ return;
|
|
2499
|
+
\\ case "write_file":
|
|
2500
|
+
\\ nscfWU8(sink, 0x08);
|
|
2501
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2502
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.okKind));
|
|
2503
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.errKind));
|
|
2504
|
+
\\ nscfWBytes(sink, cmd.path);
|
|
2505
|
+
\\ nscfWBytes(sink, cmd.bytes);
|
|
2506
|
+
\\ return;
|
|
2507
|
+
\\ case "fetch": {
|
|
2508
|
+
\\ nscfWU8(sink, 0x09);
|
|
2509
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2510
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.okKind));
|
|
2511
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.errKind));
|
|
2512
|
+
\\ nscfWU8(sink, nscfMemberIndex(nscfFetchMethods, cmd.method, "fetch method"));
|
|
2513
|
+
\\ nscfWU32(sink, cmd.timeoutMs);
|
|
2514
|
+
\\ nscfWBytes(sink, cmd.url);
|
|
2515
|
+
\\ if (cmd.headers.length > 255) {
|
|
2516
|
+
\\ nscfTrap("a fetch carries over 255 headers — the wire's header block cannot carry it");
|
|
2517
|
+
\\ }
|
|
2518
|
+
\\ nscfWU8(sink, cmd.headers.length);
|
|
2519
|
+
\\ for (let i = 0; i < cmd.headers.length; i++) {
|
|
2520
|
+
\\ const header = cmd.headers[i]!;
|
|
2521
|
+
\\ nscfWShortText(sink, header.name);
|
|
2522
|
+
\\ const value = header.value;
|
|
2523
|
+
\\ nscfWBytes(sink, typeof value === "string" ? nscfTextBytes(value) : value);
|
|
2524
|
+
\\ }
|
|
2525
|
+
\\ nscfWBytes(sink, cmd.body);
|
|
2526
|
+
\\ return;
|
|
2527
|
+
\\ }
|
|
2528
|
+
\\ case "clip_write":
|
|
2529
|
+
\\ nscfWU8(sink, 0x0a);
|
|
2530
|
+
\\ nscfWBytes(sink, cmd.bytes);
|
|
2531
|
+
\\ return;
|
|
2532
|
+
\\ case "clip_read":
|
|
2533
|
+
\\ nscfWU8(sink, 0x0b);
|
|
2534
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2535
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.okKind));
|
|
2536
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.errKind));
|
|
2537
|
+
\\ return;
|
|
2538
|
+
\\ case "delay":
|
|
2539
|
+
\\ nscfWU8(sink, 0x0c);
|
|
2540
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2541
|
+
\\ nscfWF64(sink, cmd.afterMs);
|
|
2542
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.msgKind));
|
|
2543
|
+
\\ return;
|
|
2544
|
+
\\ case "spawn": {
|
|
2545
|
+
\\ nscfWU8(sink, 0x0d);
|
|
2546
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2547
|
+
\\ nscfWU8(sink, cmd.lineKind === "" ? 0xff : nscfTagOf(cmd.lineKind));
|
|
2548
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.exitKind));
|
|
2549
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.errKind));
|
|
2550
|
+
\\ nscfWU8(sink, cmd.collect ? 1 : 0);
|
|
2551
|
+
\\ if (cmd.argv.length === 0 || cmd.argv.length > 255) {
|
|
2552
|
+
\\ nscfTrap("a spawn carries no argv or over 255 argv entries — the wire's argv block cannot carry it");
|
|
2553
|
+
\\ }
|
|
2554
|
+
\\ nscfWU8(sink, cmd.argv.length);
|
|
2555
|
+
\\ for (let i = 0; i < cmd.argv.length; i++) {
|
|
2556
|
+
\\ nscfWBytes(sink, cmd.argv[i]!);
|
|
2557
|
+
\\ }
|
|
2558
|
+
\\ nscfWBytes(sink, cmd.stdin);
|
|
2559
|
+
\\ return;
|
|
2560
|
+
\\ }
|
|
2561
|
+
\\ case "audio_play":
|
|
2562
|
+
\\ nscfWU8(sink, 0x0e);
|
|
2563
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2564
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.eventKind));
|
|
2565
|
+
\\ nscfWBytes(sink, cmd.path);
|
|
2566
|
+
\\ nscfWBytes(sink, cmd.url);
|
|
2567
|
+
\\ nscfWBytes(sink, cmd.cachePath);
|
|
2568
|
+
\\ nscfWF64(sink, cmd.expectedBytes);
|
|
2569
|
+
\\ return;
|
|
2570
|
+
\\ case "audio_ctl":
|
|
2571
|
+
\\ nscfWU8(sink, 0x0f);
|
|
2572
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2573
|
+
\\ nscfWU8(sink, nscfMemberIndex(nscfAudioVerbs, cmd.verb, "audio verb"));
|
|
2574
|
+
\\ nscfWF64(sink, cmd.value);
|
|
2575
|
+
\\ return;
|
|
2576
|
+
\\ case "window_show":
|
|
2577
|
+
\\ nscfWU8(sink, 0x10);
|
|
2578
|
+
\\ nscfWShortText(sink, cmd.label);
|
|
2579
|
+
\\ return;
|
|
2580
|
+
\\ case "quit_app":
|
|
2581
|
+
\\ nscfWU8(sink, 0x11);
|
|
2582
|
+
\\ return;
|
|
2583
|
+
\\ case "image_load":
|
|
2584
|
+
\\ nscfWU8(sink, 0x12);
|
|
2585
|
+
\\ nscfWF64(sink, cmd.id);
|
|
2586
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.eventKind));
|
|
2587
|
+
\\ nscfWBytes(sink, cmd.path);
|
|
2588
|
+
\\ nscfWBytes(sink, cmd.url);
|
|
2589
|
+
\\ nscfWBytes(sink, cmd.cachePath);
|
|
2590
|
+
\\ nscfWF64(sink, cmd.expectedBytes);
|
|
2591
|
+
\\ return;
|
|
2592
|
+
\\ case "image_cancel":
|
|
2593
|
+
\\ nscfWU8(sink, 0x13);
|
|
2594
|
+
\\ nscfWF64(sink, cmd.id);
|
|
2595
|
+
\\ return;
|
|
2596
|
+
\\ case "image_unregister":
|
|
2597
|
+
\\ nscfWU8(sink, 0x14);
|
|
2598
|
+
\\ nscfWF64(sink, cmd.id);
|
|
2599
|
+
\\ return;
|
|
2600
|
+
\\ case "channel_open":
|
|
2601
|
+
\\ nscfWU8(sink, 0x15);
|
|
2602
|
+
\\ nscfWF64(sink, cmd.key);
|
|
2603
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.eventKind));
|
|
2604
|
+
\\ return;
|
|
2605
|
+
\\ case "channel_close":
|
|
2606
|
+
\\ nscfWU8(sink, 0x16);
|
|
2607
|
+
\\ nscfWF64(sink, cmd.key);
|
|
2608
|
+
\\ return;
|
|
2609
|
+
\\ case "video_load": {
|
|
2610
|
+
\\ nscfWU8(sink, 0x17);
|
|
2611
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2612
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.eventKind));
|
|
2613
|
+
\\ nscfWF64(sink, cmd.surface);
|
|
2614
|
+
\\ nscfWBytes(sink, cmd.path);
|
|
2615
|
+
\\ nscfWBytes(sink, cmd.url);
|
|
2616
|
+
\\ // Wire flags: bit0 = autoplay, bit1 = loop, bit2 = muted.
|
|
2617
|
+
\\ let flags = 0;
|
|
2618
|
+
\\ if (cmd.autoplay) flags = flags + 1;
|
|
2619
|
+
\\ if (cmd.loop) flags = flags + 2;
|
|
2620
|
+
\\ if (cmd.muted) flags = flags + 4;
|
|
2621
|
+
\\ nscfWU8(sink, flags);
|
|
2622
|
+
\\ return;
|
|
2623
|
+
\\ }
|
|
2624
|
+
\\ case "video_ctl":
|
|
2625
|
+
\\ nscfWU8(sink, 0x18);
|
|
2626
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2627
|
+
\\ nscfWU8(sink, nscfMemberIndex(nscfVideoVerbs, cmd.verb, "video verb"));
|
|
2628
|
+
\\ nscfWF64(sink, cmd.value);
|
|
2629
|
+
\\ return;
|
|
2630
|
+
\\ case "pty_spawn": {
|
|
2631
|
+
\\ nscfWU8(sink, 0x19);
|
|
2632
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2633
|
+
\\ nscfWU8(sink, nscfTagOf(cmd.eventKind));
|
|
2634
|
+
\\ nscfWF64(sink, cmd.cols);
|
|
2635
|
+
\\ nscfWF64(sink, cmd.rows);
|
|
2636
|
+
\\ nscfWShortText(sink, cmd.term);
|
|
2637
|
+
\\ if (cmd.argv.length === 0 || cmd.argv.length > 255) {
|
|
2638
|
+
\\ nscfTrap("a pty spawn carries no argv or over 255 argv entries — the wire's argv block cannot carry it");
|
|
2639
|
+
\\ }
|
|
2640
|
+
\\ nscfWU8(sink, cmd.argv.length);
|
|
2641
|
+
\\ for (let i = 0; i < cmd.argv.length; i++) {
|
|
2642
|
+
\\ nscfWBytes(sink, cmd.argv[i]!);
|
|
2643
|
+
\\ }
|
|
2644
|
+
\\ return;
|
|
2645
|
+
\\ }
|
|
2646
|
+
\\ case "pty_write":
|
|
2647
|
+
\\ nscfWU8(sink, 0x1a);
|
|
2648
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2649
|
+
\\ nscfWBytes(sink, cmd.bytes);
|
|
2650
|
+
\\ return;
|
|
2651
|
+
\\ case "pty_resize":
|
|
2652
|
+
\\ nscfWU8(sink, 0x1b);
|
|
2653
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2654
|
+
\\ nscfWF64(sink, cmd.cols);
|
|
2655
|
+
\\ nscfWF64(sink, cmd.rows);
|
|
2656
|
+
\\ return;
|
|
2657
|
+
\\ case "pty_kill":
|
|
2658
|
+
\\ nscfWU8(sink, 0x1c);
|
|
2659
|
+
\\ nscfWShortText(sink, cmd.key);
|
|
2660
|
+
\\ return;
|
|
2661
|
+
\\ case "batch":
|
|
2662
|
+
\\ for (let i = 0; i < cmd.cmds.length; i++) {
|
|
2663
|
+
\\ nscfEncodeCmd(sink, cmd.cmds[i]!);
|
|
2664
|
+
\\ }
|
|
2665
|
+
\\ return;
|
|
2666
|
+
\\ }
|
|
2667
|
+
\\}
|
|
2668
|
+
\\
|
|
2669
|
+
);
|
|
2670
|
+
try self.print(
|
|
2671
|
+
\\
|
|
2672
|
+
\\function nscfCmdBytes(cmd: nscfCmd<{s}>): Uint8Array {{
|
|
2673
|
+
\\ const sink = nscfNewSink();
|
|
2674
|
+
\\ nscfEncodeCmd(sink, cmd);
|
|
2675
|
+
\\ return nscfFinish(sink);
|
|
2676
|
+
\\}}
|
|
2677
|
+
\\
|
|
2678
|
+
, .{msg});
|
|
2679
|
+
}
|
|
2680
|
+
|
|
2681
|
+
fn subEncoder(self: *FacadeEmitter) Error!void {
|
|
2682
|
+
const msg = self.sidecar.msg.name;
|
|
2683
|
+
try self.print(
|
|
2684
|
+
\\
|
|
2685
|
+
\\// The v3 subscription wire (timer subscriptions).
|
|
2686
|
+
\\function nscfEncodeSub(sink: nscfSink, sub: nscfSub<{s}>): void {{
|
|
2687
|
+
\\ switch (sub.op) {{
|
|
2688
|
+
\\ case "none":
|
|
2689
|
+
\\ return;
|
|
2690
|
+
\\ case "timer":
|
|
2691
|
+
\\ nscfWU8(sink, 0x01);
|
|
2692
|
+
\\ nscfWShortText(sink, sub.key);
|
|
2693
|
+
\\ nscfWF64(sink, sub.everyMs);
|
|
2694
|
+
\\ nscfWU8(sink, nscfTagOf(sub.msgKind));
|
|
2695
|
+
\\ return;
|
|
2696
|
+
\\ case "batch":
|
|
2697
|
+
\\ for (let i = 0; i < sub.subs.length; i++) {{
|
|
2698
|
+
\\ nscfEncodeSub(sink, sub.subs[i]!);
|
|
2699
|
+
\\ }}
|
|
2700
|
+
\\ return;
|
|
2701
|
+
\\ }}
|
|
2702
|
+
\\}}
|
|
2703
|
+
\\
|
|
2704
|
+
\\function nscfSubBytes(sub: nscfSub<{s}>): Uint8Array {{
|
|
2705
|
+
\\ const sink = nscfNewSink();
|
|
2706
|
+
\\ nscfEncodeSub(sink, sub);
|
|
2707
|
+
\\ return nscfFinish(sink);
|
|
2708
|
+
\\}}
|
|
2709
|
+
\\
|
|
2710
|
+
, .{ msg, msg });
|
|
2711
|
+
}
|
|
2712
|
+
|
|
2713
|
+
// ---------------------------------------------------- spellings
|
|
2714
|
+
|
|
2715
|
+
/// The one TypeRef-to-TypeScript-spelling authority.
|
|
2716
|
+
fn spellRef(self: *FacadeEmitter, ref: TypeRef) Error![]const u8 {
|
|
2717
|
+
return switch (ref) {
|
|
2718
|
+
.bool => "boolean",
|
|
2719
|
+
.f64, .i64 => "number",
|
|
2720
|
+
.bytes => "Uint8Array",
|
|
2721
|
+
.void => "void",
|
|
2722
|
+
.optional => |inner| try std.fmt.allocPrint(self.arena, "{s} | null", .{try self.spellRef(inner.*)}),
|
|
2723
|
+
.slice => |elem| blk: {
|
|
2724
|
+
// Composite element spellings parenthesize: `number |
|
|
2725
|
+
// null[]` would type the null as the array.
|
|
2726
|
+
const spelled = try self.spellRef(elem.*);
|
|
2727
|
+
if (std.mem.indexOfAny(u8, spelled, " |") != null) {
|
|
2728
|
+
break :blk try std.fmt.allocPrint(self.arena, "({s})[]", .{spelled});
|
|
2729
|
+
}
|
|
2730
|
+
break :blk try std.fmt.allocPrint(self.arena, "{s}[]", .{spelled});
|
|
2731
|
+
},
|
|
2732
|
+
// Reference storage is a layout fact of the host mirror;
|
|
2733
|
+
// TypeScript sees the record value either way.
|
|
2734
|
+
.node, .value, .enum_ref, .union_ref => |name| blk: {
|
|
2735
|
+
try self.reference(name);
|
|
2736
|
+
break :blk name;
|
|
2737
|
+
},
|
|
2738
|
+
};
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
fn indentText(self: *FacadeEmitter, depth: usize) Error![]const u8 {
|
|
2742
|
+
const text = try self.arena.alloc(u8, depth * 2);
|
|
2743
|
+
@memset(text, ' ');
|
|
2744
|
+
return text;
|
|
2745
|
+
}
|
|
2746
|
+
|
|
2747
|
+
/// The struct behind a synthesized, inlined record reference at
|
|
2748
|
+
/// this site — the shape that flattens beside `kind`. VALUE
|
|
2749
|
+
/// references only: flattening is a by-value layout, so a
|
|
2750
|
+
/// node-stored payload keeps its named declaration however its
|
|
2751
|
+
/// name is spelled.
|
|
2752
|
+
fn synthesizedRecordOf(self: *FacadeEmitter, ref: TypeRef, container: []const u8, member: []const u8) ?*const sidecar_mod.Struct {
|
|
2753
|
+
const name = switch (ref) {
|
|
2754
|
+
.value => |n| n,
|
|
2755
|
+
else => return null,
|
|
2756
|
+
};
|
|
2757
|
+
if (!emit_mod.isSynthesizedRef(container, member, name) or !nameListed(self.inlined, name)) return null;
|
|
2758
|
+
const record = sidecar_mod.findStruct(self.sidecar.types, name) orelse return null;
|
|
2759
|
+
if (record.origin != null) return null;
|
|
2760
|
+
return record;
|
|
2761
|
+
}
|
|
2762
|
+
|
|
2763
|
+
/// The synthesized single-use records this projection flattens into
|
|
2764
|
+
/// their one arm literal: neither a re-export nor a named writer
|
|
2765
|
+
/// ever spells them (the compiler re-derives the same names from
|
|
2766
|
+
/// the inline arms).
|
|
2767
|
+
fn flattenedTableNames(self: *FacadeEmitter) Error![]const []const u8 {
|
|
2768
|
+
var names: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
2769
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
2770
|
+
switch (arm.payload) {
|
|
2771
|
+
.record => {
|
|
2772
|
+
if (self.synthesizedRecordOf(recordPayloadRef(arm.payload), self.sidecar.msg.name, arm.name)) |record| {
|
|
2773
|
+
try names.append(self.arena, record.name);
|
|
2774
|
+
}
|
|
2775
|
+
},
|
|
2776
|
+
else => {},
|
|
2777
|
+
}
|
|
2778
|
+
}
|
|
2779
|
+
for (self.sidecar.types.unions) |entry| {
|
|
2780
|
+
for (entry.arms) |arm| {
|
|
2781
|
+
if (arm.payload == .void) continue;
|
|
2782
|
+
if (self.synthesizedRecordOf(arm.payload, entry.name, arm.name)) |record| {
|
|
2783
|
+
try names.append(self.arena, record.name);
|
|
2784
|
+
}
|
|
2785
|
+
}
|
|
2786
|
+
}
|
|
2787
|
+
return names.items;
|
|
2788
|
+
}
|
|
2789
|
+
|
|
2790
|
+
/// The record names the contract stores BY REFERENCE anywhere.
|
|
2791
|
+
fn nodeStoredTableNames(self: *FacadeEmitter) Error![]const []const u8 {
|
|
2792
|
+
var names: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
2793
|
+
// The model root is reference-stored by contract (no explicit
|
|
2794
|
+
// reference spells it), so it seeds the set: a `value`
|
|
2795
|
+
// reference to the root elsewhere is the mixed-storage case and
|
|
2796
|
+
// refuses like any other.
|
|
2797
|
+
try names.append(self.arena, self.sidecar.model);
|
|
2798
|
+
for (self.sidecar.types.structs) |entry| {
|
|
2799
|
+
for (entry.fields) |field| {
|
|
2800
|
+
try noteNodeRefs(&names, self.arena, field.type);
|
|
2801
|
+
}
|
|
2802
|
+
}
|
|
2803
|
+
for (self.sidecar.types.unions) |entry| {
|
|
2804
|
+
for (entry.arms) |arm| {
|
|
2805
|
+
try noteNodeRefs(&names, self.arena, arm.payload);
|
|
2806
|
+
}
|
|
2807
|
+
}
|
|
2808
|
+
for (self.sidecar.model_helpers) |helper| {
|
|
2809
|
+
try noteNodeRefs(&names, self.arena, helper.returns);
|
|
2810
|
+
for (helper.params) |param| try noteNodeRefs(&names, self.arena, param);
|
|
2811
|
+
}
|
|
2812
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
2813
|
+
switch (arm.payload) {
|
|
2814
|
+
.scalar => |ref| try noteNodeRefs(&names, self.arena, ref),
|
|
2815
|
+
else => {},
|
|
2816
|
+
}
|
|
2817
|
+
}
|
|
2818
|
+
return names.items;
|
|
2819
|
+
}
|
|
2820
|
+
};
|
|
2821
|
+
|
|
2822
|
+
/// Locals-then-construction decode of canonical bytes. The emitted mutable
|
|
2823
|
+
/// cursor advances in the lexical branch that consumes each value, so
|
|
2824
|
+
/// nonterminal optionals and variable-width composites compose naturally.
|
|
2825
|
+
/// Integer locals join the nearest construction guard and state wholeness
|
|
2826
|
+
/// with Math.trunc at the guarded write.
|
|
2827
|
+
const RecordDecode = struct {
|
|
2828
|
+
emitter: *FacadeEmitter,
|
|
2829
|
+
buf: []const u8,
|
|
2830
|
+
indent: usize,
|
|
2831
|
+
start: []const u8 = "0",
|
|
2832
|
+
saturating_selection: bool = false,
|
|
2833
|
+
local_count: usize = 0,
|
|
2834
|
+
cursor_started: bool = false,
|
|
2835
|
+
guards: std.ArrayListUnmanaged(u8) = .empty,
|
|
2836
|
+
exprs: std.ArrayListUnmanaged(struct { name: []const u8, text: []const u8 }) = .empty,
|
|
2837
|
+
|
|
2838
|
+
const Self = @This();
|
|
2839
|
+
|
|
2840
|
+
fn arena(self: *Self) std.mem.Allocator {
|
|
2841
|
+
return self.emitter.arena;
|
|
2842
|
+
}
|
|
2843
|
+
|
|
2844
|
+
fn offsetText(_: *Self) []const u8 {
|
|
2845
|
+
return "nscfAt";
|
|
2846
|
+
}
|
|
2847
|
+
|
|
2848
|
+
fn nextLocal(self: *Self) Error![]const u8 {
|
|
2849
|
+
const name = try std.fmt.allocPrint(self.arena(), "nscfV{d}", .{self.local_count});
|
|
2850
|
+
self.local_count += 1;
|
|
2851
|
+
return name;
|
|
2852
|
+
}
|
|
2853
|
+
|
|
2854
|
+
fn line(self: *Self, comptime fmt: []const u8, args: anytype) Error!void {
|
|
2855
|
+
const pad = try self.emitter.indentText(self.indent);
|
|
2856
|
+
try self.emitter.print("{s}", .{pad});
|
|
2857
|
+
try self.emitter.print(fmt, args);
|
|
2858
|
+
}
|
|
2859
|
+
|
|
2860
|
+
fn ensureCursor(self: *Self) Error!void {
|
|
2861
|
+
if (self.cursor_started) return;
|
|
2862
|
+
self.cursor_started = true;
|
|
2863
|
+
try self.line("let nscfAt = {s};\n", .{self.start});
|
|
2864
|
+
}
|
|
2865
|
+
|
|
2866
|
+
fn advance(self: *Self, amount: []const u8) Error!void {
|
|
2867
|
+
try self.line("nscfAt += {s};\n", .{amount});
|
|
2868
|
+
}
|
|
2869
|
+
|
|
2870
|
+
fn advanceFixed(self: *Self, amount: usize) Error!void {
|
|
2871
|
+
try self.advance(try std.fmt.allocPrint(self.arena(), "{d}", .{amount}));
|
|
2872
|
+
}
|
|
2873
|
+
|
|
2874
|
+
fn addGuard(self: *Self, guards: *std.ArrayListUnmanaged(u8), local: []const u8, class: IntegerClass) Error!void {
|
|
2875
|
+
if (guards.items.len > 0) try guards.appendSlice(self.arena(), " && ");
|
|
2876
|
+
try guards.appendSlice(self.arena(), try std.fmt.allocPrint(self.arena(), "{s} >= {s} && {s} <= {s}", .{ local, integerLowerBound(class), local, max_safe }));
|
|
2877
|
+
}
|
|
2878
|
+
|
|
2879
|
+
fn guardedStatement(self: *Self, guards: []const u8, statement: []const u8) Error!void {
|
|
2880
|
+
if (guards.len == 0) {
|
|
2881
|
+
try self.line("{s}\n", .{statement});
|
|
2882
|
+
return;
|
|
2883
|
+
}
|
|
2884
|
+
try self.line("if ({s}) {{\n", .{guards});
|
|
2885
|
+
self.indent += 1;
|
|
2886
|
+
try self.line("{s}\n", .{statement});
|
|
2887
|
+
self.indent -= 1;
|
|
2888
|
+
try self.line("}} else {{\n", .{});
|
|
2889
|
+
self.indent += 1;
|
|
2890
|
+
try self.line("nscfTrap(\"a decoded integer value is NaN or outside its attested exact-integer range — the integer slot has no honest value for it\");\n", .{});
|
|
2891
|
+
self.indent -= 1;
|
|
2892
|
+
try self.line("}}\n", .{});
|
|
2893
|
+
self.emitter.use(.trap);
|
|
2894
|
+
}
|
|
2895
|
+
|
|
2896
|
+
fn run(self: *Self, record: *const sidecar_mod.Struct) Error!void {
|
|
2897
|
+
try self.ensureCursor();
|
|
2898
|
+
for (record.fields) |field| try self.fieldLocal(field, record.name, field.name);
|
|
2899
|
+
}
|
|
2900
|
+
|
|
2901
|
+
fn fieldLocal(self: *Self, field: sidecar_mod.Field, container: []const u8, member: []const u8) Error!void {
|
|
2902
|
+
try self.ensureCursor();
|
|
2903
|
+
const text = try self.decodeRef(field.type, field.name, container, member, &self.guards);
|
|
2904
|
+
try self.exprs.append(self.arena(), .{ .name = field.name, .text = text });
|
|
2905
|
+
}
|
|
2906
|
+
|
|
2907
|
+
fn decodeRecord(self: *Self, record: *const sidecar_mod.Struct, guards: *std.ArrayListUnmanaged(u8)) Error![]const u8 {
|
|
2908
|
+
var parts: std.ArrayListUnmanaged(u8) = .empty;
|
|
2909
|
+
for (record.fields, 0..) |field, index| {
|
|
2910
|
+
const text = try self.decodeRef(field.type, field.name, record.name, field.name, guards);
|
|
2911
|
+
if (index > 0) try parts.appendSlice(self.arena(), ", ");
|
|
2912
|
+
try parts.appendSlice(self.arena(), try std.fmt.allocPrint(self.arena(), "{s}: {s}", .{ try tsProp(self.arena(), field.name), text }));
|
|
2913
|
+
}
|
|
2914
|
+
return try std.fmt.allocPrint(self.arena(), "{{ {s} }}", .{parts.items});
|
|
2915
|
+
}
|
|
2916
|
+
|
|
2917
|
+
fn decodeUnion(self: *Self, name: []const u8) Error![]const u8 {
|
|
2918
|
+
const em = self.emitter;
|
|
2919
|
+
const entry = sidecar_mod.findUnion(em.sidecar.types, name) orelse {
|
|
2920
|
+
em.diags.flag("types", "\"{s}\" names no tabled union", .{name});
|
|
2921
|
+
return "undefined";
|
|
2922
|
+
};
|
|
2923
|
+
em.use(.read_u8);
|
|
2924
|
+
const tag = try self.nextLocal();
|
|
2925
|
+
try self.line("const {s} = nscfReadU8({s}, nscfAt);\n", .{ tag, self.buf });
|
|
2926
|
+
try self.advanceFixed(1);
|
|
2927
|
+
const local = try self.nextLocal();
|
|
2928
|
+
try self.line("let {s}: {s};\n", .{ local, name });
|
|
2929
|
+
for (entry.arms, 0..) |arm, index| {
|
|
2930
|
+
try self.line("{s}if ({s} === {d}) {{\n", .{ if (index == 0) "" else "else ", tag, index });
|
|
2931
|
+
self.indent += 1;
|
|
2932
|
+
var arm_guards: std.ArrayListUnmanaged(u8) = .empty;
|
|
2933
|
+
const object = if (arm.payload == .void)
|
|
2934
|
+
try std.fmt.allocPrint(self.arena(), "{{ kind: \"{s}\" }}", .{try tsString(self.arena(), arm.name)})
|
|
2935
|
+
else if (em.synthesizedRecordOf(arm.payload, entry.name, arm.name)) |record| blk: {
|
|
2936
|
+
const inner = try self.decodeRecord(record, &arm_guards);
|
|
2937
|
+
break :blk try std.fmt.allocPrint(self.arena(), "{{ kind: \"{s}\",{s}}}", .{ try tsString(self.arena(), arm.name), inner[1 .. inner.len - 1] });
|
|
2938
|
+
} else blk: {
|
|
2939
|
+
const value = try self.decodeRef(arm.payload, em.memberOf(arm.member), entry.name, arm.name, &arm_guards);
|
|
2940
|
+
break :blk try std.fmt.allocPrint(self.arena(), "{{ kind: \"{s}\", {s}: {s} }}", .{ try tsString(self.arena(), arm.name), try tsProp(self.arena(), em.memberOf(arm.member)), value });
|
|
2941
|
+
};
|
|
2942
|
+
try self.guardedStatement(arm_guards.items, try std.fmt.allocPrint(self.arena(), "{s} = {s};", .{ local, object }));
|
|
2943
|
+
self.indent -= 1;
|
|
2944
|
+
try self.line("}}\n", .{});
|
|
2945
|
+
}
|
|
2946
|
+
try self.line("else {{\n", .{});
|
|
2947
|
+
self.indent += 1;
|
|
2948
|
+
try self.line("nscfTrap(\"a dispatched union payload carries a union arm index past the declared arms — the host and this core disagree about the contract\");\n", .{});
|
|
2949
|
+
self.indent -= 1;
|
|
2950
|
+
try self.line("}}\n", .{});
|
|
2951
|
+
em.use(.trap);
|
|
2952
|
+
return local;
|
|
2953
|
+
}
|
|
2954
|
+
|
|
2955
|
+
fn decodeRef(self: *Self, ref: TypeRef, field_name: []const u8, container: []const u8, member: []const u8, guards: *std.ArrayListUnmanaged(u8)) Error![]const u8 {
|
|
2956
|
+
const em = self.emitter;
|
|
2957
|
+
return switch (ref) {
|
|
2958
|
+
.f64 => blk: {
|
|
2959
|
+
em.use(.read_f64);
|
|
2960
|
+
const local = try self.nextLocal();
|
|
2961
|
+
try self.line("const {s} = nscfReadF64({s}, nscfAt);\n", .{ local, self.buf });
|
|
2962
|
+
try self.advanceFixed(8);
|
|
2963
|
+
break :blk local;
|
|
2964
|
+
},
|
|
2965
|
+
.i64 => blk: {
|
|
2966
|
+
const class = em.slotClass(container, member) orelse .i64;
|
|
2967
|
+
const selection_field = std.mem.eql(u8, field_name, "anchor") or std.mem.eql(u8, field_name, "focus");
|
|
2968
|
+
const reader: Codec = if (self.saturating_selection and selection_field)
|
|
2969
|
+
if (class == .u64) .read_u64_saturating else .read_i64_saturating
|
|
2970
|
+
else
|
|
2971
|
+
.read_i64;
|
|
2972
|
+
em.use(reader);
|
|
2973
|
+
const local = try self.nextLocal();
|
|
2974
|
+
const reader_name = switch (reader) {
|
|
2975
|
+
.read_i64 => "nscfReadI64",
|
|
2976
|
+
.read_i64_saturating => "nscfReadI64Saturating",
|
|
2977
|
+
.read_u64_saturating => "nscfReadU64Saturating",
|
|
2978
|
+
else => unreachable,
|
|
2979
|
+
};
|
|
2980
|
+
try self.line("const {s} = {s}({s}, nscfAt);\n", .{ local, reader_name, self.buf });
|
|
2981
|
+
try self.advanceFixed(8);
|
|
2982
|
+
try self.addGuard(guards, local, class);
|
|
2983
|
+
break :blk try std.fmt.allocPrint(self.arena(), "Math.trunc({s})", .{local});
|
|
2984
|
+
},
|
|
2985
|
+
.bool => blk: {
|
|
2986
|
+
em.use(.read_bool);
|
|
2987
|
+
const local = try self.nextLocal();
|
|
2988
|
+
try self.line("const {s} = nscfReadBool({s}, nscfAt);\n", .{ local, self.buf });
|
|
2989
|
+
try self.advanceFixed(1);
|
|
2990
|
+
break :blk local;
|
|
2991
|
+
},
|
|
2992
|
+
.bytes => blk: {
|
|
2993
|
+
em.use(.read_u32);
|
|
2994
|
+
em.use(.read_bytes_body);
|
|
2995
|
+
const len_local = try self.nextLocal();
|
|
2996
|
+
try self.line("const {s} = nscfReadU32({s}, nscfAt);\n", .{ len_local, self.buf });
|
|
2997
|
+
try self.advanceFixed(4);
|
|
2998
|
+
const local = try self.nextLocal();
|
|
2999
|
+
try self.line("const {s} = nscfReadBytesBody({s}, nscfAt, {s});\n", .{ local, self.buf, len_local });
|
|
3000
|
+
try self.advance(len_local);
|
|
3001
|
+
break :blk local;
|
|
3002
|
+
},
|
|
3003
|
+
.enum_ref => |name| blk: {
|
|
3004
|
+
em.use(.read_u32);
|
|
3005
|
+
try em.needEnumTable(name);
|
|
3006
|
+
em.needs_member_trap = true;
|
|
3007
|
+
const local = try self.nextLocal();
|
|
3008
|
+
try self.line("const {s} = nscfReadU32({s}, nscfAt);\n", .{ local, self.buf });
|
|
3009
|
+
try self.line("if ({s} >= nscfMembers{s}.length) nscfMember(\"{s}\", {s});\n", .{ local, name, try tsString(self.arena(), name), local });
|
|
3010
|
+
try self.advanceFixed(4);
|
|
3011
|
+
break :blk try std.fmt.allocPrint(self.arena(), "nscfMembers{s}[{s}]!", .{ name, local });
|
|
3012
|
+
},
|
|
3013
|
+
.node, .value => |name| blk: {
|
|
3014
|
+
const nested = sidecar_mod.findStruct(em.sidecar.types, name) orelse {
|
|
3015
|
+
em.diags.flag("types", "\"{s}\" names no tabled record", .{name});
|
|
3016
|
+
break :blk "undefined";
|
|
3017
|
+
};
|
|
3018
|
+
break :blk try self.decodeRecord(nested, guards);
|
|
3019
|
+
},
|
|
3020
|
+
.optional => |inner| blk: {
|
|
3021
|
+
em.use(.read_bool);
|
|
3022
|
+
const present = try self.nextLocal();
|
|
3023
|
+
try self.line("const {s} = nscfReadBool({s}, nscfAt);\n", .{ present, self.buf });
|
|
3024
|
+
try self.advanceFixed(1);
|
|
3025
|
+
const local = try self.nextLocal();
|
|
3026
|
+
try self.line("let {s}: {s} | null = null;\n", .{ local, try em.spellRef(inner.*) });
|
|
3027
|
+
try self.line("if ({s}) {{\n", .{present});
|
|
3028
|
+
self.indent += 1;
|
|
3029
|
+
var inner_guards: std.ArrayListUnmanaged(u8) = .empty;
|
|
3030
|
+
const value = try self.decodeRef(inner.*, field_name, container, member, &inner_guards);
|
|
3031
|
+
try self.guardedStatement(inner_guards.items, try std.fmt.allocPrint(self.arena(), "{s} = {s};", .{ local, value }));
|
|
3032
|
+
self.indent -= 1;
|
|
3033
|
+
try self.line("}}\n", .{});
|
|
3034
|
+
break :blk local;
|
|
3035
|
+
},
|
|
3036
|
+
.slice => |elem| blk: {
|
|
3037
|
+
em.use(.read_u32);
|
|
3038
|
+
const len_local = try self.nextLocal();
|
|
3039
|
+
try self.line("const {s} = nscfReadU32({s}, nscfAt);\n", .{ len_local, self.buf });
|
|
3040
|
+
try self.advanceFixed(4);
|
|
3041
|
+
const local = try self.nextLocal();
|
|
3042
|
+
try self.line("const {s}: {s} = [];\n", .{ local, try em.spellRef(ref) });
|
|
3043
|
+
const index = try self.nextLocal();
|
|
3044
|
+
try self.line("for (let {s} = 0; {s} < {s}; {s}++) {{\n", .{ index, index, len_local, index });
|
|
3045
|
+
self.indent += 1;
|
|
3046
|
+
var elem_guards: std.ArrayListUnmanaged(u8) = .empty;
|
|
3047
|
+
const value = try self.decodeRef(elem.*, field_name, container, member, &elem_guards);
|
|
3048
|
+
try self.guardedStatement(elem_guards.items, try std.fmt.allocPrint(self.arena(), "{s}.push({s});", .{ local, value }));
|
|
3049
|
+
self.indent -= 1;
|
|
3050
|
+
try self.line("}}\n", .{});
|
|
3051
|
+
break :blk local;
|
|
3052
|
+
},
|
|
3053
|
+
.union_ref => |name| try self.decodeUnion(name),
|
|
3054
|
+
.void => blk: {
|
|
3055
|
+
em.diags.flag("types", "a record field cannot carry void", .{});
|
|
3056
|
+
break :blk "undefined";
|
|
3057
|
+
},
|
|
3058
|
+
};
|
|
3059
|
+
}
|
|
3060
|
+
|
|
3061
|
+
fn constructionText(self: *Self, record: *const sidecar_mod.Struct) Error![]const u8 {
|
|
3062
|
+
_ = record;
|
|
3063
|
+
var out: std.ArrayListUnmanaged(u8) = .empty;
|
|
3064
|
+
try out.appendSlice(self.arena(), "{ ");
|
|
3065
|
+
for (self.exprs.items, 0..) |entry, index| {
|
|
3066
|
+
if (index > 0) try out.appendSlice(self.arena(), ", ");
|
|
3067
|
+
try out.appendSlice(self.arena(), try std.fmt.allocPrint(self.arena(), "{s}: {s}", .{ try tsProp(self.arena(), entry.name), entry.text }));
|
|
3068
|
+
}
|
|
3069
|
+
try out.appendSlice(self.arena(), " }");
|
|
3070
|
+
return out.items;
|
|
3071
|
+
}
|
|
3072
|
+
};
|
|
3073
|
+
|
|
3074
|
+
/// The scroll entry's parameter feeding an authored scroll field.
|
|
3075
|
+
fn scrollParamFor(field_name: []const u8) ?[]const u8 {
|
|
3076
|
+
for (scroll_state_fields_ts, 0..) |candidate, index| {
|
|
3077
|
+
if (std.mem.eql(u8, candidate, field_name) or std.mem.eql(u8, scroll_state_fields_canvas[index], field_name)) return candidate;
|
|
3078
|
+
}
|
|
3079
|
+
return null;
|
|
3080
|
+
}
|
|
3081
|
+
|
|
3082
|
+
/// Collect the record names `ref` reaches through NODE references,
|
|
3083
|
+
/// walking the optional/slice wrappers (a node behind an optional or a
|
|
3084
|
+
/// sequence is node storage all the same).
|
|
3085
|
+
fn noteNodeRefs(names: *std.ArrayListUnmanaged([]const u8), arena: std.mem.Allocator, ref: TypeRef) error{OutOfMemory}!void {
|
|
3086
|
+
switch (ref) {
|
|
3087
|
+
.node => |name| {
|
|
3088
|
+
if (!nameListed(names.items, name)) try names.append(arena, name);
|
|
3089
|
+
},
|
|
3090
|
+
.optional => |inner| try noteNodeRefs(names, arena, inner.*),
|
|
3091
|
+
.slice => |elem| try noteNodeRefs(names, arena, elem.*),
|
|
3092
|
+
else => {},
|
|
3093
|
+
}
|
|
3094
|
+
}
|
|
3095
|
+
|
|
3096
|
+
/// The VALUE-reference twin of noteNodeRefs.
|
|
3097
|
+
fn noteValueRefs(names: *std.ArrayListUnmanaged([]const u8), arena: std.mem.Allocator, ref: TypeRef) error{OutOfMemory}!void {
|
|
3098
|
+
switch (ref) {
|
|
3099
|
+
.value => |name| {
|
|
3100
|
+
if (!nameListed(names.items, name)) try names.append(arena, name);
|
|
3101
|
+
},
|
|
3102
|
+
.optional => |inner| try noteValueRefs(names, arena, inner.*),
|
|
3103
|
+
.slice => |elem| try noteValueRefs(names, arena, elem.*),
|
|
3104
|
+
else => {},
|
|
3105
|
+
}
|
|
3106
|
+
}
|
|
3107
|
+
|
|
3108
|
+
/// A msg record payload as the TypeRef shape synthesizedRecordOf reads.
|
|
3109
|
+
fn recordPayloadRef(payload: sidecar_mod.Payload) TypeRef {
|
|
3110
|
+
return switch (payload) {
|
|
3111
|
+
.record => |name| .{ .value = name },
|
|
3112
|
+
else => unreachable,
|
|
3113
|
+
};
|
|
3114
|
+
}
|
|
3115
|
+
|
|
3116
|
+
/// The numeric ABI class of either spelling the sidecar accepts for a
|
|
3117
|
+
/// one-number message payload. Dedicated descriptors are what current
|
|
3118
|
+
/// extractors emit; scalar descriptors remain a valid format-1 alias and
|
|
3119
|
+
/// must reach the same facade entry as the generated mirror.
|
|
3120
|
+
fn payloadNumberClass(payload: sidecar_mod.Payload) ?sidecar_mod.NumberClass {
|
|
3121
|
+
return switch (payload) {
|
|
3122
|
+
.number => |class| class,
|
|
3123
|
+
.scalar => |ref| switch (ref) {
|
|
3124
|
+
.f64 => .f64,
|
|
3125
|
+
.i64 => .i64,
|
|
3126
|
+
else => null,
|
|
3127
|
+
},
|
|
3128
|
+
else => null,
|
|
3129
|
+
};
|
|
3130
|
+
}
|
|
3131
|
+
|
|
3132
|
+
fn pathText(arena: std.mem.Allocator, comptime fmt: []const u8, args: anytype) []const u8 {
|
|
3133
|
+
return std.fmt.allocPrint(arena, fmt, args) catch "";
|
|
3134
|
+
}
|
|
3135
|
+
|
|
3136
|
+
fn nameListed(names: []const []const u8, name: []const u8) bool {
|
|
3137
|
+
for (names) |candidate| {
|
|
3138
|
+
if (std.mem.eql(u8, candidate, name)) return true;
|
|
3139
|
+
}
|
|
3140
|
+
return false;
|
|
3141
|
+
}
|
|
3142
|
+
|
|
3143
|
+
fn commentText(arena: std.mem.Allocator, text: []const u8) error{OutOfMemory}![]const u8 {
|
|
3144
|
+
const out = try arena.dupe(u8, text);
|
|
3145
|
+
for (out) |*char| {
|
|
3146
|
+
if (char.* < 0x20 or char.* == 0x7f) char.* = ' ';
|
|
3147
|
+
}
|
|
3148
|
+
// U+2028/U+2029 are line terminators to a TypeScript scanner: blank
|
|
3149
|
+
// their UTF-8 bytes so provenance text cannot end the comment early.
|
|
3150
|
+
var index: usize = 0;
|
|
3151
|
+
while (index + 2 < out.len) : (index += 1) {
|
|
3152
|
+
if (out[index] == 0xe2 and out[index + 1] == 0x80 and (out[index + 2] == 0xa8 or out[index + 2] == 0xa9)) {
|
|
3153
|
+
out[index] = ' ';
|
|
3154
|
+
out[index + 1] = ' ';
|
|
3155
|
+
out[index + 2] = ' ';
|
|
3156
|
+
}
|
|
3157
|
+
}
|
|
3158
|
+
return out;
|
|
3159
|
+
}
|
|
3160
|
+
|
|
3161
|
+
/// Escape a name into a TS double-quoted string literal (arm names ride
|
|
3162
|
+
/// string literals in the kind-tagged union). The scanner's line
|
|
3163
|
+
/// terminators are LF, CR, LS (U+2028), and PS (U+2029) — all escaped
|
|
3164
|
+
/// here (NEL U+0085 is ordinary text to the scanner); quotes and
|
|
3165
|
+
/// backslashes escape byte-for-byte.
|
|
3166
|
+
fn tsString(arena: std.mem.Allocator, text: []const u8) error{OutOfMemory}![]const u8 {
|
|
3167
|
+
var out: std.ArrayListUnmanaged(u8) = .empty;
|
|
3168
|
+
var index: usize = 0;
|
|
3169
|
+
while (index < text.len) {
|
|
3170
|
+
const char = text[index];
|
|
3171
|
+
if (char == 0xe2 and index + 2 < text.len and text[index + 1] == 0x80 and
|
|
3172
|
+
(text[index + 2] == 0xa8 or text[index + 2] == 0xa9))
|
|
3173
|
+
{
|
|
3174
|
+
const escape: []const u8 = if (text[index + 2] == 0xa8) "\\u2028" else "\\u2029";
|
|
3175
|
+
try out.appendSlice(arena, escape);
|
|
3176
|
+
index += 3;
|
|
3177
|
+
continue;
|
|
3178
|
+
}
|
|
3179
|
+
switch (char) {
|
|
3180
|
+
'"' => try out.appendSlice(arena, "\\\""),
|
|
3181
|
+
'\\' => try out.appendSlice(arena, "\\\\"),
|
|
3182
|
+
'\n' => try out.appendSlice(arena, "\\n"),
|
|
3183
|
+
'\r' => try out.appendSlice(arena, "\\r"),
|
|
3184
|
+
'\t' => try out.appendSlice(arena, "\\t"),
|
|
3185
|
+
else => try out.append(arena, char),
|
|
3186
|
+
}
|
|
3187
|
+
index += 1;
|
|
3188
|
+
}
|
|
3189
|
+
return out.items;
|
|
3190
|
+
}
|
|
3191
|
+
|
|
3192
|
+
/// A property spelling: plain identifiers stay bare (reserved words
|
|
3193
|
+
/// are legal property names); anything else quotes.
|
|
3194
|
+
fn tsProp(arena: std.mem.Allocator, name: []const u8) error{OutOfMemory}![]const u8 {
|
|
3195
|
+
if (isIdentifierFragment(name) and name.len > 0 and !(name[0] >= '0' and name[0] <= '9')) {
|
|
3196
|
+
return name;
|
|
3197
|
+
}
|
|
3198
|
+
return std.fmt.allocPrint(arena, "\"{s}\"", .{try tsString(arena, name)});
|
|
3199
|
+
}
|
|
3200
|
+
|
|
3201
|
+
/// A property ACCESS: dot for plain spellings, brackets otherwise.
|
|
3202
|
+
fn tsAccess(arena: std.mem.Allocator, base: []const u8, name: []const u8) error{OutOfMemory}![]const u8 {
|
|
3203
|
+
if (isIdentifierFragment(name) and name.len > 0 and !(name[0] >= '0' and name[0] <= '9')) {
|
|
3204
|
+
return std.fmt.allocPrint(arena, "{s}.{s}", .{ base, name });
|
|
3205
|
+
}
|
|
3206
|
+
return std.fmt.allocPrint(arena, "{s}[\"{s}\"]", .{ base, try tsString(arena, name) });
|
|
3207
|
+
}
|
|
3208
|
+
|
|
3209
|
+
/// Why a name cannot survive the WHOLE pipeline, or null when it can:
|
|
3210
|
+
/// the shared identifier charset (letters, digits, underscore), a
|
|
3211
|
+
/// non-digit start, not the discard spelling, and no keyword or
|
|
3212
|
+
/// primitive-type name of either language.
|
|
3213
|
+
const NameRole = enum {
|
|
3214
|
+
declaration,
|
|
3215
|
+
member,
|
|
3216
|
+
};
|
|
3217
|
+
|
|
3218
|
+
fn pipelineIdentifierIssue(name: []const u8, role: NameRole) ?[]const u8 {
|
|
3219
|
+
if (name.len == 0) return "is empty";
|
|
3220
|
+
if (std.mem.eql(u8, name, "_")) return "is the discard spelling in the compiled module";
|
|
3221
|
+
if (name[0] >= '0' and name[0] <= '9') return "starts with a digit";
|
|
3222
|
+
for (name) |char| {
|
|
3223
|
+
const ok = (char >= 'a' and char <= 'z') or (char >= 'A' and char <= 'Z') or
|
|
3224
|
+
(char >= '0' and char <= '9') or char == '_';
|
|
3225
|
+
if (!ok) return "uses characters outside the compiled module's identifier set (letters, digits, underscore)";
|
|
3226
|
+
}
|
|
3227
|
+
if (std.zig.Token.keywords.has(name)) return "is a keyword in the compiled module";
|
|
3228
|
+
if (std.zig.isPrimitive(name)) return "is a primitive type name in the compiled module";
|
|
3229
|
+
if (role == .declaration) {
|
|
3230
|
+
for (ts_reserved_words) |word| {
|
|
3231
|
+
if (std.mem.eql(u8, name, word)) return "is a reserved word in TypeScript";
|
|
3232
|
+
}
|
|
3233
|
+
}
|
|
3234
|
+
return null;
|
|
3235
|
+
}
|
|
3236
|
+
|
|
3237
|
+
fn isIdentifierFragment(name: []const u8) bool {
|
|
3238
|
+
if (name.len == 0) return false;
|
|
3239
|
+
for (name) |char| {
|
|
3240
|
+
const ok = (char >= 'a' and char <= 'z') or (char >= 'A' and char <= 'Z') or
|
|
3241
|
+
(char >= '0' and char <= '9') or char == '_' or char == '$';
|
|
3242
|
+
if (!ok) return false;
|
|
3243
|
+
}
|
|
3244
|
+
return true;
|
|
3245
|
+
}
|
|
3246
|
+
|
|
3247
|
+
fn isTsIdentifier(name: []const u8) bool {
|
|
3248
|
+
if (name.len == 0) return false;
|
|
3249
|
+
if (name[0] >= '0' and name[0] <= '9') return false;
|
|
3250
|
+
for (name) |char| {
|
|
3251
|
+
const ok = (char >= 'a' and char <= 'z') or (char >= 'A' and char <= 'Z') or
|
|
3252
|
+
(char >= '0' and char <= '9') or char == '_' or char == '$';
|
|
3253
|
+
if (!ok) return false;
|
|
3254
|
+
}
|
|
3255
|
+
for (ts_reserved_words) |word| {
|
|
3256
|
+
if (std.mem.eql(u8, name, word)) return false;
|
|
3257
|
+
}
|
|
3258
|
+
return true;
|
|
3259
|
+
}
|
|
3260
|
+
|
|
3261
|
+
// --------------------------------------------------------------- tests
|
|
3262
|
+
|
|
3263
|
+
const testing = std.testing;
|
|
3264
|
+
|
|
3265
|
+
/// Most facade tests perturb sidecar_mod's deliberately LEGACY minimal
|
|
3266
|
+
/// fixture. Upgrade its two authored facts here so unrelated refusal tests
|
|
3267
|
+
/// exercise the current facade boundary; the dedicated legacy test below
|
|
3268
|
+
/// calls emitFacade directly on the untouched fixture.
|
|
3269
|
+
fn withCurrentFacadeFacts(arena: std.mem.Allocator, json: []const u8) ![]const u8 {
|
|
3270
|
+
var current = json;
|
|
3271
|
+
const model = "{\"name\": \"Model\", \"fields\":";
|
|
3272
|
+
if (std.mem.indexOf(u8, current, model) != null) {
|
|
3273
|
+
current = try std.mem.replaceOwned(u8, arena, current, model, "{\"name\": \"Model\", \"origin\": \"core.ts\", \"fields\":");
|
|
3274
|
+
}
|
|
3275
|
+
const label_arm = "{\"name\": \"label_set\", \"payload\": {\"kind\": \"bytes\"}}";
|
|
3276
|
+
if (std.mem.indexOf(u8, current, label_arm) != null) {
|
|
3277
|
+
current = try std.mem.replaceOwned(u8, arena, current, label_arm, "{\"name\": \"label_set\", \"member\": \"value\", \"payload\": {\"kind\": \"bytes\"}}");
|
|
3278
|
+
}
|
|
3279
|
+
return current;
|
|
3280
|
+
}
|
|
3281
|
+
|
|
3282
|
+
fn facadeFromJson(arena: std.mem.Allocator, json: []const u8) ![]const u8 {
|
|
3283
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
3284
|
+
const parsed = sidecar_mod.read(arena, try withCurrentFacadeFacts(arena, json), &diags) catch |err| {
|
|
3285
|
+
for (diags.list.items) |item| {
|
|
3286
|
+
std.debug.print(" [{s}] {s}: {s}\n", .{ @tagName(item.severity), item.path, item.message });
|
|
3287
|
+
}
|
|
3288
|
+
return err;
|
|
3289
|
+
};
|
|
3290
|
+
return emitFacade(arena, parsed, &diags) catch |err| {
|
|
3291
|
+
for (diags.list.items) |item| {
|
|
3292
|
+
std.debug.print(" [{s}] {s}: {s}\n", .{ @tagName(item.severity), item.path, item.message });
|
|
3293
|
+
}
|
|
3294
|
+
return err;
|
|
3295
|
+
};
|
|
3296
|
+
}
|
|
3297
|
+
|
|
3298
|
+
fn expectFacadeRefusal(arena: std.mem.Allocator, json: []const u8, fragment: []const u8) !void {
|
|
3299
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
3300
|
+
const parsed = try sidecar_mod.read(arena, try withCurrentFacadeFacts(arena, json), &diags);
|
|
3301
|
+
try testing.expectError(error.Refused, emitFacade(arena, parsed, &diags));
|
|
3302
|
+
for (diags.list.items) |item| {
|
|
3303
|
+
if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, fragment) != null) return;
|
|
3304
|
+
}
|
|
3305
|
+
std.debug.print("no refusal containing \"{s}\"; got:\n", .{fragment});
|
|
3306
|
+
for (diags.list.items) |item| {
|
|
3307
|
+
std.debug.print(" [{s}] {s}: {s}\n", .{ @tagName(item.severity), item.path, item.message });
|
|
3308
|
+
}
|
|
3309
|
+
return error.TestExpectedRefusal;
|
|
3310
|
+
}
|
|
3311
|
+
|
|
3312
|
+
test "legacy sidecars remain mirror-readable but refuse facade projection" {
|
|
3313
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3314
|
+
defer arena_state.deinit();
|
|
3315
|
+
const arena = arena_state.allocator();
|
|
3316
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
3317
|
+
const parsed = try sidecar_mod.read(arena, sidecar_mod.minimal_valid_json, &diags);
|
|
3318
|
+
try testing.expectError(error.Refused, emitFacade(arena, parsed, &diags));
|
|
3319
|
+
var taught_origin = false;
|
|
3320
|
+
var taught_member = false;
|
|
3321
|
+
for (diags.list.items) |item| {
|
|
3322
|
+
if (std.mem.indexOf(u8, item.message, "type-origin fact") != null) taught_origin = true;
|
|
3323
|
+
if (std.mem.indexOf(u8, item.message, "member-name fact") != null) taught_member = true;
|
|
3324
|
+
}
|
|
3325
|
+
try testing.expect(taught_origin);
|
|
3326
|
+
try testing.expect(taught_member);
|
|
3327
|
+
}
|
|
3328
|
+
|
|
3329
|
+
test "facade emission is deterministic and carries the adapter surface" {
|
|
3330
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3331
|
+
defer arena_state.deinit();
|
|
3332
|
+
const arena = arena_state.allocator();
|
|
3333
|
+
const first = try facadeFromJson(arena, sidecar_mod.minimal_valid_json);
|
|
3334
|
+
const second = try facadeFromJson(arena, sidecar_mod.minimal_valid_json);
|
|
3335
|
+
try testing.expectEqualStrings(first, second);
|
|
3336
|
+
// The author's behavioral exports import under nscf aliases from the
|
|
3337
|
+
// entry module; the contract types re-export from it.
|
|
3338
|
+
try testing.expect(std.mem.indexOf(u8, first, "initialModel as nscfInitialModel,") != null);
|
|
3339
|
+
try testing.expect(std.mem.indexOf(u8, first, "update as nscfUpdate,") != null);
|
|
3340
|
+
try testing.expect(std.mem.indexOf(u8, first, "} from \"./core.ts\";") != null);
|
|
3341
|
+
try testing.expect(std.mem.indexOf(u8, first, "export type { Model, Msg } from \"./core.ts\";") != null);
|
|
3342
|
+
// The designated shape-flag entries restate the contract's flags.
|
|
3343
|
+
try testing.expect(std.mem.indexOf(u8, first, "export function init(): Model {") != null);
|
|
3344
|
+
try testing.expect(std.mem.indexOf(u8, first, "export function coreUpdate(model: Model, msg: Msg): [Model, Uint8Array] {") != null);
|
|
3345
|
+
try testing.expect(std.mem.indexOf(u8, first, "coreSubscriptions") == null);
|
|
3346
|
+
// The dispatch surface: committed state, tag table, arm routing.
|
|
3347
|
+
try testing.expect(std.mem.indexOf(u8, first, "let nscfCommitted: Model = nscfInitialModel();") != null);
|
|
3348
|
+
try testing.expect(std.mem.indexOf(u8, first, "const nscfTag_bump = 0;") != null);
|
|
3349
|
+
try testing.expect(std.mem.indexOf(u8, first, "if (tag === nscfTag_bump) return nscfCommit(coreUpdate(nscfCommitted, { kind: \"bump\" }));") != null);
|
|
3350
|
+
try testing.expect(std.mem.indexOf(u8, first, "if (tag === nscfTag_label_set) return nscfCommit(coreUpdate(nscfCommitted, { kind: \"label_set\", value: payload }));") != null);
|
|
3351
|
+
// Post-cycle: the snapshot rides the generated model writer with
|
|
3352
|
+
// the attested integer class.
|
|
3353
|
+
try testing.expect(std.mem.indexOf(u8, first, "nscfWI64(sink, value.count);") != null);
|
|
3354
|
+
try testing.expect(std.mem.indexOf(u8, first, "export function abi_subscriptions(): Uint8Array {") != null);
|
|
3355
|
+
try testing.expect(std.mem.indexOf(u8, first, "export function subscriptions(): Uint8Array {") == null);
|
|
3356
|
+
try testing.expect(std.mem.indexOf(u8, first, "export function model_snapshot(): Uint8Array {") != null);
|
|
3357
|
+
// The unbound list restates the author's markings.
|
|
3358
|
+
try testing.expect(std.mem.indexOf(u8, first, "export const viewUnbound = [\n \"label_set\",\n];") != null);
|
|
3359
|
+
// Unwired channels stay out of the module.
|
|
3360
|
+
try testing.expect(std.mem.indexOf(u8, first, "abi_frame_msg") == null);
|
|
3361
|
+
try testing.expect(std.mem.indexOf(u8, first, "nscfPackMsg") == null);
|
|
3362
|
+
// Facade-only types, tag constants, and SDK effect imports all live in
|
|
3363
|
+
// the lowercase reserved namespace, so authored capitalized names cannot
|
|
3364
|
+
// collide with them.
|
|
3365
|
+
try testing.expect(std.mem.indexOf(u8, first, "type nscfSink = number[];") != null);
|
|
3366
|
+
try testing.expect(std.mem.indexOf(u8, first, "import type { Cmd as nscfCmd }") != null);
|
|
3367
|
+
try testing.expect(std.mem.indexOf(u8, first, "NscfSink") == null);
|
|
3368
|
+
try testing.expect(std.mem.indexOf(u8, first, "NSCF_TAG_") == null);
|
|
3369
|
+
}
|
|
3370
|
+
|
|
3371
|
+
test "member facts spell the author's payload property names" {
|
|
3372
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3373
|
+
defer arena_state.deinit();
|
|
3374
|
+
const arena = arena_state.allocator();
|
|
3375
|
+
var source = try std.mem.replaceOwned(
|
|
3376
|
+
u8,
|
|
3377
|
+
arena,
|
|
3378
|
+
sidecar_mod.minimal_valid_json,
|
|
3379
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
3380
|
+
"{\"name\": \"toggle\", \"member\": \"taskId\", \"payload\": {\"kind\": \"number\", \"class\": \"i64\"}}",
|
|
3381
|
+
);
|
|
3382
|
+
source = try std.mem.replaceOwned(u8, arena, source, "{\"slot\": \"Model.count\", \"class\": \"i64\"}", "{\"slot\": \"Model.count\", \"class\": \"i64\"}, {\"slot\": \"Msg.toggle\", \"class\": \"i64\"}");
|
|
3383
|
+
const generated = try facadeFromJson(arena, source);
|
|
3384
|
+
// The integer-classed arm proves in place with the authored member
|
|
3385
|
+
// spelling at the write.
|
|
3386
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (value >= -9007199254740991 && value <= 9007199254740991) {") != null);
|
|
3387
|
+
try testing.expect(std.mem.indexOf(u8, generated, "const whole = Math.trunc(value);") != null);
|
|
3388
|
+
try testing.expect(std.mem.indexOf(u8, generated, "{ kind: \"toggle\", taskId: whole }") != null);
|
|
3389
|
+
// The baseline's own member fact remains authoritative too.
|
|
3390
|
+
try testing.expect(std.mem.indexOf(u8, generated, "{ kind: \"label_set\", value: payload }") != null);
|
|
3391
|
+
}
|
|
3392
|
+
|
|
3393
|
+
test "origin facts group re-exports by declaring module" {
|
|
3394
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3395
|
+
defer arena_state.deinit();
|
|
3396
|
+
const arena = arena_state.allocator();
|
|
3397
|
+
const source = try std.mem.replaceOwned(
|
|
3398
|
+
u8,
|
|
3399
|
+
arena,
|
|
3400
|
+
sidecar_mod.minimal_valid_json,
|
|
3401
|
+
"\"structs\": [",
|
|
3402
|
+
"\"structs\": [\n {\"name\": \"Turn\", \"origin\": \"domain/api.ts\", \"fields\": [{\"name\": \"id\", \"type\": {\"kind\": \"f64\"}}]},",
|
|
3403
|
+
);
|
|
3404
|
+
const with_field = try std.mem.replaceOwned(
|
|
3405
|
+
u8,
|
|
3406
|
+
arena,
|
|
3407
|
+
source,
|
|
3408
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
3409
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"node\", \"name\": \"Turn\"}}",
|
|
3410
|
+
);
|
|
3411
|
+
const generated = try facadeFromJson(arena, with_field);
|
|
3412
|
+
try testing.expect(std.mem.indexOf(u8, generated, "export type { Turn } from \"./domain/api.ts\";") != null);
|
|
3413
|
+
try testing.expect(std.mem.indexOf(u8, generated, "export type { Model, Msg } from \"./core.ts\";") != null);
|
|
3414
|
+
// The generated snapshot writer reaches the record through its
|
|
3415
|
+
// named writer, importing the type from its own module.
|
|
3416
|
+
try testing.expect(std.mem.indexOf(u8, generated, "import type { Turn } from \"./domain/api.ts\";") != null);
|
|
3417
|
+
try testing.expect(std.mem.indexOf(u8, generated, "function nscfWriteTurn(sink: nscfSink, value: Turn): void {") != null);
|
|
3418
|
+
}
|
|
3419
|
+
|
|
3420
|
+
test "private reachable types get structural twins instead of invalid imports" {
|
|
3421
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3422
|
+
defer arena_state.deinit();
|
|
3423
|
+
const arena = arena_state.allocator();
|
|
3424
|
+
var source = try std.mem.replaceOwned(
|
|
3425
|
+
u8,
|
|
3426
|
+
arena,
|
|
3427
|
+
sidecar_mod.minimal_valid_json,
|
|
3428
|
+
"\"structs\": [",
|
|
3429
|
+
"\"structs\": [\n {\"name\": \"Hidden\", \"origin\": \"core.ts\", \"exported\": false, \"fields\": [{\"name\": \"value\", \"type\": {\"kind\": \"f64\"}}, {\"name\": \"mode\", \"type\": {\"kind\": \"enum\", \"name\": \"HiddenMode\"}}, {\"name\": \"state\", \"type\": {\"kind\": \"union\", \"name\": \"HiddenState\"}}]},",
|
|
3430
|
+
);
|
|
3431
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"enums\": []", "\"enums\": [{\"name\": \"HiddenMode\", \"origin\": \"core.ts\", \"exported\": false, \"members\": [\"one\", \"two\"]}]");
|
|
3432
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"unions\": []", "\"unions\": [{\"name\": \"HiddenState\", \"origin\": \"core.ts\", \"exported\": false, \"arms\": [{\"name\": \"off\", \"payload\": {\"kind\": \"void\"}}, {\"name\": \"on\", \"member\": \"level\", \"payload\": {\"kind\": \"f64\"}}]}]");
|
|
3433
|
+
source = try std.mem.replaceOwned(
|
|
3434
|
+
u8,
|
|
3435
|
+
arena,
|
|
3436
|
+
source,
|
|
3437
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
3438
|
+
"{\"name\": \"hidden\", \"type\": {\"kind\": \"node\", \"name\": \"Hidden\"}}",
|
|
3439
|
+
);
|
|
3440
|
+
const generated = try facadeFromJson(arena, source);
|
|
3441
|
+
try testing.expect(std.mem.indexOf(u8, generated, "import type { Model, Msg } from \"./core.ts\";") != null);
|
|
3442
|
+
try testing.expect(std.mem.indexOf(u8, generated, "import type { Model, Msg, Hidden }") == null);
|
|
3443
|
+
try testing.expect(std.mem.indexOf(u8, generated, "export type { Model, Msg, Hidden }") == null);
|
|
3444
|
+
try testing.expect(std.mem.indexOf(u8, generated, "type Hidden = {\n readonly value: number;\n readonly mode: HiddenMode;\n readonly state: HiddenState;\n};") != null);
|
|
3445
|
+
try testing.expect(std.mem.indexOf(u8, generated, "type HiddenMode = \"one\" | \"two\";") != null);
|
|
3446
|
+
try testing.expect(std.mem.indexOf(u8, generated, "type HiddenState =\n | { readonly kind: \"off\" }\n | { readonly kind: \"on\"; readonly level: number }") != null);
|
|
3447
|
+
try testing.expect(std.mem.indexOf(u8, generated, "function nscfWriteHidden(sink: nscfSink, value: Hidden): void {") != null);
|
|
3448
|
+
}
|
|
3449
|
+
|
|
3450
|
+
test "generated generic table names refuse before emitting invalid imports" {
|
|
3451
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3452
|
+
defer arena_state.deinit();
|
|
3453
|
+
const arena = arena_state.allocator();
|
|
3454
|
+
var source = try std.mem.replaceOwned(
|
|
3455
|
+
u8,
|
|
3456
|
+
arena,
|
|
3457
|
+
sidecar_mod.minimal_valid_json,
|
|
3458
|
+
"{\"name\": \"Model\", \"fields\": [",
|
|
3459
|
+
"{\"name\": \"Model\", \"origin\": \"core.ts\", \"fields\": [",
|
|
3460
|
+
);
|
|
3461
|
+
source = try std.mem.replaceOwned(
|
|
3462
|
+
u8,
|
|
3463
|
+
arena,
|
|
3464
|
+
source,
|
|
3465
|
+
"\"structs\": [",
|
|
3466
|
+
"\"structs\": [\n {\"name\": \"Item\", \"origin\": \"core.ts\", \"fields\": [{\"name\": \"value\", \"type\": {\"kind\": \"f64\"}}]},\n {\"name\": \"Box__Item\", \"fields\": [{\"name\": \"item\", \"type\": {\"kind\": \"node\", \"name\": \"Item\"}}]},",
|
|
3467
|
+
);
|
|
3468
|
+
source = try std.mem.replaceOwned(
|
|
3469
|
+
u8,
|
|
3470
|
+
arena,
|
|
3471
|
+
source,
|
|
3472
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
3473
|
+
"{\"name\": \"box\", \"type\": {\"kind\": \"node\", \"name\": \"Box__Item\"}}",
|
|
3474
|
+
);
|
|
3475
|
+
// The monomorphized name is not an authored export, and the external
|
|
3476
|
+
// library compiler cannot project the generic contract field either.
|
|
3477
|
+
// Refuse here instead of generating `import type { Box__Item }` and
|
|
3478
|
+
// deferring failure to the TypeScript/compiler boundary.
|
|
3479
|
+
try expectFacadeRefusal(arena, source, "generic instantiations cannot cross the compiled-core contract surface yet");
|
|
3480
|
+
}
|
|
3481
|
+
|
|
3482
|
+
test "helpers wrap in declaration order with classed-return proofs" {
|
|
3483
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3484
|
+
defer arena_state.deinit();
|
|
3485
|
+
const arena = arena_state.allocator();
|
|
3486
|
+
var source = try std.mem.replaceOwned(
|
|
3487
|
+
u8,
|
|
3488
|
+
arena,
|
|
3489
|
+
sidecar_mod.minimal_valid_json,
|
|
3490
|
+
"\"model_helpers\": []",
|
|
3491
|
+
"\"model_helpers\": [{\"name\": \"summary\", \"params\": [], \"returns\": {\"kind\": \"bytes\"}, \"arena\": false}, {\"name\": \"rowCount\", \"params\": [], \"returns\": {\"kind\": \"i64\"}, \"arena\": false}]",
|
|
3492
|
+
);
|
|
3493
|
+
source = try std.mem.replaceOwned(u8, arena, source, "{\"slot\": \"Model.count\", \"class\": \"i64\"}", "{\"slot\": \"Model.count\", \"class\": \"i64\"}, {\"slot\": \"helpers.rowCount.return\", \"class\": \"i64\"}");
|
|
3494
|
+
const generated = try facadeFromJson(arena, source);
|
|
3495
|
+
try testing.expect(std.mem.indexOf(u8, generated, "summary as nscfH_summary,") != null);
|
|
3496
|
+
try testing.expect(std.mem.indexOf(u8, generated, "export function summary(model: Model): Uint8Array {") != null);
|
|
3497
|
+
// The classed return binds, guards, and truncs at the boundary.
|
|
3498
|
+
try testing.expect(std.mem.indexOf(u8, generated, "export function rowCount(model: Model): number {") != null);
|
|
3499
|
+
try testing.expect(std.mem.indexOf(u8, generated, "const nscfValue = nscfH_rowCount(model);") != null);
|
|
3500
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (nscfValue >= -9007199254740991 && nscfValue <= 9007199254740991) return Math.trunc(nscfValue);") != null);
|
|
3501
|
+
// helper_call indexes the declaration order and encodes each result
|
|
3502
|
+
// in its declared return encoding.
|
|
3503
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (helper === 0) {") != null);
|
|
3504
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (helper === 1) {") != null);
|
|
3505
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfWBytes(sink, nscfValue);") != null);
|
|
3506
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfWI64(sink, nscfValue);") != null);
|
|
3507
|
+
}
|
|
3508
|
+
|
|
3509
|
+
test "nullable integer helper returns prove their present branch" {
|
|
3510
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3511
|
+
defer arena_state.deinit();
|
|
3512
|
+
const arena = arena_state.allocator();
|
|
3513
|
+
var source = try std.mem.replaceOwned(
|
|
3514
|
+
u8,
|
|
3515
|
+
arena,
|
|
3516
|
+
sidecar_mod.minimal_valid_json,
|
|
3517
|
+
"\"model_helpers\": []",
|
|
3518
|
+
"\"model_helpers\": [{\"name\": \"maybeCount\", \"params\": [], \"returns\": {\"kind\": \"optional\", \"inner\": {\"kind\": \"i64\"}}, \"arena\": false}]",
|
|
3519
|
+
);
|
|
3520
|
+
source = try std.mem.replaceOwned(u8, arena, source, "{\"slot\": \"Model.count\", \"class\": \"i64\"}", "{\"slot\": \"Model.count\", \"class\": \"i64\"}, {\"slot\": \"helpers.maybeCount.return\", \"class\": \"i64\"}");
|
|
3521
|
+
const generated = try facadeFromJson(arena, source);
|
|
3522
|
+
try testing.expect(std.mem.indexOf(u8, generated, "export function maybeCount(model: Model): number | null {") != null);
|
|
3523
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (nscfValue === null) return null;") != null);
|
|
3524
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (nscfValue >= -9007199254740991 && nscfValue <= 9007199254740991) return Math.trunc(nscfValue);") != null);
|
|
3525
|
+
}
|
|
3526
|
+
|
|
3527
|
+
test "wired channels emit abi entries and the generic payload packer" {
|
|
3528
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3529
|
+
defer arena_state.deinit();
|
|
3530
|
+
const arena = arena_state.allocator();
|
|
3531
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"key_msg\": false", "\"key_msg\": true");
|
|
3532
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"frame_msg\": false", "\"frame_msg\": true");
|
|
3533
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"helper_call\"]", "\"helper_call\", \"frame_msg\", \"key_msg\"]");
|
|
3534
|
+
const generated = try facadeFromJson(arena, source);
|
|
3535
|
+
try testing.expect(std.mem.indexOf(u8, generated, "frameMsg as nscfChanFrameMsg,") != null);
|
|
3536
|
+
try testing.expect(std.mem.indexOf(u8, generated, "keyMsg as nscfChanKeyMsg,") != null);
|
|
3537
|
+
// The frame gate receives the committed model; the key gate the
|
|
3538
|
+
// event record with the wire's 0-or-1 modifier conversion.
|
|
3539
|
+
try testing.expect(std.mem.indexOf(u8, generated, "export function abi_frame_msg(width: number, height: number, timestampMs: number, intervalMs: number): Uint8Array {") != null);
|
|
3540
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfPackMsg(nscfChanFrameMsg(nscfCommitted, {") != null);
|
|
3541
|
+
try testing.expect(std.mem.indexOf(u8, generated, "shift: shift !== 0,") != null);
|
|
3542
|
+
// The envelope: [produced u8][tag u8][canonical payload].
|
|
3543
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (produced === null) return new Uint8Array(2);") != null);
|
|
3544
|
+
try testing.expect(std.mem.indexOf(u8, generated, "out[1] = nscfTagOf(produced.kind);") != null);
|
|
3545
|
+
try testing.expect(std.mem.indexOf(u8, generated, "function nscfMsgPayload(sink: nscfSink, value: Msg): void {") != null);
|
|
3546
|
+
// The unwired channels stay out.
|
|
3547
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi_pinch_msg") == null);
|
|
3548
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi_command_msg") == null);
|
|
3549
|
+
}
|
|
3550
|
+
|
|
3551
|
+
test "scalar primitive message payload aliases share dispatch and channel encoders" {
|
|
3552
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3553
|
+
defer arena_state.deinit();
|
|
3554
|
+
const arena = arena_state.allocator();
|
|
3555
|
+
var source = try std.mem.replaceOwned(
|
|
3556
|
+
u8,
|
|
3557
|
+
arena,
|
|
3558
|
+
sidecar_mod.minimal_valid_json,
|
|
3559
|
+
"{\"name\": \"label_set\", \"payload\": {\"kind\": \"bytes\"}}",
|
|
3560
|
+
"{\"name\": \"blob\", \"member\": \"blob\", \"payload\": {\"kind\": \"scalar\", \"type\": {\"kind\": \"bytes\"}}}, {\"name\": \"ratio\", \"member\": \"ratio\", \"payload\": {\"kind\": \"scalar\", \"type\": {\"kind\": \"f64\"}}}, {\"name\": \"count_set\", \"member\": \"count\", \"payload\": {\"kind\": \"scalar\", \"type\": {\"kind\": \"i64\"}}}",
|
|
3561
|
+
);
|
|
3562
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"unbound\": [\"label_set\"]", "\"unbound\": []");
|
|
3563
|
+
source = try std.mem.replaceOwned(u8, arena, source, "{\"slot\": \"Model.count\", \"class\": \"i64\"}", "{\"slot\": \"Model.count\", \"class\": \"i64\"}, {\"slot\": \"Msg.count_set\", \"class\": \"i64\"}");
|
|
3564
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"key_msg\": false", "\"key_msg\": true");
|
|
3565
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"helper_call\"]", "\"helper_call\", \"key_msg\"]");
|
|
3566
|
+
|
|
3567
|
+
const generated = try facadeFromJson(arena, source);
|
|
3568
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (tag === nscfTag_blob) return nscfCommit(coreUpdate(nscfCommitted, { kind: \"blob\", blob: payload }));") != null);
|
|
3569
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (tag === nscfTag_ratio) return nscfCommit(coreUpdate(nscfCommitted, { kind: \"ratio\", ratio: value }));") != null);
|
|
3570
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (tag === nscfTag_count_set) {") != null);
|
|
3571
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfWBytes(sink, value.blob);") != null);
|
|
3572
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfWF64(sink, value.ratio);") != null);
|
|
3573
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfWI64(sink, value.count);") != null);
|
|
3574
|
+
}
|
|
3575
|
+
|
|
3576
|
+
test "u64-attested slots ride the unsigned writer" {
|
|
3577
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3578
|
+
defer arena_state.deinit();
|
|
3579
|
+
const arena = arena_state.allocator();
|
|
3580
|
+
const source = try std.mem.replaceOwned(
|
|
3581
|
+
u8,
|
|
3582
|
+
arena,
|
|
3583
|
+
sidecar_mod.minimal_valid_json,
|
|
3584
|
+
"{\"slot\": \"Model.count\", \"class\": \"i64\"}",
|
|
3585
|
+
"{\"slot\": \"Model.count\", \"class\": \"u64\"}",
|
|
3586
|
+
);
|
|
3587
|
+
const generated = try facadeFromJson(arena, source);
|
|
3588
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfWU64(sink, value.count);") != null);
|
|
3589
|
+
try testing.expect(std.mem.indexOf(u8, generated, "function nscfWU64(sink: nscfSink, value: number): void {") != null);
|
|
3590
|
+
const signed = try facadeFromJson(arena, sidecar_mod.minimal_valid_json);
|
|
3591
|
+
try testing.expect(std.mem.indexOf(u8, signed, "nscfWU64") == null);
|
|
3592
|
+
}
|
|
3593
|
+
|
|
3594
|
+
test "u64 attestations set nonnegative proofs on every ingress shape" {
|
|
3595
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3596
|
+
defer arena_state.deinit();
|
|
3597
|
+
const arena = arena_state.allocator();
|
|
3598
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"structs\": [", "\"structs\": [\n {\"name\": \"Payload\", \"origin\": \"core.ts\", \"fields\": [{\"name\": \"id\", \"type\": {\"kind\": \"i64\"}}]},");
|
|
3599
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"model_helpers\": []", "\"model_helpers\": [{\"name\": \"nextId\", \"params\": [], \"returns\": {\"kind\": \"i64\"}, \"arena\": false}]");
|
|
3600
|
+
source = try std.mem.replaceOwned(
|
|
3601
|
+
u8,
|
|
3602
|
+
arena,
|
|
3603
|
+
source,
|
|
3604
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
3605
|
+
"{\"name\": \"id_set\", \"member\": \"value\", \"payload\": {\"kind\": \"number\", \"class\": \"i64\"}}, {\"name\": \"loaded\", \"member\": \"payload\", \"payload\": {\"kind\": \"record\", \"name\": \"Payload\"}}",
|
|
3606
|
+
);
|
|
3607
|
+
source = try std.mem.replaceOwned(u8, arena, source, "{\"name\": \"label_set\", \"payload\": {\"kind\": \"bytes\"}}", "{\"name\": \"sized\", \"payload\": {\"kind\": \"number_bytes\", \"number_field\": \"size\", \"number_class\": \"i64\", \"bytes_field\": \"label\"}}");
|
|
3608
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"unbound\": [\"label_set\"]", "\"unbound\": [\"id_set\", \"loaded\", \"sized\"]");
|
|
3609
|
+
source = try std.mem.replaceOwned(
|
|
3610
|
+
u8,
|
|
3611
|
+
arena,
|
|
3612
|
+
source,
|
|
3613
|
+
"{\"slot\": \"Model.count\", \"class\": \"i64\"}",
|
|
3614
|
+
"{\"slot\": \"Model.count\", \"class\": \"i64\"}, {\"slot\": \"Msg.id_set\", \"class\": \"u64\"}, {\"slot\": \"Msg.sized.size\", \"class\": \"u64\"}, {\"slot\": \"Payload.id\", \"class\": \"u64\"}, {\"slot\": \"helpers.nextId.return\", \"class\": \"u64\"}",
|
|
3615
|
+
);
|
|
3616
|
+
const generated = try facadeFromJson(arena, source);
|
|
3617
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (tag === nscfTag_id_set) {\n if (value >= 0 && value <= 9007199254740991) {") != null);
|
|
3618
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (tag === nscfTag_sized) {\n // The number field is i64-classed") != null);
|
|
3619
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (value >= 0 && value <= 9007199254740991) {\n return nscfCommit(coreUpdate(nscfCommitted, { kind: \"sized\"") != null);
|
|
3620
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (nscfValue >= 0 && nscfValue <= 9007199254740991) return Math.trunc(nscfValue);") != null);
|
|
3621
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (nscfV0 >= 0 && nscfV0 <= 9007199254740991) {") != null);
|
|
3622
|
+
}
|
|
3623
|
+
|
|
3624
|
+
test "an authored pattern-named record keeps its member and re-export" {
|
|
3625
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3626
|
+
defer arena_state.deinit();
|
|
3627
|
+
const arena = arena_state.allocator();
|
|
3628
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"structs\": [", "\"structs\": [\n {\"name\": \"Msg_loaded\", \"origin\": \"core.ts\", \"fields\": [{\"name\": \"status\", \"type\": {\"kind\": \"f64\"}}, {\"name\": \"ok\", \"type\": {\"kind\": \"bool\"}}]},");
|
|
3629
|
+
source = try std.mem.replaceOwned(
|
|
3630
|
+
u8,
|
|
3631
|
+
arena,
|
|
3632
|
+
source,
|
|
3633
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
3634
|
+
"{\"name\": \"loaded\", \"member\": \"payload\", \"payload\": {\"kind\": \"record\", \"name\": \"Msg_loaded\"}}",
|
|
3635
|
+
);
|
|
3636
|
+
const generated = try facadeFromJson(arena, source);
|
|
3637
|
+
try testing.expect(std.mem.indexOf(u8, generated, "export type { Model, Msg, Msg_loaded } from \"./core.ts\";") != null);
|
|
3638
|
+
try testing.expect(std.mem.indexOf(u8, generated, "{ kind: \"loaded\", payload: { status:") != null);
|
|
3639
|
+
try testing.expect(std.mem.indexOf(u8, generated, "{ kind: \"loaded\", status:") == null);
|
|
3640
|
+
}
|
|
3641
|
+
|
|
3642
|
+
test "a helper taking a fixed export's name refuses" {
|
|
3643
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3644
|
+
defer arena_state.deinit();
|
|
3645
|
+
const arena = arena_state.allocator();
|
|
3646
|
+
const source = try std.mem.replaceOwned(
|
|
3647
|
+
u8,
|
|
3648
|
+
arena,
|
|
3649
|
+
sidecar_mod.minimal_valid_json,
|
|
3650
|
+
"\"model_helpers\": []",
|
|
3651
|
+
"\"model_helpers\": [{\"name\": \"dispatch_void\", \"params\": [], \"returns\": {\"kind\": \"bytes\"}, \"arena\": false}]",
|
|
3652
|
+
);
|
|
3653
|
+
try expectFacadeRefusal(arena, source, "collides with a declaration the generated facade itself must export");
|
|
3654
|
+
}
|
|
3655
|
+
|
|
3656
|
+
test "capitalized authored names no longer collide with facade internals" {
|
|
3657
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3658
|
+
defer arena_state.deinit();
|
|
3659
|
+
const arena = arena_state.allocator();
|
|
3660
|
+
var source = try std.mem.replaceOwned(
|
|
3661
|
+
u8,
|
|
3662
|
+
arena,
|
|
3663
|
+
sidecar_mod.minimal_valid_json,
|
|
3664
|
+
"\"structs\": [",
|
|
3665
|
+
"\"structs\": [\n {\"name\": \"NscfSink\", \"origin\": \"core.ts\", \"fields\": [{\"name\": \"id\", \"type\": {\"kind\": \"f64\"}}]},\n {\"name\": \"Cmd\", \"origin\": \"core.ts\", \"fields\": [{\"name\": \"id\", \"type\": {\"kind\": \"f64\"}}]},",
|
|
3666
|
+
);
|
|
3667
|
+
source = try std.mem.replaceOwned(
|
|
3668
|
+
u8,
|
|
3669
|
+
arena,
|
|
3670
|
+
source,
|
|
3671
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
3672
|
+
"{\"name\": \"sinkValue\", \"type\": {\"kind\": \"node\", \"name\": \"NscfSink\"}}, {\"name\": \"commandValue\", \"type\": {\"kind\": \"node\", \"name\": \"Cmd\"}}",
|
|
3673
|
+
);
|
|
3674
|
+
source = try std.mem.replaceOwned(
|
|
3675
|
+
u8,
|
|
3676
|
+
arena,
|
|
3677
|
+
source,
|
|
3678
|
+
"\"model_helpers\": []",
|
|
3679
|
+
"\"model_helpers\": [{\"name\": \"NSCF_TAG_bump\", \"params\": [], \"returns\": {\"kind\": \"bytes\"}, \"arena\": false}]",
|
|
3680
|
+
);
|
|
3681
|
+
const generated = try facadeFromJson(arena, source);
|
|
3682
|
+
try testing.expect(std.mem.indexOf(u8, generated, "import type { Model, Msg, NscfSink, Cmd } from \"./core.ts\";") != null);
|
|
3683
|
+
try testing.expect(std.mem.indexOf(u8, generated, "import type { Cmd as nscfCmd } from \"./sdk/core.ts\";") != null);
|
|
3684
|
+
try testing.expect(std.mem.indexOf(u8, generated, "export function NSCF_TAG_bump(model: Model): Uint8Array {") != null);
|
|
3685
|
+
try testing.expect(std.mem.indexOf(u8, generated, "const nscfTag_bump = 0;") != null);
|
|
3686
|
+
try testing.expect(std.mem.indexOf(u8, generated, "type nscfSink = number[];") != null);
|
|
3687
|
+
}
|
|
3688
|
+
|
|
3689
|
+
test "a helper may not shadow an ambient facade value" {
|
|
3690
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3691
|
+
defer arena_state.deinit();
|
|
3692
|
+
const arena = arena_state.allocator();
|
|
3693
|
+
const source = try std.mem.replaceOwned(
|
|
3694
|
+
u8,
|
|
3695
|
+
arena,
|
|
3696
|
+
sidecar_mod.minimal_valid_json,
|
|
3697
|
+
"\"model_helpers\": []",
|
|
3698
|
+
"\"model_helpers\": [{\"name\": \"Math\", \"params\": [], \"returns\": {\"kind\": \"bytes\"}, \"arena\": false}]",
|
|
3699
|
+
);
|
|
3700
|
+
try expectFacadeRefusal(arena, source, "shadows an ambient value");
|
|
3701
|
+
}
|
|
3702
|
+
|
|
3703
|
+
test "a payload member spelled kind refuses against the discriminator" {
|
|
3704
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3705
|
+
defer arena_state.deinit();
|
|
3706
|
+
const arena = arena_state.allocator();
|
|
3707
|
+
const source = try std.mem.replaceOwned(
|
|
3708
|
+
u8,
|
|
3709
|
+
arena,
|
|
3710
|
+
sidecar_mod.minimal_valid_json,
|
|
3711
|
+
"{\"name\": \"label_set\", \"payload\": {\"kind\": \"bytes\"}}",
|
|
3712
|
+
"{\"name\": \"label_set\", \"member\": \"kind\", \"payload\": {\"kind\": \"bytes\"}}",
|
|
3713
|
+
);
|
|
3714
|
+
try expectFacadeRefusal(arena, source, "the discriminator's own spelling");
|
|
3715
|
+
}
|
|
3716
|
+
|
|
3717
|
+
test "a type in the facade's reserved nsc name space refuses" {
|
|
3718
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3719
|
+
defer arena_state.deinit();
|
|
3720
|
+
const arena = arena_state.allocator();
|
|
3721
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"enums\": []", "\"enums\": [{\"name\": \"nscfHelper\", \"members\": [\"a\", \"b\"]}]");
|
|
3722
|
+
source = try std.mem.replaceOwned(
|
|
3723
|
+
u8,
|
|
3724
|
+
arena,
|
|
3725
|
+
source,
|
|
3726
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
3727
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"enum\", \"name\": \"nscfHelper\"}}",
|
|
3728
|
+
);
|
|
3729
|
+
try expectFacadeRefusal(arena, source, "reserved nsc name space");
|
|
3730
|
+
}
|
|
3731
|
+
|
|
3732
|
+
test "an unbound message arm shadowed by a homonymous model field refuses" {
|
|
3733
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3734
|
+
defer arena_state.deinit();
|
|
3735
|
+
const arena = arena_state.allocator();
|
|
3736
|
+
var source = try std.mem.replaceOwned(
|
|
3737
|
+
u8,
|
|
3738
|
+
arena,
|
|
3739
|
+
sidecar_mod.minimal_valid_json,
|
|
3740
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
3741
|
+
"{\"name\": \"count\", \"payload\": {\"kind\": \"void\"}}",
|
|
3742
|
+
);
|
|
3743
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"unbound\": [\"label_set\"]", "\"unbound\": [\"count\"]");
|
|
3744
|
+
try expectFacadeRefusal(arena, source, "resolves Model fields and helpers before message arms");
|
|
3745
|
+
}
|
|
3746
|
+
|
|
3747
|
+
test "an unbound model field may share a name with a bound message arm" {
|
|
3748
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3749
|
+
defer arena_state.deinit();
|
|
3750
|
+
const arena = arena_state.allocator();
|
|
3751
|
+
var source = try std.mem.replaceOwned(
|
|
3752
|
+
u8,
|
|
3753
|
+
arena,
|
|
3754
|
+
sidecar_mod.minimal_valid_json,
|
|
3755
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
3756
|
+
"{\"name\": \"count\", \"payload\": {\"kind\": \"void\"}}",
|
|
3757
|
+
);
|
|
3758
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"model_unbound\": []", "\"model_unbound\": [\"count\"]");
|
|
3759
|
+
const generated = try facadeFromJson(arena, source);
|
|
3760
|
+
try testing.expect(std.mem.indexOf(u8, generated, "export const viewUnbound = [\n \"label_set\",\n \"count\",\n];") != null);
|
|
3761
|
+
}
|
|
3762
|
+
|
|
3763
|
+
test "an unbound message arm shadowed by a homonymous helper refuses" {
|
|
3764
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3765
|
+
defer arena_state.deinit();
|
|
3766
|
+
const arena = arena_state.allocator();
|
|
3767
|
+
var source = try std.mem.replaceOwned(
|
|
3768
|
+
u8,
|
|
3769
|
+
arena,
|
|
3770
|
+
sidecar_mod.minimal_valid_json,
|
|
3771
|
+
"\"model_helpers\": []",
|
|
3772
|
+
"\"model_helpers\": [{\"name\": \"bump\", \"params\": [], \"returns\": {\"kind\": \"bool\"}, \"arena\": false}]",
|
|
3773
|
+
);
|
|
3774
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"unbound\": [\"label_set\"]", "\"unbound\": [\"bump\"]");
|
|
3775
|
+
try expectFacadeRefusal(arena, source, "resolves Model fields and helpers before message arms");
|
|
3776
|
+
}
|
|
3777
|
+
|
|
3778
|
+
test "nested optionals refuse in the projection" {
|
|
3779
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3780
|
+
defer arena_state.deinit();
|
|
3781
|
+
const arena = arena_state.allocator();
|
|
3782
|
+
const source = try std.mem.replaceOwned(
|
|
3783
|
+
u8,
|
|
3784
|
+
arena,
|
|
3785
|
+
sidecar_mod.minimal_valid_json,
|
|
3786
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
3787
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"optional\", \"inner\": {\"kind\": \"optional\", \"inner\": {\"kind\": \"f64\"}}}}",
|
|
3788
|
+
);
|
|
3789
|
+
try expectFacadeRefusal(arena, source, "one absence level");
|
|
3790
|
+
}
|
|
3791
|
+
|
|
3792
|
+
test "a record referenced by node and value at once refuses" {
|
|
3793
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3794
|
+
defer arena_state.deinit();
|
|
3795
|
+
const arena = arena_state.allocator();
|
|
3796
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"structs\": [", "\"structs\": [\n {\"name\": \"Item\", \"fields\": [{\"name\": \"x\", \"type\": {\"kind\": \"f64\"}}]},");
|
|
3797
|
+
source = try std.mem.replaceOwned(
|
|
3798
|
+
u8,
|
|
3799
|
+
arena,
|
|
3800
|
+
source,
|
|
3801
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
3802
|
+
"{\"name\": \"live\", \"type\": {\"kind\": \"node\", \"name\": \"Item\"}}, {\"name\": \"cached\", \"type\": {\"kind\": \"value\", \"name\": \"Item\"}}",
|
|
3803
|
+
);
|
|
3804
|
+
try expectFacadeRefusal(arena, source, "storage once per declaration");
|
|
3805
|
+
}
|
|
3806
|
+
|
|
3807
|
+
test "record dispatch decodes nonterminal optionals and nullable composites" {
|
|
3808
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3809
|
+
defer arena_state.deinit();
|
|
3810
|
+
const arena = arena_state.allocator();
|
|
3811
|
+
var source = try std.mem.replaceOwned(
|
|
3812
|
+
u8,
|
|
3813
|
+
arena,
|
|
3814
|
+
sidecar_mod.minimal_valid_json,
|
|
3815
|
+
"\"structs\": [",
|
|
3816
|
+
"\"structs\": [\n {\"name\": \"Payload\", \"origin\": \"core.ts\", \"fields\": [{\"name\": \"maybe\", \"type\": {\"kind\": \"optional\", \"inner\": {\"kind\": \"f64\"}}}, {\"name\": \"tail\", \"type\": {\"kind\": \"f64\"}}, {\"name\": \"blob\", \"type\": {\"kind\": \"optional\", \"inner\": {\"kind\": \"bytes\"}}}]},",
|
|
3817
|
+
);
|
|
3818
|
+
source = try std.mem.replaceOwned(
|
|
3819
|
+
u8,
|
|
3820
|
+
arena,
|
|
3821
|
+
source,
|
|
3822
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
3823
|
+
"{\"name\": \"loaded\", \"member\": \"payload\", \"payload\": {\"kind\": \"record\", \"name\": \"Payload\"}}",
|
|
3824
|
+
);
|
|
3825
|
+
const generated = try facadeFromJson(arena, source);
|
|
3826
|
+
try testing.expect(std.mem.indexOf(u8, generated, "let nscfAt = 0;") != null);
|
|
3827
|
+
const optional_at = std.mem.indexOf(u8, generated, "let nscfV1: number | null = null;").?;
|
|
3828
|
+
const tail_at = std.mem.indexOfPos(u8, generated, optional_at, "nscfReadF64(fields, nscfAt)").?;
|
|
3829
|
+
try testing.expect(tail_at > optional_at);
|
|
3830
|
+
try testing.expect(std.mem.indexOf(u8, generated, "Uint8Array | null = null;") != null);
|
|
3831
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfReadBytesBody(fields, nscfAt,") != null);
|
|
3832
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfAssertConsumed(fields, nscfAt);") != null);
|
|
3833
|
+
}
|
|
3834
|
+
|
|
3835
|
+
test "record dispatch recursively decodes sequences and nested unions" {
|
|
3836
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3837
|
+
defer arena_state.deinit();
|
|
3838
|
+
const arena = arena_state.allocator();
|
|
3839
|
+
var source = try std.mem.replaceOwned(
|
|
3840
|
+
u8,
|
|
3841
|
+
arena,
|
|
3842
|
+
sidecar_mod.minimal_valid_json,
|
|
3843
|
+
"\"structs\": [",
|
|
3844
|
+
"\"structs\": [\n {\"name\": \"Payload\", \"origin\": \"core.ts\", \"fields\": [{\"name\": \"values\", \"type\": {\"kind\": \"slice\", \"elem\": {\"kind\": \"f64\"}}}, {\"name\": \"choice\", \"type\": {\"kind\": \"optional\", \"inner\": {\"kind\": \"union\", \"name\": \"Choice\"}}}]},",
|
|
3845
|
+
);
|
|
3846
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"unions\": []", "\"unions\": [{\"name\": \"Choice\", \"origin\": \"core.ts\", \"arms\": [{\"name\": \"none\", \"payload\": {\"kind\": \"void\"}}, {\"name\": \"text\", \"member\": \"value\", \"payload\": {\"kind\": \"bytes\"}}]}]");
|
|
3847
|
+
source = try std.mem.replaceOwned(
|
|
3848
|
+
u8,
|
|
3849
|
+
arena,
|
|
3850
|
+
source,
|
|
3851
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
3852
|
+
"{\"name\": \"loaded\", \"member\": \"payload\", \"payload\": {\"kind\": \"record\", \"name\": \"Payload\"}}",
|
|
3853
|
+
);
|
|
3854
|
+
const generated = try facadeFromJson(arena, source);
|
|
3855
|
+
try testing.expect(std.mem.indexOf(u8, generated, ": number[] = [];") != null);
|
|
3856
|
+
try testing.expect(std.mem.indexOf(u8, generated, ".push(") != null);
|
|
3857
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfReadU8(fields, nscfAt)") != null);
|
|
3858
|
+
try testing.expect(std.mem.indexOf(u8, generated, ": Choice | null = null;") != null);
|
|
3859
|
+
try testing.expect(std.mem.indexOf(u8, generated, "{ kind: \"text\", value:") != null);
|
|
3860
|
+
}
|
|
3861
|
+
|
|
3862
|
+
test "ordinary set_selection arms keep exact integer decoding" {
|
|
3863
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3864
|
+
defer arena_state.deinit();
|
|
3865
|
+
const arena = arena_state.allocator();
|
|
3866
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"structs\": [", "\"structs\": [\n {\"name\": \"CaretRange\", \"origin\": \"core.ts\", \"fields\": [{\"name\": \"anchor\", \"type\": {\"kind\": \"i64\"}}, {\"name\": \"focus\", \"type\": {\"kind\": \"i64\"}}]},");
|
|
3867
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"unions\": []", "\"unions\": [{\"name\": \"Edit\", \"origin\": \"core.ts\", \"arms\": [{\"name\": \"clear\", \"payload\": {\"kind\": \"void\"}}, {\"name\": \"set_selection\", \"member\": \"selection\", \"payload\": {\"kind\": \"value\", \"name\": \"CaretRange\"}}]}]");
|
|
3868
|
+
source = try std.mem.replaceOwned(
|
|
3869
|
+
u8,
|
|
3870
|
+
arena,
|
|
3871
|
+
source,
|
|
3872
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
3873
|
+
"{\"name\": \"edited\", \"member\": \"edit\", \"payload\": {\"kind\": \"union\", \"name\": \"Edit\"}}",
|
|
3874
|
+
);
|
|
3875
|
+
source = try std.mem.replaceOwned(u8, arena, source, "{\"slot\": \"Model.count\", \"class\": \"i64\"}", "{\"slot\": \"Model.count\", \"class\": \"i64\"}, {\"slot\": \"CaretRange.anchor\", \"class\": \"i64\"}, {\"slot\": \"CaretRange.focus\", \"class\": \"i64\"}");
|
|
3876
|
+
const generated = try facadeFromJson(arena, source);
|
|
3877
|
+
// The union decodes standalone on the record and text-input entries.
|
|
3878
|
+
try testing.expect(std.mem.indexOf(u8, generated, "function nscfDecodeEdit(bytes: Uint8Array): Edit {") != null);
|
|
3879
|
+
try testing.expect(std.mem.indexOf(u8, generated, "{ kind: \"edited\", edit: nscfDecodeEdit(fields) }") != null);
|
|
3880
|
+
try testing.expect(std.mem.indexOf(u8, generated, "{ kind: \"edited\", edit: nscfDecodeEdit(event) }") != null);
|
|
3881
|
+
// This is not the complete text-input protocol, so a homonymous arm
|
|
3882
|
+
// retains the ordinary exact reader and rejects out-of-window integers.
|
|
3883
|
+
try testing.expect(std.mem.indexOf(u8, generated, "let nscfAt = 1;") != null);
|
|
3884
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfReadI64(bytes, nscfAt)") != null);
|
|
3885
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfReadI64Saturating") == null);
|
|
3886
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfReadU64Saturating") == null);
|
|
3887
|
+
try testing.expect(std.mem.indexOf(u8, generated, "anchor: Math.trunc(") != null);
|
|
3888
|
+
}
|
|
3889
|
+
|
|
3890
|
+
test "text-input selection saturates signed and unsigned select-all sentinels" {
|
|
3891
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3892
|
+
defer arena_state.deinit();
|
|
3893
|
+
const arena = arena_state.allocator();
|
|
3894
|
+
const records =
|
|
3895
|
+
\\ {"name": "Move", "origin": "core.ts", "fields": [
|
|
3896
|
+
\\ {"name": "direction", "type": {"kind": "enum", "name": "Dir"}},
|
|
3897
|
+
\\ {"name": "extend", "type": {"kind": "bool"}}
|
|
3898
|
+
\\ ]},
|
|
3899
|
+
\\ {"name": "Sel", "origin": "core.ts", "fields": [
|
|
3900
|
+
\\ {"name": "anchor", "type": {"kind": "i64"}},
|
|
3901
|
+
\\ {"name": "focus", "type": {"kind": "i64"}}
|
|
3902
|
+
\\ ]},
|
|
3903
|
+
\\ {"name": "Comp", "origin": "core.ts", "fields": [
|
|
3904
|
+
\\ {"name": "text", "type": {"kind": "bytes"}},
|
|
3905
|
+
\\ {"name": "cursor", "type": {"kind": "optional", "inner": {"kind": "i64"}}}
|
|
3906
|
+
\\ ]},
|
|
3907
|
+
;
|
|
3908
|
+
const union_entry =
|
|
3909
|
+
\\"unions": [{"name": "Edit", "origin": "core.ts", "arms": [
|
|
3910
|
+
\\ {"name": "insert_text", "member": "text", "payload": {"kind": "bytes"}},
|
|
3911
|
+
\\ {"name": "delete_backward", "payload": {"kind": "void"}},
|
|
3912
|
+
\\ {"name": "delete_forward", "payload": {"kind": "void"}},
|
|
3913
|
+
\\ {"name": "delete_word_backward", "payload": {"kind": "void"}},
|
|
3914
|
+
\\ {"name": "delete_word_forward", "payload": {"kind": "void"}},
|
|
3915
|
+
\\ {"name": "clear", "payload": {"kind": "void"}},
|
|
3916
|
+
\\ {"name": "move_caret", "member": "move", "payload": {"kind": "value", "name": "Move"}},
|
|
3917
|
+
\\ {"name": "set_selection", "member": "selection", "payload": {"kind": "value", "name": "Sel"}},
|
|
3918
|
+
\\ {"name": "set_composition", "member": "composition", "payload": {"kind": "value", "name": "Comp"}},
|
|
3919
|
+
\\ {"name": "commit_composition", "payload": {"kind": "void"}},
|
|
3920
|
+
\\ {"name": "cancel_composition", "payload": {"kind": "void"}}
|
|
3921
|
+
\\ ]}]
|
|
3922
|
+
;
|
|
3923
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"structs\": [\n", try std.fmt.allocPrint(arena, "\"structs\": [\n{s}\n", .{records}));
|
|
3924
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"enums\": []", "\"enums\": [{\"name\": \"Dir\", \"origin\": \"core.ts\", \"members\": [\"previous\", \"next\", \"previous_word\", \"next_word\", \"start\", \"end\"]}]");
|
|
3925
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"unions\": []", union_entry);
|
|
3926
|
+
source = try std.mem.replaceOwned(u8, arena, source, "{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}", "{\"name\": \"edited\", \"member\": \"edit\", \"payload\": {\"kind\": \"union\", \"name\": \"Edit\"}}");
|
|
3927
|
+
source = try std.mem.replaceOwned(
|
|
3928
|
+
u8,
|
|
3929
|
+
arena,
|
|
3930
|
+
source,
|
|
3931
|
+
"{\"slot\": \"Model.count\", \"class\": \"i64\"}",
|
|
3932
|
+
"{\"slot\": \"Model.count\", \"class\": \"i64\"}, {\"slot\": \"Sel.anchor\", \"class\": \"u64\"}, {\"slot\": \"Sel.focus\", \"class\": \"i64\"}, {\"slot\": \"Comp.cursor\", \"class\": \"u64\"}",
|
|
3933
|
+
);
|
|
3934
|
+
const generated = try facadeFromJson(arena, source);
|
|
3935
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfReadU64Saturating(bytes, nscfAt)") != null);
|
|
3936
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfReadI64Saturating(bytes, nscfAt)") != null);
|
|
3937
|
+
try testing.expect(std.mem.indexOf(u8, generated, "function nscfReadU64Saturating(bytes: Uint8Array, at: number): number {") != null);
|
|
3938
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (hi > 2097151) return 9007199254740991;") != null);
|
|
3939
|
+
}
|
|
3940
|
+
|
|
3941
|
+
test "scroll-shaped record arms answer the dedicated scroll entry" {
|
|
3942
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3943
|
+
defer arena_state.deinit();
|
|
3944
|
+
const arena = arena_state.allocator();
|
|
3945
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"structs\": [", "\"structs\": [\n {\"name\": \"ScrollState\", \"origin\": \"core.ts\", \"fields\": [{\"name\": \"offsetX\", \"type\": {\"kind\": \"f64\"}}, {\"name\": \"offsetY\", \"type\": {\"kind\": \"f64\"}}, {\"name\": \"velocityX\", \"type\": {\"kind\": \"f64\"}}, {\"name\": \"velocityY\", \"type\": {\"kind\": \"f64\"}}, {\"name\": \"viewportExtentX\", \"type\": {\"kind\": \"f64\"}}, {\"name\": \"viewportExtentY\", \"type\": {\"kind\": \"f64\"}}, {\"name\": \"contentExtentX\", \"type\": {\"kind\": \"f64\"}}, {\"name\": \"contentExtentY\", \"type\": {\"kind\": \"f64\"}}]},");
|
|
3946
|
+
source = try std.mem.replaceOwned(
|
|
3947
|
+
u8,
|
|
3948
|
+
arena,
|
|
3949
|
+
source,
|
|
3950
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
3951
|
+
"{\"name\": \"scrolled\", \"member\": \"scroll\", \"payload\": {\"kind\": \"record\", \"name\": \"ScrollState\"}}",
|
|
3952
|
+
);
|
|
3953
|
+
const generated = try facadeFromJson(arena, source);
|
|
3954
|
+
// Both routes: the generic record decode and the flat-scalar entry.
|
|
3955
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (tag === nscfTag_scrolled) return nscfCommit(coreUpdate(nscfCommitted, { kind: \"scrolled\", scroll: { offsetX: offsetX, offsetY: offsetY, velocityX: velocityX, velocityY: velocityY, viewportExtentX: viewportExtentX, viewportExtentY: viewportExtentY, contentExtentX: contentExtentX, contentExtentY: contentExtentY } }));") != null);
|
|
3956
|
+
try testing.expect(std.mem.indexOf(u8, generated, "nscfUnknownTag(\"scroll-state\", tag);") != null);
|
|
3957
|
+
|
|
3958
|
+
var integer_source = try std.mem.replaceOwned(u8, arena, source, "{\"name\": \"offsetX\", \"type\": {\"kind\": \"f64\"}}", "{\"name\": \"offsetX\", \"type\": {\"kind\": \"i64\"}}");
|
|
3959
|
+
integer_source = try std.mem.replaceOwned(u8, arena, integer_source, "{\"name\": \"offsetY\", \"type\": {\"kind\": \"f64\"}}", "{\"name\": \"offsetY\", \"type\": {\"kind\": \"i64\"}}");
|
|
3960
|
+
integer_source = try std.mem.replaceOwned(u8, arena, integer_source, "{\"slot\": \"Model.count\", \"class\": \"i64\"}", "{\"slot\": \"Model.count\", \"class\": \"i64\"}, {\"slot\": \"ScrollState.offsetX\", \"class\": \"u64\"}, {\"slot\": \"ScrollState.offsetY\", \"class\": \"i64\"}");
|
|
3961
|
+
const integer_generated = try facadeFromJson(arena, integer_source);
|
|
3962
|
+
try testing.expect(std.mem.indexOf(u8, integer_generated, "offsetX >= 0 && offsetX <= 9007199254740991 && offsetY >= -9007199254740991 && offsetY <= 9007199254740991") != null);
|
|
3963
|
+
try testing.expect(std.mem.indexOf(u8, integer_generated, "offsetX: Math.trunc(offsetX), offsetY: Math.trunc(offsetY)") != null);
|
|
3964
|
+
|
|
3965
|
+
// The canvas snake_case vocabulary routes through the same ABI entry,
|
|
3966
|
+
// with each authored field fed by its corresponding camelCase ABI param.
|
|
3967
|
+
var snake_source = source;
|
|
3968
|
+
for (scroll_state_fields_ts, scroll_state_fields_canvas) |ts_name, canvas_name| {
|
|
3969
|
+
snake_source = try std.mem.replaceOwned(u8, arena, snake_source, ts_name, canvas_name);
|
|
3970
|
+
}
|
|
3971
|
+
const snake_generated = try facadeFromJson(arena, snake_source);
|
|
3972
|
+
try testing.expect(std.mem.indexOf(u8, snake_generated, "offset_x: offsetX, offset_y: offsetY, velocity_x: velocityX, velocity_y: velocityY") != null);
|
|
3973
|
+
|
|
3974
|
+
// A current sidecar's originless pattern-named arm record is the
|
|
3975
|
+
// compiler's inline shape: scroll construction flattens beside kind
|
|
3976
|
+
// rather than inventing a `value` payload member.
|
|
3977
|
+
var inline_source = try std.mem.replaceOwned(u8, arena, source, "{\"name\": \"Model\", \"fields\": [", "{\"name\": \"Model\", \"origin\": \"core.ts\", \"fields\": [");
|
|
3978
|
+
inline_source = try std.mem.replaceOwned(u8, arena, inline_source, "ScrollState", "Msg_scrolled");
|
|
3979
|
+
inline_source = try std.mem.replaceOwned(u8, arena, inline_source, "{\"name\": \"Msg_scrolled\", \"origin\": \"core.ts\"", "{\"name\": \"Msg_scrolled\"");
|
|
3980
|
+
inline_source = try std.mem.replaceOwned(u8, arena, inline_source, "\"member\": \"scroll\", ", "");
|
|
3981
|
+
const inline_generated = try facadeFromJson(arena, inline_source);
|
|
3982
|
+
try testing.expect(std.mem.indexOf(u8, inline_generated, "{ kind: \"scrolled\", offsetX: offsetX, offsetY: offsetY") != null);
|
|
3983
|
+
try testing.expect(std.mem.indexOf(u8, inline_generated, "{ kind: \"scrolled\", value:") == null);
|
|
3984
|
+
}
|