@native-sdk/cli 0.7.2 → 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 +265 -65
- package/build/ts_run.mjs +6 -8
- package/build.zig +292 -349
- 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/ts-core/SKILL.md +29 -29
- package/src/runtime/ts_core_host.zig +38 -38
- package/src/runtime/ts_ui_app.zig +12 -12
- package/src/tooling/manifest.zig +32 -0
- package/src/tooling/raw_manifest.zig +4 -0
- package/src/tooling/templates.zig +25 -20
- 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,2418 @@
|
|
|
1
|
+
//! core_shim.zig emission: turn a validated contract sidecar into the
|
|
2
|
+
//! Zig mirror module the app wiring imports in place of transpiler
|
|
3
|
+
//! output.
|
|
4
|
+
//!
|
|
5
|
+
//! The mirror is the load-bearing move of the whole design: every seam
|
|
6
|
+
//! that reflects over an app core at comptime — the markup engines, the
|
|
7
|
+
//! adapter's channel stamping, the bridge's tag table, the
|
|
8
|
+
//! model-contract emit — keeps its `@typeInfo` worldview because the
|
|
9
|
+
//! generated module declares REAL Zig types with the sidecar's field
|
|
10
|
+
//! names, declaration order, and type spellings. Fidelity rules, each
|
|
11
|
+
//! matching the transpiler's emission so the reflecting seams cannot
|
|
12
|
+
//! tell the two lanes apart:
|
|
13
|
+
//!
|
|
14
|
+
//! - bytes spell `[]const u8`; numbers spell `f64` or `i64` per the
|
|
15
|
+
//! sidecar's per-slot class — `u64` where the slot's integer_slots
|
|
16
|
+
//! attestation refines the i64 spelling to the unsigned class, so the
|
|
17
|
+
//! decoder reads the slot's 8 wire bytes unsigned; every non-attested
|
|
18
|
+
//! numeric slot stays f64. Enums are `enum(u8)` with explicit
|
|
19
|
+
//! declaration-order values; node references spell `*const T`.
|
|
20
|
+
//! - The message union is `union(enum)` in sidecar arm order — the arm
|
|
21
|
+
//! index IS the wire tag.
|
|
22
|
+
//! - Synthesized type-table names (the schema's `<Container>_<member>`
|
|
23
|
+
//! pattern for tabled anonymous records) are emitted INLINE at their
|
|
24
|
+
//! one reference site, exactly where the transpiler emits an inline
|
|
25
|
+
//! struct — a named top-level declaration would change nothing for
|
|
26
|
+
//! layout but everything for `@typeName`-carrying artifacts.
|
|
27
|
+
//! - Exported helpers become Model methods (`fn (self: *const Model)`,
|
|
28
|
+
//! plus the build-arena form) forwarding to the core's helper_call
|
|
29
|
+
//! entry; `view_unbound` tuples mirror the unbound lists.
|
|
30
|
+
//!
|
|
31
|
+
//! Executable surface: dispatch stubs route each arm through the ABI
|
|
32
|
+
//! entry its payload descriptor selects, the snapshot decoder
|
|
33
|
+
//! materializes the committed model into a mirror value after every
|
|
34
|
+
//! dispatch, and the boot path runs the identity fence before anything
|
|
35
|
+
//! else. All extern references live in core_abi.zig (staged beside the
|
|
36
|
+
//! output), so ABI ratification cannot touch this emitter.
|
|
37
|
+
|
|
38
|
+
const std = @import("std");
|
|
39
|
+
const sidecar_mod = @import("sidecar.zig");
|
|
40
|
+
|
|
41
|
+
const Sidecar = sidecar_mod.Sidecar;
|
|
42
|
+
const TypeRef = sidecar_mod.TypeRef;
|
|
43
|
+
const Payload = sidecar_mod.Payload;
|
|
44
|
+
|
|
45
|
+
/// The eleven text-input event tags the markup engines recognize
|
|
46
|
+
/// structurally; a union payload carrying exactly these dispatches
|
|
47
|
+
/// through the ABI's text_input entry.
|
|
48
|
+
const text_input_event_tags = [_][]const u8{
|
|
49
|
+
"insert_text", "delete_backward", "delete_forward", "delete_word_backward",
|
|
50
|
+
"delete_word_forward", "clear", "move_caret", "set_selection",
|
|
51
|
+
"set_composition", "commit_composition", "cancel_composition",
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/// Whether a union payload declares the text-input event shape the markup
|
|
55
|
+
/// engines route through the dedicated ABI entry: exactly the eleven tags,
|
|
56
|
+
/// with the same structural payload vocabulary. This is public because the
|
|
57
|
+
/// generated facade must make the identical decision when selecting the one
|
|
58
|
+
/// sentinel-saturating decoder; one contract cannot have two recognizers.
|
|
59
|
+
pub fn isTextInputUnion(sidecar: Sidecar, type_name: []const u8) bool {
|
|
60
|
+
const entry = sidecar_mod.findUnion(sidecar.types, type_name) orelse return false;
|
|
61
|
+
if (entry.arms.len != text_input_event_tags.len) return false;
|
|
62
|
+
outer: for (text_input_event_tags) |tag_name| {
|
|
63
|
+
for (entry.arms) |arm| {
|
|
64
|
+
if (std.mem.eql(u8, arm.name, tag_name)) continue :outer;
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
for (entry.arms) |arm| {
|
|
69
|
+
if (std.mem.eql(u8, arm.name, "insert_text")) {
|
|
70
|
+
if (arm.payload != .bytes) return false;
|
|
71
|
+
} else if (std.mem.eql(u8, arm.name, "move_caret")) {
|
|
72
|
+
if (!isTextCaretMoveRecord(sidecar, arm.payload)) return false;
|
|
73
|
+
} else if (std.mem.eql(u8, arm.name, "set_selection")) {
|
|
74
|
+
if (!isTextSelectionRecord(sidecar, arm.payload)) return false;
|
|
75
|
+
} else if (std.mem.eql(u8, arm.name, "set_composition")) {
|
|
76
|
+
if (!isTextCompositionRecord(sidecar, arm.payload)) return false;
|
|
77
|
+
} else if (arm.payload != .void) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fn textRecordOf(sidecar: Sidecar, ref: TypeRef) ?*const sidecar_mod.Struct {
|
|
85
|
+
return switch (ref) {
|
|
86
|
+
.value => |name| sidecar_mod.findStruct(sidecar.types, name),
|
|
87
|
+
else => null,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn isTextNumericRef(ref: TypeRef) bool {
|
|
92
|
+
return ref == .f64 or ref == .i64;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
fn isTextCaretMoveRecord(sidecar: Sidecar, ref: TypeRef) bool {
|
|
96
|
+
const record = textRecordOf(sidecar, ref) orelse return false;
|
|
97
|
+
if (record.fields.len != 2) return false;
|
|
98
|
+
const caret_members = [_][]const u8{ "previous", "next", "previous_word", "next_word", "start", "end" };
|
|
99
|
+
var direction_ok = false;
|
|
100
|
+
var extend_ok = false;
|
|
101
|
+
for (record.fields) |field| {
|
|
102
|
+
if (std.mem.eql(u8, field.name, "extend")) {
|
|
103
|
+
extend_ok = field.type == .bool;
|
|
104
|
+
} else if (std.mem.eql(u8, field.name, "direction")) {
|
|
105
|
+
const members = switch (field.type) {
|
|
106
|
+
.enum_ref => |name| (sidecar_mod.findEnum(sidecar.types, name) orelse return false).members,
|
|
107
|
+
else => return false,
|
|
108
|
+
};
|
|
109
|
+
if (members.len != caret_members.len) return false;
|
|
110
|
+
outer: for (caret_members) |member| {
|
|
111
|
+
for (members) |declared| {
|
|
112
|
+
if (std.mem.eql(u8, declared, member)) continue :outer;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
direction_ok = true;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return direction_ok and extend_ok;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fn isTextSelectionRecord(sidecar: Sidecar, ref: TypeRef) bool {
|
|
123
|
+
const record = textRecordOf(sidecar, ref) orelse return false;
|
|
124
|
+
if (record.fields.len != 2) return false;
|
|
125
|
+
var anchor_ok = false;
|
|
126
|
+
var focus_ok = false;
|
|
127
|
+
for (record.fields) |field| {
|
|
128
|
+
if (std.mem.eql(u8, field.name, "anchor")) anchor_ok = isTextNumericRef(field.type);
|
|
129
|
+
if (std.mem.eql(u8, field.name, "focus")) focus_ok = isTextNumericRef(field.type);
|
|
130
|
+
}
|
|
131
|
+
return anchor_ok and focus_ok;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
fn isTextCompositionRecord(sidecar: Sidecar, ref: TypeRef) bool {
|
|
135
|
+
const record = textRecordOf(sidecar, ref) orelse return false;
|
|
136
|
+
if (record.fields.len != 2) return false;
|
|
137
|
+
var text_ok = false;
|
|
138
|
+
var cursor_ok = false;
|
|
139
|
+
for (record.fields) |field| {
|
|
140
|
+
if (std.mem.eql(u8, field.name, "text")) text_ok = field.type == .bytes;
|
|
141
|
+
if (std.mem.eql(u8, field.name, "cursor")) {
|
|
142
|
+
cursor_ok = switch (field.type) {
|
|
143
|
+
.optional => |inner| isTextNumericRef(inner.*),
|
|
144
|
+
else => false,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return text_ok and cursor_ok;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/// The scroll-state field vocabulary — the TWO-AXIS record, eight
|
|
152
|
+
/// per-axis fields — in the TS spelling (the emitted-core mirror keeps
|
|
153
|
+
/// the author's names) and the canvas spelling. The order is
|
|
154
|
+
/// `canvas.ScrollState`'s declaration order, which is also the ABI
|
|
155
|
+
/// entry's parameter order. The retired one-axis quartet
|
|
156
|
+
/// (`{offset, velocity, viewportExtent, contentExtent}`) is NOT scroll
|
|
157
|
+
/// state anymore: a record carrying it rides the generic record entry,
|
|
158
|
+
/// and the markup engines refuse to bind `on-scroll` to it with a
|
|
159
|
+
/// teaching that names these fields.
|
|
160
|
+
const scroll_state_fields_ts = [_][]const u8{
|
|
161
|
+
"offsetX", "offsetY",
|
|
162
|
+
"velocityX", "velocityY",
|
|
163
|
+
"viewportExtentX", "viewportExtentY",
|
|
164
|
+
"contentExtentX", "contentExtentY",
|
|
165
|
+
};
|
|
166
|
+
const scroll_state_fields_canvas = [_][]const u8{
|
|
167
|
+
"offset_x", "offset_y",
|
|
168
|
+
"velocity_x", "velocity_y",
|
|
169
|
+
"viewport_extent_x", "viewport_extent_y",
|
|
170
|
+
"content_extent_x", "content_extent_y",
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
pub const Error = error{ Refused, OutOfMemory };
|
|
174
|
+
|
|
175
|
+
/// Emit the complete core_shim.zig source. Deterministic: a pure
|
|
176
|
+
/// function of the sidecar value (V13's generator-side counterpart,
|
|
177
|
+
/// pinned by a test).
|
|
178
|
+
pub fn emit(arena: std.mem.Allocator, sidecar: Sidecar, diags: *sidecar_mod.Diagnostics) Error![]const u8 {
|
|
179
|
+
var emitter = Emitter{
|
|
180
|
+
.arena = arena,
|
|
181
|
+
.sidecar = sidecar,
|
|
182
|
+
.diags = diags,
|
|
183
|
+
.out = .empty,
|
|
184
|
+
};
|
|
185
|
+
try emitter.run();
|
|
186
|
+
if (diags.hasErrors()) return error.Refused;
|
|
187
|
+
return emitter.out.items;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const Emitter = struct {
|
|
191
|
+
arena: std.mem.Allocator,
|
|
192
|
+
sidecar: Sidecar,
|
|
193
|
+
diags: *sidecar_mod.Diagnostics,
|
|
194
|
+
out: std.ArrayListUnmanaged(u8),
|
|
195
|
+
/// Table entries emitted inline at their single reference site
|
|
196
|
+
/// (see inlinedNames).
|
|
197
|
+
inlined: []const []const u8 = &.{},
|
|
198
|
+
|
|
199
|
+
fn print(self: *Emitter, comptime fmt: []const u8, args: anytype) Error!void {
|
|
200
|
+
const text = try std.fmt.allocPrint(self.arena, fmt, args);
|
|
201
|
+
try self.out.appendSlice(self.arena, text);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
fn raw(self: *Emitter, text: []const u8) Error!void {
|
|
205
|
+
try self.out.appendSlice(self.arena, text);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
fn run(self: *Emitter) Error!void {
|
|
209
|
+
try self.validateEmissionNames();
|
|
210
|
+
self.validateChannelShapes();
|
|
211
|
+
try self.validateArmStorage();
|
|
212
|
+
try self.validateIntegerAttestations();
|
|
213
|
+
if (self.diags.hasErrors()) return;
|
|
214
|
+
self.inlined = try self.inlinedNames();
|
|
215
|
+
try self.header();
|
|
216
|
+
try self.mirrorTypes();
|
|
217
|
+
try self.modelStruct();
|
|
218
|
+
try self.msgUnion();
|
|
219
|
+
try self.wiringAliases();
|
|
220
|
+
try self.tagTable();
|
|
221
|
+
try self.entryPoints();
|
|
222
|
+
try self.channels();
|
|
223
|
+
try self.helperPlumbing();
|
|
224
|
+
try self.snapshotPlumbing();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// -------------------------------------------- emission-name space
|
|
228
|
+
//
|
|
229
|
+
// The generated module declares more than the mirror types: the rt
|
|
230
|
+
// block, the tag table, the entry points, and (per wired channel)
|
|
231
|
+
// the fixed event-record vocabulary. A sidecar whose own names
|
|
232
|
+
// collide with those declarations would generate a module that
|
|
233
|
+
// cannot compile — refuse at tool time with the exact name instead.
|
|
234
|
+
|
|
235
|
+
fn validateEmissionNames(self: *Emitter) Error!void {
|
|
236
|
+
var reserved: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
237
|
+
try reserved.appendSlice(self.arena, &.{
|
|
238
|
+
"std", "shim_rt", "core_abi", "abi",
|
|
239
|
+
"rt", "msg_tags", "boot", "initialModel",
|
|
240
|
+
"update", "commitModelRoot", "sidecar_build_id", "sidecar_abi_version",
|
|
241
|
+
"deterministic", "async_free", "snapshotModel", "referenceAttestedExports",
|
|
242
|
+
});
|
|
243
|
+
// Parameter and local names the generated bodies bind: a
|
|
244
|
+
// module-level type under any of them would be shadowed, which
|
|
245
|
+
// the compiler refuses.
|
|
246
|
+
try reserved.appendSlice(self.arena, &.{
|
|
247
|
+
"T", "n", "model", "msg", "payload", "encoded",
|
|
248
|
+
"cmd_ptr", "cmd_len", "subs_ptr", "subs_len", "snap_ptr", "snap_len",
|
|
249
|
+
"out", "out_ptr", "out_len", "tag", "tag_name", "name",
|
|
250
|
+
"frame", "key", "pinch", "value", "self", "index",
|
|
251
|
+
"args", "args_tuple", "allocator", "fields", "field", "next",
|
|
252
|
+
"helper_args",
|
|
253
|
+
// The unbound-list tuples nest inside Model and Msg, where
|
|
254
|
+
// they would shadow a same-named top-level type at every
|
|
255
|
+
// field-type resolution inside those structs.
|
|
256
|
+
"view_unbound",
|
|
257
|
+
});
|
|
258
|
+
for (self.sidecar.model_helpers) |helper| {
|
|
259
|
+
for (helper.params, 0..) |_, param_index| {
|
|
260
|
+
try reserved.append(self.arena, try std.fmt.allocPrint(self.arena, "p{d}", .{param_index}));
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
// The host layer wires channels by PROBING these declaration
|
|
264
|
+
// names (`@hasDecl` — export presence IS the wiring decision),
|
|
265
|
+
// so they are off limits even when the channel is absent: a
|
|
266
|
+
// mere type under one of these names would falsely activate
|
|
267
|
+
// the channel and then fail as a non-function.
|
|
268
|
+
try reserved.appendSlice(self.arena, &.{
|
|
269
|
+
"subscriptions", "commandMsg", "frameMsg", "keyMsg",
|
|
270
|
+
"pinchMsg", "appearanceMsg", "chromeMsg", "envMsgs",
|
|
271
|
+
});
|
|
272
|
+
// The wiring aliases (wiringAliases) claim the host layer's
|
|
273
|
+
// fixed spellings whenever the sidecar's own names differ.
|
|
274
|
+
if (!std.mem.eql(u8, self.sidecar.model, "Model")) try reserved.append(self.arena, "Model");
|
|
275
|
+
if (!std.mem.eql(u8, self.sidecar.msg.name, "Msg")) try reserved.append(self.arena, "Msg");
|
|
276
|
+
// Optional glue reserves its name only when it is emitted.
|
|
277
|
+
if (self.sidecar.model_helpers.len > 0) try reserved.append(self.arena, "callHelper");
|
|
278
|
+
const chan = self.sidecar.channels;
|
|
279
|
+
if (chan.command_msg or chan.frame_msg or chan.key_msg or chan.pinch_msg) {
|
|
280
|
+
try reserved.appendSlice(self.arena, &.{ "msgFromEnvelope", "envelope", "header" });
|
|
281
|
+
}
|
|
282
|
+
if (self.sidecar.init_returns_cmd) try reserved.append(self.arena, "InitResult");
|
|
283
|
+
if (self.sidecar.update_returns_cmd) try reserved.append(self.arena, "UpdateResult");
|
|
284
|
+
if (self.sidecar.channels.frame_msg) try reserved.append(self.arena, "FrameEvent");
|
|
285
|
+
if (self.sidecar.channels.key_msg) try reserved.append(self.arena, "KeyEvent");
|
|
286
|
+
if (self.sidecar.channels.pinch_msg) try reserved.appendSlice(self.arena, &.{ "PinchEvent", "PinchPhase" });
|
|
287
|
+
|
|
288
|
+
const table_names = try self.allTableNames();
|
|
289
|
+
for (table_names) |name| {
|
|
290
|
+
if (nameListed(reserved.items, name)) {
|
|
291
|
+
self.diags.flag("types", "type name \"{s}\" collides with a declaration the generated shim itself must make; rename the type in the core source", .{name});
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (nameListed(reserved.items, self.sidecar.msg.name) or nameListed(table_names, self.sidecar.msg.name)) {
|
|
295
|
+
self.diags.flag("msg.name", "message union name \"{s}\" collides with another declaration of the generated shim; rename the union in the core source", .{self.sidecar.msg.name});
|
|
296
|
+
}
|
|
297
|
+
// Helper methods share the model struct's member namespace with
|
|
298
|
+
// its fields — and "view_unbound" is the opt-out tuple's
|
|
299
|
+
// spelling, which every reflecting consumer reads as data, so a
|
|
300
|
+
// helper must never take it. A helper may not take a reserved
|
|
301
|
+
// name either: methods shadow file-scope declarations inside
|
|
302
|
+
// the struct, so a helper named after generated glue (callHelper
|
|
303
|
+
// above all) would capture the glue's own call sites.
|
|
304
|
+
for (self.sidecar.model_helpers) |helper| {
|
|
305
|
+
if (nameListed(reserved.items, helper.name)) {
|
|
306
|
+
self.diags.flag("model_helpers", "helper \"{s}\" collides with a declaration the generated shim itself must make (methods shadow file-scope names inside the model struct); rename the helper in the core source", .{helper.name});
|
|
307
|
+
}
|
|
308
|
+
// The same shadowing applies to the mirror's own type
|
|
309
|
+
// declarations: a field spelled `row: Row` inside the model
|
|
310
|
+
// struct resolves Row to a method of that name first.
|
|
311
|
+
if (nameListed(table_names, helper.name) or std.mem.eql(u8, helper.name, self.sidecar.msg.name)) {
|
|
312
|
+
self.diags.flag("model_helpers", "helper \"{s}\" shadows the type of the same name inside the model struct, where field types resolve; rename one in the core source", .{helper.name});
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (sidecar_mod.findStruct(self.sidecar.types, self.sidecar.model)) |model| {
|
|
316
|
+
for (self.sidecar.model_helpers) |helper| {
|
|
317
|
+
if (std.mem.eql(u8, helper.name, "view_unbound")) {
|
|
318
|
+
self.diags.flag("model_helpers", "helper \"view_unbound\" takes the unbound-list declaration's spelling — the contract reflection reads that name as the opt-out tuple; rename the helper in the core source", .{});
|
|
319
|
+
}
|
|
320
|
+
for (model.fields) |field| {
|
|
321
|
+
if (std.mem.eql(u8, helper.name, field.name)) {
|
|
322
|
+
self.diags.flag("model_helpers", "helper \"{s}\" collides with the model field of the same name — the mirror declares helpers as model methods, one member namespace; rename one in the core source", .{helper.name});
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
if (self.sidecar.model_unbound.len > 0) {
|
|
327
|
+
for (model.fields) |field| {
|
|
328
|
+
if (std.mem.eql(u8, field.name, "view_unbound")) {
|
|
329
|
+
self.diags.flag("model_unbound", "the model field \"view_unbound\" collides with the unbound-list declaration the mirror must make; rename the field in the core source", .{});
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (self.sidecar.msg.unbound.len > 0) {
|
|
335
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
336
|
+
if (std.mem.eql(u8, arm.name, "view_unbound")) {
|
|
337
|
+
self.diags.flag("msg.unbound", "the message arm \"view_unbound\" collides with the unbound-list declaration the mirror must make; rename the arm in the core source", .{});
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// ------------------------------------------- channel arm shapes
|
|
344
|
+
//
|
|
345
|
+
// The appearance and chrome channels have no function entry point:
|
|
346
|
+
// the host constructs the named arm's record itself, so the arm
|
|
347
|
+
// must carry the exact structural vocabulary the adapter builds by
|
|
348
|
+
// field name. The adapter re-proves this at comptime over the
|
|
349
|
+
// mirror; checking here moves the teaching to tool time instead of
|
|
350
|
+
// a compile error inside generated wiring.
|
|
351
|
+
|
|
352
|
+
fn validateChannelShapes(self: *Emitter) void {
|
|
353
|
+
if (self.sidecar.channels.appearance_msg) |arm_name| {
|
|
354
|
+
const teaching = "the appearance arm must carry exactly {{ colorScheme: a light/dark enum, reduceMotion: bool, highContrast: bool }} — the host builds this record by field name";
|
|
355
|
+
const record = self.channelArmRecord(arm_name) orelse {
|
|
356
|
+
self.diags.flag("channels.appearance_msg", teaching, .{});
|
|
357
|
+
return;
|
|
358
|
+
};
|
|
359
|
+
if (!self.isAppearanceRecord(record)) {
|
|
360
|
+
self.diags.flag("channels.appearance_msg", teaching, .{});
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (self.sidecar.channels.chrome_msg) |arm_name| {
|
|
364
|
+
const teaching = "the chrome arm must carry exactly {{ insets: top/right/bottom/left numbers, buttons: x/y/width/height numbers, tabsProjected: bool }} — the host builds this record by field name";
|
|
365
|
+
const record = self.channelArmRecord(arm_name) orelse {
|
|
366
|
+
self.diags.flag("channels.chrome_msg", teaching, .{});
|
|
367
|
+
return;
|
|
368
|
+
};
|
|
369
|
+
if (!self.isChromeRecord(record)) {
|
|
370
|
+
self.diags.flag("channels.chrome_msg", teaching, .{});
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
fn channelArmRecord(self: *Emitter, arm_name: []const u8) ?*const sidecar_mod.Struct {
|
|
376
|
+
const arm = sidecar_mod.findArm(self.sidecar.msg, arm_name) orelse return null;
|
|
377
|
+
return switch (arm.payload) {
|
|
378
|
+
.record => |name| sidecar_mod.findStruct(self.sidecar.types, name),
|
|
379
|
+
else => null,
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
fn isAppearanceRecord(self: *Emitter, record: *const sidecar_mod.Struct) bool {
|
|
384
|
+
if (record.fields.len != 3) return false;
|
|
385
|
+
var scheme_ok = false;
|
|
386
|
+
var reduce_ok = false;
|
|
387
|
+
var contrast_ok = false;
|
|
388
|
+
for (record.fields) |field| {
|
|
389
|
+
if (std.mem.eql(u8, field.name, "colorScheme")) {
|
|
390
|
+
const members = switch (field.type) {
|
|
391
|
+
.enum_ref => |name| (sidecar_mod.findEnum(self.sidecar.types, name) orelse return false).members,
|
|
392
|
+
else => return false,
|
|
393
|
+
};
|
|
394
|
+
if (members.len != 2) return false;
|
|
395
|
+
var light = false;
|
|
396
|
+
var dark = false;
|
|
397
|
+
for (members) |member| {
|
|
398
|
+
if (std.mem.eql(u8, member, "light")) light = true;
|
|
399
|
+
if (std.mem.eql(u8, member, "dark")) dark = true;
|
|
400
|
+
}
|
|
401
|
+
scheme_ok = light and dark;
|
|
402
|
+
} else if (std.mem.eql(u8, field.name, "reduceMotion")) {
|
|
403
|
+
reduce_ok = field.type == .bool;
|
|
404
|
+
} else if (std.mem.eql(u8, field.name, "highContrast")) {
|
|
405
|
+
contrast_ok = field.type == .bool;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return scheme_ok and reduce_ok and contrast_ok;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
fn isChromeRecord(self: *Emitter, record: *const sidecar_mod.Struct) bool {
|
|
412
|
+
if (record.fields.len != 3) return false;
|
|
413
|
+
var insets_ok = false;
|
|
414
|
+
var buttons_ok = false;
|
|
415
|
+
var tabs_ok = false;
|
|
416
|
+
for (record.fields) |field| {
|
|
417
|
+
if (std.mem.eql(u8, field.name, "insets")) {
|
|
418
|
+
insets_ok = self.isNumericRecord(field.type, &.{ "top", "right", "bottom", "left" });
|
|
419
|
+
} else if (std.mem.eql(u8, field.name, "buttons")) {
|
|
420
|
+
buttons_ok = self.isNumericRecord(field.type, &.{ "x", "y", "width", "height" });
|
|
421
|
+
} else if (std.mem.eql(u8, field.name, "tabsProjected")) {
|
|
422
|
+
tabs_ok = field.type == .bool;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return insets_ok and buttons_ok and tabs_ok;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/// A BY-VALUE record reference for structural host vocabularies. A node
|
|
429
|
+
/// reference mirrors as a pointer and therefore cannot satisfy a record
|
|
430
|
+
/// the host constructs directly by field name.
|
|
431
|
+
fn recordOf(self: *Emitter, ref: TypeRef) ?*const sidecar_mod.Struct {
|
|
432
|
+
return switch (ref) {
|
|
433
|
+
.value => |name| sidecar_mod.findStruct(self.sidecar.types, name),
|
|
434
|
+
else => null,
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
fn isNumericRef(ref: TypeRef) bool {
|
|
439
|
+
return ref == .f64 or ref == .i64;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
fn isNumericRecord(self: *Emitter, ref: TypeRef, names: []const []const u8) bool {
|
|
443
|
+
const record = self.recordOf(ref) orelse return false;
|
|
444
|
+
if (record.fields.len != names.len) return false;
|
|
445
|
+
outer: for (names) |name| {
|
|
446
|
+
for (record.fields) |field| {
|
|
447
|
+
if (std.mem.eql(u8, field.name, name)) {
|
|
448
|
+
if (!isNumericRef(field.type)) return false;
|
|
449
|
+
continue :outer;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
return true;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/// Records the type table stores BY REFERENCE anywhere (`node`
|
|
458
|
+
/// TypeRefs — committed-heap node types, the model root included)
|
|
459
|
+
/// also ride by reference when they double as a message arm payload
|
|
460
|
+
/// (`picked: *const Item`, verified against the shipped emitter) —
|
|
461
|
+
/// a storage fact the record payload family cannot carry, so
|
|
462
|
+
/// mirroring the arm by value would silently change the reflected
|
|
463
|
+
/// layout. Refuse that shape until the schema says storage
|
|
464
|
+
/// (SCHEMA-GAPS.md). Value-stored records — the corpus's
|
|
465
|
+
/// synthesized inline records, event shapes, and value-promoted
|
|
466
|
+
/// records like a text-input union's caret and selection payloads —
|
|
467
|
+
/// stay by value in arms exactly as the emission keeps them.
|
|
468
|
+
fn validateArmStorage(self: *Emitter) Error!void {
|
|
469
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
470
|
+
switch (arm.payload) {
|
|
471
|
+
.record => |name| {
|
|
472
|
+
if (self.nodeStored(name)) {
|
|
473
|
+
self.diags.flag("msg.arms", "arm \"{s}\": record \"{s}\" is stored by reference in the model graph, and the compiled core keeps that storage when the record doubles as an arm payload — a fact the record payload family cannot carry; keep the arm's payload a value-stored record", .{ arm.name, name });
|
|
474
|
+
}
|
|
475
|
+
},
|
|
476
|
+
else => {},
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/// Whether any reference in the contract stores this record by
|
|
482
|
+
/// reference (or it is the committed model root itself).
|
|
483
|
+
fn nodeStored(self: *Emitter, name: []const u8) bool {
|
|
484
|
+
if (std.mem.eql(u8, self.sidecar.model, name)) return true;
|
|
485
|
+
for (self.sidecar.types.structs) |entry| {
|
|
486
|
+
for (entry.fields) |field| {
|
|
487
|
+
if (refNodeNames(field.type, name)) return true;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
for (self.sidecar.types.unions) |entry| {
|
|
491
|
+
for (entry.arms) |arm| {
|
|
492
|
+
if (arm.payload != .void and refNodeNames(arm.payload, name)) return true;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
for (self.sidecar.model_helpers) |helper| {
|
|
496
|
+
if (refNodeNames(helper.returns, name)) return true;
|
|
497
|
+
for (helper.params) |param| {
|
|
498
|
+
if (refNodeNames(param, name)) return true;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return false;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
fn allTableNames(self: *Emitter) Error![]const []const u8 {
|
|
505
|
+
var names: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
506
|
+
for (self.sidecar.types.structs) |entry| try names.append(self.arena, entry.name);
|
|
507
|
+
for (self.sidecar.types.enums) |entry| try names.append(self.arena, entry.name);
|
|
508
|
+
for (self.sidecar.types.unions) |entry| try names.append(self.arena, entry.name);
|
|
509
|
+
return names.items;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// ------------------------------------------------------- preamble
|
|
513
|
+
|
|
514
|
+
fn header(self: *Emitter) Error!void {
|
|
515
|
+
try self.print(
|
|
516
|
+
\\//! Generated by corewire from this app's core.contract.json — the
|
|
517
|
+
\\//! host-side mirror of a compiled TypeScript core. Every type below
|
|
518
|
+
\\//! reproduces the compiled core's contract exactly (field names,
|
|
519
|
+
\\//! declaration order, number classes), so the markup engines, the
|
|
520
|
+
\\//! adapter, the bridge, and the model-contract step reflect over it
|
|
521
|
+
\\//! the way they reflect over transpiler output. Do not edit;
|
|
522
|
+
\\//! regenerate from the sidecar.
|
|
523
|
+
\\//!
|
|
524
|
+
\\//! Compiled core identity: entry {s}, compiler {s},
|
|
525
|
+
\\//! build_id {x:0>16} (checked against the object's exported getter
|
|
526
|
+
\\//! before the first dispatch).
|
|
527
|
+
\\
|
|
528
|
+
\\const std = @import("std");
|
|
529
|
+
\\const shim_rt = @import("shim_rt.zig");
|
|
530
|
+
\\const core_abi = @import("core_abi.zig");
|
|
531
|
+
\\const abi = core_abi.Bindings("{f}");
|
|
532
|
+
\\
|
|
533
|
+
\\/// The sidecar's identity facts, restated for the boot-time pairing
|
|
534
|
+
\\/// fence (the out-of-graph backstop: a cached or hand-copied core
|
|
535
|
+
\\/// must never dispatch against another build's tag order).
|
|
536
|
+
\\pub const sidecar_build_id: u64 = 0x{x:0>16};
|
|
537
|
+
\\pub const sidecar_abi_version: u32 = {d};
|
|
538
|
+
\\
|
|
539
|
+
\\/// The compiler's module-graph attestations, restated so host
|
|
540
|
+
\\/// policy can gate on them: record/replay arms only against a
|
|
541
|
+
\\/// deterministic core, and loop-free-core policies read
|
|
542
|
+
\\/// async_free. Computed facts of the compiled graph — the sidecar
|
|
543
|
+
\\/// records verdicts, never hopes.
|
|
544
|
+
\\pub const deterministic: bool = {};
|
|
545
|
+
\\pub const async_free: bool = {};
|
|
546
|
+
\\
|
|
547
|
+
\\/// The kernel surface the host layer drives, forwarded to the shim
|
|
548
|
+
\\/// arena and the core's own frame reset (one cycle boundary for
|
|
549
|
+
\\/// both sides).
|
|
550
|
+
\\pub const rt = struct {{
|
|
551
|
+
\\ pub const Cmd = []const u8;
|
|
552
|
+
\\ pub const Sub = []const u8;
|
|
553
|
+
\\ pub const cmd_none: Cmd = &.{{}};
|
|
554
|
+
\\ pub const sub_none: Sub = &.{{}};
|
|
555
|
+
\\ pub fn frameAlloc(comptime T: type, n: usize) []T {{
|
|
556
|
+
\\ return shim_rt.frameAlloc(T, n);
|
|
557
|
+
\\ }}
|
|
558
|
+
\\ pub fn frameCreate(comptime T: type, value: T) *T {{
|
|
559
|
+
\\ return shim_rt.frameCreate(T, value);
|
|
560
|
+
\\ }}
|
|
561
|
+
\\ pub fn frameReset() void {{
|
|
562
|
+
\\ shim_rt.frameReset();
|
|
563
|
+
\\ abi.frame_reset();
|
|
564
|
+
\\ }}
|
|
565
|
+
\\ pub fn resetAll() void {{
|
|
566
|
+
\\ shim_rt.resetAll();
|
|
567
|
+
\\ }}
|
|
568
|
+
\\}};
|
|
569
|
+
\\
|
|
570
|
+
, .{
|
|
571
|
+
try commentText(self.arena, self.sidecar.entry),
|
|
572
|
+
try commentText(self.arena, self.sidecar.compiler_version),
|
|
573
|
+
self.sidecar.build_id,
|
|
574
|
+
std.zig.fmtString(self.sidecar.abi.prefix),
|
|
575
|
+
self.sidecar.build_id,
|
|
576
|
+
self.sidecar.abi_version,
|
|
577
|
+
self.sidecar.deterministic,
|
|
578
|
+
self.sidecar.async_free,
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// ------------------------------------------- attested integer class
|
|
583
|
+
//
|
|
584
|
+
// The sidecar's one integer TypeRef spelling is i64; the matching
|
|
585
|
+
// integer_slots entry may refine it to the u64 class, and the
|
|
586
|
+
// mirror must decode what the attestation says (8-byte unsigned LE
|
|
587
|
+
// instead of two's complement). The reader validates the bijection
|
|
588
|
+
// before emission ever runs, so a lookup miss here simply means the
|
|
589
|
+
// slot keeps its i64 spelling.
|
|
590
|
+
|
|
591
|
+
fn attestedClass(self: *Emitter, slot: []const u8) ?sidecar_mod.IntegerClass {
|
|
592
|
+
for (self.sidecar.integer_slots) |entry| {
|
|
593
|
+
if (std.mem.eql(u8, entry.slot, slot)) return entry.class;
|
|
594
|
+
}
|
|
595
|
+
return null;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/// The Zig spelling of an i64-spelled slot, per its attestation.
|
|
599
|
+
fn integerSpelling(self: *Emitter, slot: ?[]const u8) []const u8 {
|
|
600
|
+
const path = slot orelse return "i64";
|
|
601
|
+
const class = self.attestedClass(path) orelse return "i64";
|
|
602
|
+
return @tagName(class);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/// The exact-crossing conversion for an integer-classed dispatch
|
|
606
|
+
/// payload (the scalar entries carry f64): the signed guard or its
|
|
607
|
+
/// unsigned twin, per the slot's attested class.
|
|
608
|
+
fn exactCall(self: *Emitter, slot: []const u8, inner: []const u8) Error![]const u8 {
|
|
609
|
+
const converter = if (self.attestedClass(slot) == .u64) "exactF64Unsigned" else "exactF64";
|
|
610
|
+
return std.fmt.allocPrint(self.arena, "shim_rt.{s}({s})", .{ converter, inner });
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/// A slot path in the sidecar's grammar: `<Container>.<member>`.
|
|
614
|
+
fn slotPath(self: *Emitter, container: []const u8, member: []const u8) Error![]const u8 {
|
|
615
|
+
return std.fmt.allocPrint(self.arena, "{s}.{s}", .{ container, member });
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/// A number_bytes arm's number-field slot path:
|
|
619
|
+
/// `<msgName>.<arm>.<field>`.
|
|
620
|
+
fn numberBytesSlot(self: *Emitter, arm: []const u8, field: []const u8) Error![]const u8 {
|
|
621
|
+
return std.fmt.allocPrint(self.arena, "{s}.{s}.{s}", .{ self.sidecar.msg.name, arm, field });
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/// The Zig spelling of a number descriptor's payload, per class and
|
|
625
|
+
/// attestation.
|
|
626
|
+
fn numberSpelling(self: *Emitter, class: sidecar_mod.NumberClass, slot: []const u8) Error![]const u8 {
|
|
627
|
+
return switch (class) {
|
|
628
|
+
.f64 => "f64",
|
|
629
|
+
.i64 => self.integerSpelling(slot),
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// ------------------------------------ host-supplied signed slots
|
|
634
|
+
//
|
|
635
|
+
// Two structural vocabularies carry slots the HOST fills with
|
|
636
|
+
// values that go negative: a scroll arm's offsets and velocities
|
|
637
|
+
// (rubber-banding, upward flicks) and the window-control cluster's
|
|
638
|
+
// position (signed content coordinates). A u64 attestation on such
|
|
639
|
+
// a slot could never carry those values — the generated wiring
|
|
640
|
+
// would trap converting them — so the checker refuses the
|
|
641
|
+
// attestation at tool time instead. Everything the host supplies
|
|
642
|
+
// non-negative stays attestable: scroll extents, selection bounds,
|
|
643
|
+
// chrome insets and cluster sizes — and a scroll-shaped record the
|
|
644
|
+
// host never dispatches into (a model-only metrics record) is not
|
|
645
|
+
// this rule's business at all.
|
|
646
|
+
|
|
647
|
+
/// The scroll-state axes the host supplies signed, in both
|
|
648
|
+
/// spellings (the TS mirror's and the canvas core's).
|
|
649
|
+
const signed_scroll_axes = [_][]const u8{
|
|
650
|
+
"offsetX", "offsetY", "velocityX", "velocityY",
|
|
651
|
+
"offset_x", "offset_y", "velocity_x", "velocity_y",
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
fn validateIntegerAttestations(self: *Emitter) Error!void {
|
|
655
|
+
// Only records a message arm routes through the scroll entry
|
|
656
|
+
// receive host scroll values.
|
|
657
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
658
|
+
const record_name = switch (arm.payload) {
|
|
659
|
+
.record => |name| name,
|
|
660
|
+
else => continue,
|
|
661
|
+
};
|
|
662
|
+
if (self.scrollStateFields(record_name) == null) continue;
|
|
663
|
+
const entry = sidecar_mod.findStruct(self.sidecar.types, record_name) orelse continue;
|
|
664
|
+
for (entry.fields) |field| {
|
|
665
|
+
if (field.type != .i64) continue;
|
|
666
|
+
if (!nameListed(&signed_scroll_axes, field.name)) continue;
|
|
667
|
+
const slot = try self.slotPath(entry.name, field.name);
|
|
668
|
+
if (self.attestedClass(slot) == .u64) {
|
|
669
|
+
self.diags.flag("integer_slots", "\"{s}\" is a signed scroll-state axis (offsets and velocities go negative during rubber-banding and upward flicks) — the u64 class cannot carry those values; keep offset and velocity fields i64 or f64", .{slot});
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
if (self.sidecar.channels.chrome_msg) |arm_name| {
|
|
674
|
+
if (self.channelArmRecord(arm_name)) |record| {
|
|
675
|
+
for (record.fields) |field| {
|
|
676
|
+
// Insets and cluster sizes are non-negative overlay
|
|
677
|
+
// extents; only the cluster's position is signed.
|
|
678
|
+
if (!std.mem.eql(u8, field.name, "buttons")) continue;
|
|
679
|
+
const nested = self.recordOf(field.type) orelse continue;
|
|
680
|
+
for (nested.fields) |geometry| {
|
|
681
|
+
if (geometry.type != .i64) continue;
|
|
682
|
+
if (!std.mem.eql(u8, geometry.name, "x") and !std.mem.eql(u8, geometry.name, "y")) continue;
|
|
683
|
+
const slot = try self.slotPath(nested.name, geometry.name);
|
|
684
|
+
if (self.attestedClass(slot) == .u64) {
|
|
685
|
+
self.diags.flag("integer_slots", "\"{s}\" is the window-control cluster's position, reported in signed content coordinates — the u64 class cannot carry it; keep buttons x and y i64 or f64", .{slot});
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// --------------------------------------------------- mirror types
|
|
694
|
+
|
|
695
|
+
/// Inline a table entry only when the pattern matches AND the entry
|
|
696
|
+
/// is referenced exactly once in the whole sidecar: an authored type
|
|
697
|
+
/// that merely spells like the pattern but is shared across sites
|
|
698
|
+
/// stays a named top-level declaration (inlining it at one site
|
|
699
|
+
/// would leave the others dangling).
|
|
700
|
+
fn inlinedNames(self: *Emitter) Error![]const []const u8 {
|
|
701
|
+
return inlinedTableNames(self.arena, self.sidecar);
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
fn mirrorTypes(self: *Emitter) Error!void {
|
|
705
|
+
const inlined = self.inlined;
|
|
706
|
+
for (self.sidecar.types.enums) |entry| {
|
|
707
|
+
// The 256-member bound is validated by the reader
|
|
708
|
+
// (sidecar.zig) before emission ever runs.
|
|
709
|
+
try self.print("\npub const {f} = enum(u8) {{", .{ident(entry.name)});
|
|
710
|
+
for (entry.members, 0..) |member, index| {
|
|
711
|
+
try self.print("\n {f} = {d},", .{ ident(member), index });
|
|
712
|
+
}
|
|
713
|
+
try self.raw("\n};\n");
|
|
714
|
+
}
|
|
715
|
+
for (self.sidecar.types.structs) |entry| {
|
|
716
|
+
if (std.mem.eql(u8, entry.name, self.sidecar.model)) continue;
|
|
717
|
+
if (nameListed(inlined, entry.name)) continue;
|
|
718
|
+
try self.print("\npub const {f} = struct {{", .{ident(entry.name)});
|
|
719
|
+
for (entry.fields) |field| {
|
|
720
|
+
try self.print("\n {f}: {s},", .{ ident(field.name), try self.spellRef(field.type, entry.name, field.name, try self.slotPath(entry.name, field.name)) });
|
|
721
|
+
}
|
|
722
|
+
try self.raw("\n};\n");
|
|
723
|
+
}
|
|
724
|
+
for (self.sidecar.types.unions) |entry| {
|
|
725
|
+
if (nameListed(inlined, entry.name)) continue;
|
|
726
|
+
try self.print("\npub const {f} = union(enum) {{", .{ident(entry.name)});
|
|
727
|
+
for (entry.arms) |arm| {
|
|
728
|
+
if (arm.payload == .void) {
|
|
729
|
+
try self.print("\n {f},", .{ident(arm.name)});
|
|
730
|
+
} else {
|
|
731
|
+
try self.print("\n {f}: {s},", .{ ident(arm.name), try self.spellRef(arm.payload, entry.name, arm.name, try self.slotPath(entry.name, arm.name)) });
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
try self.raw("\n};\n");
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/// The one TypeRef-to-Zig-spelling authority. `container`/`member`
|
|
739
|
+
/// name the reference site so synthesized entries inline here;
|
|
740
|
+
/// `slot` is the site's slot path in the sidecar's grammar (null
|
|
741
|
+
/// where no path form exists), consulted for the attested integer
|
|
742
|
+
/// class. Slice elements have no slot path — the format-1 grammar
|
|
743
|
+
/// cannot attest them, so they always keep the i64 spelling.
|
|
744
|
+
fn spellRef(self: *Emitter, ref: TypeRef, container: []const u8, member: []const u8, slot: ?[]const u8) Error![]const u8 {
|
|
745
|
+
return switch (ref) {
|
|
746
|
+
.bool => "bool",
|
|
747
|
+
.f64 => "f64",
|
|
748
|
+
.i64 => self.integerSpelling(slot),
|
|
749
|
+
.bytes => "[]const u8",
|
|
750
|
+
.void => "void",
|
|
751
|
+
.optional => |inner| try std.fmt.allocPrint(self.arena, "?{s}", .{try self.spellRef(inner.*, container, member, slot)}),
|
|
752
|
+
.slice => |elem| try std.fmt.allocPrint(self.arena, "[]const {s}", .{try self.spellRef(elem.*, container, member, null)}),
|
|
753
|
+
.node => |name| try std.fmt.allocPrint(self.arena, "*const {s}", .{try self.spellNamed(name, container, member)}),
|
|
754
|
+
.value => |name| try self.spellNamed(name, container, member),
|
|
755
|
+
.enum_ref, .union_ref => |name| try std.fmt.allocPrint(self.arena, "{f}", .{ident(name)}),
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
fn spellNamed(self: *Emitter, name: []const u8, container: []const u8, member: []const u8) Error![]const u8 {
|
|
760
|
+
if (!isSynthesizedRef(container, member, name) or !nameListed(self.inlined, name)) {
|
|
761
|
+
return std.fmt.allocPrint(self.arena, "{f}", .{ident(name)});
|
|
762
|
+
}
|
|
763
|
+
const entry = sidecar_mod.findStruct(self.sidecar.types, name) orelse
|
|
764
|
+
return std.fmt.allocPrint(self.arena, "{f}", .{ident(name)});
|
|
765
|
+
var text: std.ArrayListUnmanaged(u8) = .empty;
|
|
766
|
+
try text.appendSlice(self.arena, "struct {");
|
|
767
|
+
for (entry.fields, 0..) |field, index| {
|
|
768
|
+
if (index > 0) try text.appendSlice(self.arena, ",");
|
|
769
|
+
const piece = try std.fmt.allocPrint(self.arena, " {f}: {s}", .{ ident(field.name), try self.spellRef(field.type, entry.name, field.name, try self.slotPath(entry.name, field.name)) });
|
|
770
|
+
try text.appendSlice(self.arena, piece);
|
|
771
|
+
}
|
|
772
|
+
try text.appendSlice(self.arena, " }");
|
|
773
|
+
return text.items;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
// ---------------------------------------------------------- model
|
|
777
|
+
|
|
778
|
+
fn modelStruct(self: *Emitter) Error!void {
|
|
779
|
+
const model = sidecar_mod.findStruct(self.sidecar.types, self.sidecar.model) orelse {
|
|
780
|
+
self.diags.flag("model", "\"{s}\" names no struct in the type table", .{self.sidecar.model});
|
|
781
|
+
return;
|
|
782
|
+
};
|
|
783
|
+
try self.print("\npub const {f} = struct {{", .{ident(model.name)});
|
|
784
|
+
for (model.fields) |field| {
|
|
785
|
+
try self.print("\n {f}: {s},", .{ ident(field.name), try self.spellRef(field.type, model.name, field.name, try self.slotPath(model.name, field.name)) });
|
|
786
|
+
}
|
|
787
|
+
if (self.sidecar.model_helpers.len > 0) try self.raw("\n");
|
|
788
|
+
for (self.sidecar.model_helpers, 0..) |helper, index| {
|
|
789
|
+
const returns = try self.spellRef(helper.returns, "helpers", helper.name, try std.fmt.allocPrint(self.arena, "helpers.{s}.return", .{helper.name}));
|
|
790
|
+
if (helper.arena) {
|
|
791
|
+
// The build-arena scalar form: the view build hands its
|
|
792
|
+
// arena; the decoded result lives there.
|
|
793
|
+
try self.print("\n pub fn {f}(self: *const {f}, arena: std.mem.Allocator) {s} {{\n _ = self;\n return callHelper({s}, {d}, &.{{}}, arena);\n }}", .{ ident(helper.name), ident(model.name), returns, returns, index });
|
|
794
|
+
if (helper.params.len > 0) {
|
|
795
|
+
self.diags.flag("model_helpers", "helper \"{s}\": the arena form takes no extra parameters (the arena is the second parameter by contract)", .{helper.name});
|
|
796
|
+
}
|
|
797
|
+
continue;
|
|
798
|
+
}
|
|
799
|
+
if (helper.params.len == 0) {
|
|
800
|
+
try self.print("\n pub fn {f}(self: *const {f}) {s} {{\n _ = self;\n return callHelper({s}, {d}, &.{{}}, shim_rt.frameAllocator());\n }}", .{ ident(helper.name), ident(model.name), returns, returns, index });
|
|
801
|
+
} else {
|
|
802
|
+
// Extra parameters cross as their canonical encodings,
|
|
803
|
+
// concatenated in order (a tuple encodes as exactly
|
|
804
|
+
// that).
|
|
805
|
+
var params: std.ArrayListUnmanaged(u8) = .empty;
|
|
806
|
+
var tuple: std.ArrayListUnmanaged(u8) = .empty;
|
|
807
|
+
for (helper.params, 0..) |param, param_index| {
|
|
808
|
+
const spelled = try self.spellRef(param, "helpers", helper.name, try std.fmt.allocPrint(self.arena, "helpers.{s}.params[{d}]", .{ helper.name, param_index }));
|
|
809
|
+
try params.appendSlice(self.arena, try std.fmt.allocPrint(self.arena, ", p{d}: {s}", .{ param_index, spelled }));
|
|
810
|
+
if (param_index > 0) try tuple.appendSlice(self.arena, ", ");
|
|
811
|
+
try tuple.appendSlice(self.arena, try std.fmt.allocPrint(self.arena, "p{d}", .{param_index}));
|
|
812
|
+
}
|
|
813
|
+
try self.print("\n pub fn {f}(self: *const {f}{s}) {s} {{\n _ = self;\n const args_tuple = .{{ {s} }};\n const args = shim_rt.encodeAlloc(@TypeOf(args_tuple), args_tuple, shim_rt.frameAllocator());\n return callHelper({s}, {d}, args, shim_rt.frameAllocator());\n }}", .{ ident(helper.name), ident(model.name), params.items, returns, tuple.items, returns, index });
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
try self.unboundDecl(self.sidecar.model_unbound);
|
|
817
|
+
try self.raw("\n};\n");
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
fn unboundDecl(self: *Emitter, names: []const []const u8) Error!void {
|
|
821
|
+
if (names.len == 0) return;
|
|
822
|
+
try self.raw("\n\n pub const view_unbound = .{");
|
|
823
|
+
for (names, 0..) |name, index| {
|
|
824
|
+
if (index > 0) try self.raw(",");
|
|
825
|
+
try self.print(" \"{f}\"", .{std.zig.fmtString(name)});
|
|
826
|
+
}
|
|
827
|
+
try self.raw(" };");
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// ------------------------------------------------------------ msg
|
|
831
|
+
|
|
832
|
+
fn msgUnion(self: *Emitter) Error!void {
|
|
833
|
+
try self.print("\npub const {f} = union(enum) {{", .{ident(self.sidecar.msg.name)});
|
|
834
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
835
|
+
switch (arm.payload) {
|
|
836
|
+
.void => try self.print("\n {f},", .{ident(arm.name)}),
|
|
837
|
+
.bytes => try self.print("\n {f}: []const u8,", .{ident(arm.name)}),
|
|
838
|
+
.number => |class| try self.print("\n {f}: {s},", .{ ident(arm.name), try self.numberSpelling(class, try self.slotPath(self.sidecar.msg.name, arm.name)) }),
|
|
839
|
+
// The two-field family carries no field-order fact; the
|
|
840
|
+
// mirror declares the number field first, the emitted
|
|
841
|
+
// convention of every producer of this shape. A record
|
|
842
|
+
// declared bytes-first must ride the record family (its
|
|
843
|
+
// table entry carries order explicitly) — see
|
|
844
|
+
// SCHEMA-GAPS.md.
|
|
845
|
+
.number_bytes => |desc| try self.print("\n {f}: struct {{ {f}: {s}, {f}: []const u8 }},", .{ ident(arm.name), ident(desc.number_field), try self.numberSpelling(desc.number_class, try self.numberBytesSlot(arm.name, desc.number_field)), ident(desc.bytes_field) }),
|
|
846
|
+
.record => |name| try self.print("\n {f}: {s},", .{ ident(arm.name), try self.spellNamed(name, self.sidecar.msg.name, arm.name) }),
|
|
847
|
+
.union_ref, .enum_ref => |name| try self.print("\n {f}: {f},", .{ ident(arm.name), ident(name) }),
|
|
848
|
+
.scalar => |ref| try self.print("\n {f}: {s},", .{ ident(arm.name), try self.spellRef(ref, self.sidecar.msg.name, arm.name, try self.slotPath(self.sidecar.msg.name, arm.name)) }),
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
try self.unboundDecl(self.sidecar.msg.unbound);
|
|
852
|
+
try self.raw("\n};\n");
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
/// The host wiring names its roots by fixed spelling — the staged
|
|
856
|
+
/// main re-exports `core.Model` and `core.Msg`, and the adapter and
|
|
857
|
+
/// bridge reflect those decls — so a sidecar whose own names differ
|
|
858
|
+
/// gets aliases (same types; aliases are transparent to every
|
|
859
|
+
/// reflecting consumer).
|
|
860
|
+
fn wiringAliases(self: *Emitter) Error!void {
|
|
861
|
+
if (!std.mem.eql(u8, self.sidecar.model, "Model")) {
|
|
862
|
+
try self.print("\n/// The host wiring's spelling for the root state type.\npub const Model = {f};\n", .{ident(self.sidecar.model)});
|
|
863
|
+
}
|
|
864
|
+
if (!std.mem.eql(u8, self.sidecar.msg.name, "Msg")) {
|
|
865
|
+
try self.print("\n/// The host wiring's spelling for the message union.\npub const Msg = {f};\n", .{ident(self.sidecar.msg.name)});
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
fn tagTable(self: *Emitter) Error!void {
|
|
870
|
+
try self.raw(
|
|
871
|
+
\\
|
|
872
|
+
\\/// Declaration-order wire tags: the arm's index in this table IS
|
|
873
|
+
\\/// its wire tag (dense, u8). The sidecar is the tag authority; the
|
|
874
|
+
\\/// compiled object's lowering assigns the same order, and the
|
|
875
|
+
\\/// boot-time build_id fence proves both came from one compile.
|
|
876
|
+
\\pub const msg_tags = [_][]const u8{
|
|
877
|
+
);
|
|
878
|
+
for (self.sidecar.msg.arms) |arm| {
|
|
879
|
+
try self.print("\n \"{f}\",", .{std.zig.fmtString(arm.name)});
|
|
880
|
+
}
|
|
881
|
+
try self.raw("\n};\n");
|
|
882
|
+
try self.print(
|
|
883
|
+
\\
|
|
884
|
+
\\comptime {{
|
|
885
|
+
\\ // The union and the tag table are emitted from one arm list;
|
|
886
|
+
\\ // hold them equal anyway so a hand edit cannot skew dispatch.
|
|
887
|
+
\\ const fields = @typeInfo({f}).@"union".fields;
|
|
888
|
+
\\ if (fields.len != msg_tags.len) @compileError("core_shim: msg_tags and the message union disagree — regenerate from the sidecar");
|
|
889
|
+
\\ for (fields, msg_tags) |field, tag_name| {{
|
|
890
|
+
\\ if (!std.mem.eql(u8, field.name, tag_name)) @compileError("core_shim: msg_tags and the message union disagree — regenerate from the sidecar");
|
|
891
|
+
\\ }}
|
|
892
|
+
\\}}
|
|
893
|
+
\\
|
|
894
|
+
, .{ident(self.sidecar.msg.name)});
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
// --------------------------------------------------- entry points
|
|
898
|
+
|
|
899
|
+
fn entryPoints(self: *Emitter) Error!void {
|
|
900
|
+
const model_name = try std.fmt.allocPrint(self.arena, "{f}", .{ident(self.sidecar.model)});
|
|
901
|
+
|
|
902
|
+
// Boot. Export attestation first (V11's link-time half: the
|
|
903
|
+
// bindings resolve lazily, so referencing every attested
|
|
904
|
+
// symbol here turns a missing export into a LINK failure
|
|
905
|
+
// instead of a latent hole), then identity (pure getters, the
|
|
906
|
+
// staleness fence), then the sink, then init — the ABI's
|
|
907
|
+
// required ordering.
|
|
908
|
+
try self.raw(
|
|
909
|
+
\\
|
|
910
|
+
\\fn boot() void {
|
|
911
|
+
\\ referenceAttestedExports();
|
|
912
|
+
\\ shim_rt.verifyIdentity(abi.abi_version_fn(), abi.build_id(), sidecar_abi_version, sidecar_build_id);
|
|
913
|
+
\\ abi.set_panic_sink(shim_rt.panicSink, null);
|
|
914
|
+
\\ abi.init();
|
|
915
|
+
\\}
|
|
916
|
+
\\
|
|
917
|
+
\\/// The sidecar's abi.exports list is a biconditional attestation:
|
|
918
|
+
\\/// every listed symbol exists in the object, and the object
|
|
919
|
+
\\/// exports nothing else under the prefix. Referencing each listed
|
|
920
|
+
\\/// symbol makes the LINKER prove the "exists" direction against
|
|
921
|
+
\\/// the real binary. The "nothing else" direction is not provable
|
|
922
|
+
\\/// from the consumer side — a linker ignores unreferenced extra
|
|
923
|
+
\\/// exports, and this generator never opens the object — so it
|
|
924
|
+
\\/// belongs to the producer's conformance suite, which probes the
|
|
925
|
+
\\/// object's full symbol table at compile time.
|
|
926
|
+
\\fn referenceAttestedExports() void {
|
|
927
|
+
\\
|
|
928
|
+
);
|
|
929
|
+
for (self.sidecar.abi.exports) |suffix| {
|
|
930
|
+
const binding = if (std.mem.eql(u8, suffix, "abi_version")) "abi_version_fn" else suffix;
|
|
931
|
+
try self.print(" std.mem.doNotOptimizeAway(abi.{f});\n", .{ident(binding)});
|
|
932
|
+
}
|
|
933
|
+
try self.raw("}\n");
|
|
934
|
+
|
|
935
|
+
if (self.sidecar.init_returns_cmd) {
|
|
936
|
+
try self.print(
|
|
937
|
+
\\
|
|
938
|
+
\\pub const InitResult = struct {{ model: *const {s}, cmd: rt.Cmd }};
|
|
939
|
+
\\
|
|
940
|
+
\\pub fn initialModel() InitResult {{
|
|
941
|
+
\\ boot();
|
|
942
|
+
\\ var cmd_ptr: [*]const u8 = undefined;
|
|
943
|
+
\\ var cmd_len: usize = 0;
|
|
944
|
+
\\ abi.boot_cmd(&cmd_ptr, &cmd_len);
|
|
945
|
+
\\ return .{{ .model = snapshotModel(), .cmd = cmd_ptr[0..cmd_len] }};
|
|
946
|
+
\\}}
|
|
947
|
+
\\
|
|
948
|
+
, .{model_name});
|
|
949
|
+
} else {
|
|
950
|
+
try self.print(
|
|
951
|
+
\\
|
|
952
|
+
\\pub fn initialModel() *const {s} {{
|
|
953
|
+
\\ boot();
|
|
954
|
+
\\ return snapshotModel();
|
|
955
|
+
\\}}
|
|
956
|
+
\\
|
|
957
|
+
, .{model_name});
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
if (self.sidecar.update_returns_cmd) {
|
|
961
|
+
try self.print(
|
|
962
|
+
\\
|
|
963
|
+
\\pub const UpdateResult = struct {{ model: *const {s}, cmd: rt.Cmd }};
|
|
964
|
+
\\
|
|
965
|
+
\\pub fn update(model: *const {s}, msg: {f}) UpdateResult {{
|
|
966
|
+
\\
|
|
967
|
+
, .{ model_name, model_name, ident(self.sidecar.msg.name) });
|
|
968
|
+
} else {
|
|
969
|
+
try self.print(
|
|
970
|
+
\\
|
|
971
|
+
\\pub fn update(model: *const {s}, msg: {f}) *const {s} {{
|
|
972
|
+
\\
|
|
973
|
+
, .{ model_name, ident(self.sidecar.msg.name), model_name });
|
|
974
|
+
}
|
|
975
|
+
try self.raw(
|
|
976
|
+
\\ // The compiled core owns the committed state; the mirror root
|
|
977
|
+
\\ // the host passes is a decoded copy, so only tag and payload
|
|
978
|
+
\\ // cross the boundary.
|
|
979
|
+
\\ _ = model;
|
|
980
|
+
\\ var cmd_ptr: [*]const u8 = undefined;
|
|
981
|
+
\\ var cmd_len: usize = 0;
|
|
982
|
+
\\ switch (msg) {
|
|
983
|
+
\\
|
|
984
|
+
);
|
|
985
|
+
for (self.sidecar.msg.arms, 0..) |arm, tag| {
|
|
986
|
+
try self.dispatchArm(arm, tag);
|
|
987
|
+
}
|
|
988
|
+
try self.raw(" }\n");
|
|
989
|
+
if (self.sidecar.update_returns_cmd) {
|
|
990
|
+
try self.raw(" return .{ .model = snapshotModel(), .cmd = cmd_ptr[0..cmd_len] };\n}\n");
|
|
991
|
+
} else {
|
|
992
|
+
try self.raw(" _ = cmd_ptr;\n _ = cmd_len;\n return snapshotModel();\n}\n");
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
if (self.sidecar.has_subscriptions) {
|
|
996
|
+
try self.print(
|
|
997
|
+
\\
|
|
998
|
+
\\pub fn subscriptions(model: *const {s}) rt.Sub {{
|
|
999
|
+
\\ // The core derives subscriptions from its own committed
|
|
1000
|
+
\\ // model; the mirror root only satisfies the signature the
|
|
1001
|
+
\\ // host layer expects.
|
|
1002
|
+
\\ _ = model;
|
|
1003
|
+
\\ var subs_ptr: [*]const u8 = undefined;
|
|
1004
|
+
\\ var subs_len: usize = 0;
|
|
1005
|
+
\\ abi.subscriptions(&subs_ptr, &subs_len);
|
|
1006
|
+
\\ return subs_ptr[0..subs_len];
|
|
1007
|
+
\\}}
|
|
1008
|
+
\\
|
|
1009
|
+
, .{model_name});
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
try self.print(
|
|
1013
|
+
\\
|
|
1014
|
+
\\/// Commit happens inside every dispatch entry core-side; the
|
|
1015
|
+
\\/// decoded mirror is already the committed value, so the frame-end
|
|
1016
|
+
\\/// commit is identity here.
|
|
1017
|
+
\\pub fn commitModelRoot(next: *const {s}) *const {s} {{
|
|
1018
|
+
\\ return next;
|
|
1019
|
+
\\}}
|
|
1020
|
+
\\
|
|
1021
|
+
, .{ model_name, model_name });
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
fn dispatchArm(self: *Emitter, arm: sidecar_mod.MsgArm, tag: usize) Error!void {
|
|
1025
|
+
const name = try std.fmt.allocPrint(self.arena, "{f}", .{ident(arm.name)});
|
|
1026
|
+
switch (arm.payload) {
|
|
1027
|
+
.void => try self.print(" .{s} => abi.dispatch_void({d}, &cmd_ptr, &cmd_len),\n", .{ name, tag }),
|
|
1028
|
+
.bytes => try self.print(" .{s} => |payload| abi.dispatch_bytes({d}, payload.ptr, payload.len, &cmd_ptr, &cmd_len),\n", .{ name, tag }),
|
|
1029
|
+
.number => |class| switch (class) {
|
|
1030
|
+
.f64 => try self.print(" .{s} => |payload| abi.dispatch_number({d}, payload, &cmd_ptr, &cmd_len),\n", .{ name, tag }),
|
|
1031
|
+
.i64 => try self.print(" .{s} => |payload| abi.dispatch_number({d}, {s}, &cmd_ptr, &cmd_len),\n", .{ name, tag, try self.exactCall(try self.slotPath(self.sidecar.msg.name, arm.name), "payload") }),
|
|
1032
|
+
},
|
|
1033
|
+
.number_bytes => |desc| {
|
|
1034
|
+
const number_expr = switch (desc.number_class) {
|
|
1035
|
+
.f64 => try std.fmt.allocPrint(self.arena, "payload.{f}", .{ident(desc.number_field)}),
|
|
1036
|
+
.i64 => try self.exactCall(try self.numberBytesSlot(arm.name, desc.number_field), try std.fmt.allocPrint(self.arena, "payload.{f}", .{ident(desc.number_field)})),
|
|
1037
|
+
};
|
|
1038
|
+
try self.print(" .{s} => |payload| abi.dispatch_number_bytes({d}, {s}, payload.{f}.ptr, payload.{f}.len, &cmd_ptr, &cmd_len),\n", .{ name, tag, number_expr, ident(desc.bytes_field), ident(desc.bytes_field) });
|
|
1039
|
+
},
|
|
1040
|
+
.enum_ref => try self.print(" .{s} => |payload| abi.dispatch_enum({d}, @intCast(@intFromEnum(payload)), &cmd_ptr, &cmd_len),\n", .{ name, tag }),
|
|
1041
|
+
.union_ref => |type_name| {
|
|
1042
|
+
if (isTextInputUnion(self.sidecar, type_name)) {
|
|
1043
|
+
try self.print(" .{s} => |payload| {{\n const encoded = shim_rt.encodeAlloc(@TypeOf(payload), payload, shim_rt.frameAllocator());\n abi.dispatch_text_input({d}, encoded.ptr, encoded.len, &cmd_ptr, &cmd_len);\n }},\n", .{ name, tag });
|
|
1044
|
+
} else {
|
|
1045
|
+
try self.print(" .{s} => |payload| {{\n const encoded = shim_rt.encodeAlloc(@TypeOf(payload), payload, shim_rt.frameAllocator());\n abi.dispatch_record({d}, encoded.ptr, encoded.len, &cmd_ptr, &cmd_len);\n }},\n", .{ name, tag });
|
|
1046
|
+
}
|
|
1047
|
+
},
|
|
1048
|
+
.record => |type_name| {
|
|
1049
|
+
if (self.scrollStateFields(type_name)) |fields| {
|
|
1050
|
+
var scalars: std.ArrayListUnmanaged(u8) = .empty;
|
|
1051
|
+
for (fields, 0..) |field, index| {
|
|
1052
|
+
if (index > 0) try scalars.appendSlice(self.arena, ", ");
|
|
1053
|
+
const record = sidecar_mod.findStruct(self.sidecar.types, type_name).?;
|
|
1054
|
+
const expr = switch (record.fields[field].type) {
|
|
1055
|
+
.i64 => try self.exactCall(try self.slotPath(type_name, record.fields[field].name), try std.fmt.allocPrint(self.arena, "payload.{f}", .{ident(record.fields[field].name)})),
|
|
1056
|
+
else => try std.fmt.allocPrint(self.arena, "payload.{f}", .{ident(record.fields[field].name)}),
|
|
1057
|
+
};
|
|
1058
|
+
try scalars.appendSlice(self.arena, expr);
|
|
1059
|
+
}
|
|
1060
|
+
try self.print(" .{s} => |payload| abi.dispatch_scroll_state({d}, {s}, &cmd_ptr, &cmd_len),\n", .{ name, tag, scalars.items });
|
|
1061
|
+
} else {
|
|
1062
|
+
try self.print(" .{s} => |payload| {{\n const encoded = shim_rt.encodeAlloc(@TypeOf(payload), payload, shim_rt.frameAllocator());\n abi.dispatch_record({d}, encoded.ptr, encoded.len, &cmd_ptr, &cmd_len);\n }},\n", .{ name, tag });
|
|
1063
|
+
}
|
|
1064
|
+
},
|
|
1065
|
+
.scalar => |ref| switch (ref) {
|
|
1066
|
+
.bool => try self.print(" .{s} => |payload| abi.dispatch_bool({d}, @intFromBool(payload), &cmd_ptr, &cmd_len),\n", .{ name, tag }),
|
|
1067
|
+
.f64 => try self.print(" .{s} => |payload| abi.dispatch_number({d}, payload, &cmd_ptr, &cmd_len),\n", .{ name, tag }),
|
|
1068
|
+
.i64 => try self.print(" .{s} => |payload| abi.dispatch_number({d}, {s}, &cmd_ptr, &cmd_len),\n", .{ name, tag, try self.exactCall(try self.slotPath(self.sidecar.msg.name, arm.name), "payload") }),
|
|
1069
|
+
.bytes => try self.print(" .{s} => |payload| abi.dispatch_bytes({d}, payload.ptr, payload.len, &cmd_ptr, &cmd_len),\n", .{ name, tag }),
|
|
1070
|
+
else => self.diags.flag("msg.arms", "arm \"{s}\": ABI version 1 has no dispatch entry for this scalar shape (bool, number, and bytes scalars only)", .{arm.name}),
|
|
1071
|
+
},
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
/// Declaration-order field indexes of a scroll-state record, in the
|
|
1076
|
+
/// ABI entry's parameter order (the eight per-axis scalars,
|
|
1077
|
+
/// offset_x through content_extent_y) — or null when the record is
|
|
1078
|
+
/// not that shape.
|
|
1079
|
+
fn scrollStateFields(self: *Emitter, type_name: []const u8) ?[8]usize {
|
|
1080
|
+
const entry = sidecar_mod.findStruct(self.sidecar.types, type_name) orelse return null;
|
|
1081
|
+
if (entry.fields.len != 8) return null;
|
|
1082
|
+
const spellings = [_][8][]const u8{ scroll_state_fields_ts, scroll_state_fields_canvas };
|
|
1083
|
+
for (spellings) |names| {
|
|
1084
|
+
var indexes: [8]usize = undefined;
|
|
1085
|
+
var all_found = true;
|
|
1086
|
+
for (names, 0..) |field_name, position| {
|
|
1087
|
+
indexes[position] = for (entry.fields, 0..) |field, index| {
|
|
1088
|
+
const numeric = field.type == .f64 or field.type == .i64;
|
|
1089
|
+
if (std.mem.eql(u8, field.name, field_name) and numeric) break index;
|
|
1090
|
+
} else {
|
|
1091
|
+
all_found = false;
|
|
1092
|
+
break;
|
|
1093
|
+
};
|
|
1094
|
+
if (!all_found) break;
|
|
1095
|
+
}
|
|
1096
|
+
if (all_found) return indexes;
|
|
1097
|
+
}
|
|
1098
|
+
return null;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
// -------------------------------------------------------- channels
|
|
1102
|
+
|
|
1103
|
+
fn channels(self: *Emitter) Error!void {
|
|
1104
|
+
const chan = self.sidecar.channels;
|
|
1105
|
+
const any_fn_channel = chan.command_msg or chan.frame_msg or chan.key_msg or chan.pinch_msg;
|
|
1106
|
+
|
|
1107
|
+
if (chan.command_msg) {
|
|
1108
|
+
try self.print(
|
|
1109
|
+
\\
|
|
1110
|
+
\\/// Menus, shortcuts, and chrome tabs dispatch through the core's
|
|
1111
|
+
\\/// exported command mapper.
|
|
1112
|
+
\\pub fn commandMsg(name: []const u8) ?{f} {{
|
|
1113
|
+
\\ var out_ptr: [*]const u8 = &shim_rt.channel_out_guard;
|
|
1114
|
+
\\ var out_len: usize = 0;
|
|
1115
|
+
\\ abi.command_msg(name.ptr, name.len, &out_ptr, &out_len);
|
|
1116
|
+
\\ return msgFromEnvelope(shim_rt.channelEnvelopeBytes(out_ptr, out_len));
|
|
1117
|
+
\\}}
|
|
1118
|
+
\\
|
|
1119
|
+
, .{ident(self.sidecar.msg.name)});
|
|
1120
|
+
}
|
|
1121
|
+
if (chan.frame_msg) {
|
|
1122
|
+
try self.print(
|
|
1123
|
+
\\
|
|
1124
|
+
\\/// The presented-frame channel's record: canvas points plus the
|
|
1125
|
+
\\/// frame clock in fractional milliseconds. The adapter builds it
|
|
1126
|
+
\\/// by field name, exactly as for transpiler output.
|
|
1127
|
+
\\pub const FrameEvent = struct {{
|
|
1128
|
+
\\ width: f64,
|
|
1129
|
+
\\ height: f64,
|
|
1130
|
+
\\ timestampMs: f64,
|
|
1131
|
+
\\ intervalMs: f64,
|
|
1132
|
+
\\}};
|
|
1133
|
+
\\
|
|
1134
|
+
\\pub fn frameMsg(model: *const {f}, frame: FrameEvent) ?{f} {{
|
|
1135
|
+
\\ _ = model;
|
|
1136
|
+
\\ var out_ptr: [*]const u8 = &shim_rt.channel_out_guard;
|
|
1137
|
+
\\ var out_len: usize = 0;
|
|
1138
|
+
\\ abi.frame_msg(frame.width, frame.height, frame.timestampMs, frame.intervalMs, &out_ptr, &out_len);
|
|
1139
|
+
\\ return msgFromEnvelope(shim_rt.channelEnvelopeBytes(out_ptr, out_len));
|
|
1140
|
+
\\}}
|
|
1141
|
+
\\
|
|
1142
|
+
, .{ ident(self.sidecar.model), ident(self.sidecar.msg.name) });
|
|
1143
|
+
}
|
|
1144
|
+
if (chan.key_msg) {
|
|
1145
|
+
try self.print(
|
|
1146
|
+
\\
|
|
1147
|
+
\\/// The key-fallback channel's record: the lowercased key name
|
|
1148
|
+
\\/// plus the four modifier booleans.
|
|
1149
|
+
\\pub const KeyEvent = struct {{
|
|
1150
|
+
\\ key: []const u8,
|
|
1151
|
+
\\ shift: bool,
|
|
1152
|
+
\\ control: bool,
|
|
1153
|
+
\\ alt: bool,
|
|
1154
|
+
\\ super: bool,
|
|
1155
|
+
\\}};
|
|
1156
|
+
\\
|
|
1157
|
+
\\pub fn keyMsg(key: KeyEvent) ?{f} {{
|
|
1158
|
+
\\ var out_ptr: [*]const u8 = &shim_rt.channel_out_guard;
|
|
1159
|
+
\\ var out_len: usize = 0;
|
|
1160
|
+
\\ abi.key_msg(key.key.ptr, key.key.len, @intFromBool(key.shift), @intFromBool(key.control), @intFromBool(key.alt), @intFromBool(key.super), &out_ptr, &out_len);
|
|
1161
|
+
\\ return msgFromEnvelope(shim_rt.channelEnvelopeBytes(out_ptr, out_len));
|
|
1162
|
+
\\}}
|
|
1163
|
+
\\
|
|
1164
|
+
, .{ident(self.sidecar.msg.name)});
|
|
1165
|
+
}
|
|
1166
|
+
if (chan.pinch_msg) {
|
|
1167
|
+
try self.print(
|
|
1168
|
+
\\
|
|
1169
|
+
\\pub const PinchPhase = enum(u8) {{ begin = 0, change = 1, end = 2 }};
|
|
1170
|
+
\\
|
|
1171
|
+
\\/// The pinch channel's record: window/view source identity, the
|
|
1172
|
+
\\/// multiplicative magnification delta, and the view-local anchor.
|
|
1173
|
+
\\pub const PinchEvent = struct {{
|
|
1174
|
+
\\ windowId: f64,
|
|
1175
|
+
\\ label: []const u8,
|
|
1176
|
+
\\ phase: PinchPhase,
|
|
1177
|
+
\\ scale: f64,
|
|
1178
|
+
\\ x: f64,
|
|
1179
|
+
\\ y: f64,
|
|
1180
|
+
\\}};
|
|
1181
|
+
\\
|
|
1182
|
+
\\pub fn pinchMsg(pinch: PinchEvent) ?{f} {{
|
|
1183
|
+
\\ var out_ptr: [*]const u8 = &shim_rt.channel_out_guard;
|
|
1184
|
+
\\ var out_len: usize = 0;
|
|
1185
|
+
\\ abi.pinch_msg(pinch.windowId, pinch.label.ptr, pinch.label.len, @intCast(@intFromEnum(pinch.phase)), pinch.scale, pinch.x, pinch.y, &out_ptr, &out_len);
|
|
1186
|
+
\\ return msgFromEnvelope(shim_rt.channelEnvelopeBytes(out_ptr, out_len));
|
|
1187
|
+
\\}}
|
|
1188
|
+
\\
|
|
1189
|
+
, .{ident(self.sidecar.msg.name)});
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
if (chan.appearance_msg) |arm_name| {
|
|
1193
|
+
try self.print("\n/// The arm the host fills with the structural appearance record.\npub const appearanceMsg = \"{f}\";\n", .{std.zig.fmtString(arm_name)});
|
|
1194
|
+
}
|
|
1195
|
+
if (chan.chrome_msg) |arm_name| {
|
|
1196
|
+
try self.print("\n/// The arm the host fills with the structural window-chrome record.\npub const chromeMsg = \"{f}\";\n", .{std.zig.fmtString(arm_name)});
|
|
1197
|
+
}
|
|
1198
|
+
if (chan.env_msgs.len > 0) {
|
|
1199
|
+
try self.raw(
|
|
1200
|
+
\\
|
|
1201
|
+
\\/// The launch-time environment channel: each variable present at
|
|
1202
|
+
\\/// launch dispatches one journaled Msg on its bytes arm, right
|
|
1203
|
+
\\/// after the boot command. The core itself never reads the
|
|
1204
|
+
\\/// environment; replay carries the recorded values.
|
|
1205
|
+
\\pub const envMsgs = .{
|
|
1206
|
+
\\
|
|
1207
|
+
);
|
|
1208
|
+
for (chan.env_msgs) |entry| {
|
|
1209
|
+
try self.print(" .{{ .env = \"{f}\", .msg = \"{f}\" }},\n", .{ std.zig.fmtString(entry.env), std.zig.fmtString(entry.msg) });
|
|
1210
|
+
}
|
|
1211
|
+
try self.raw("};\n");
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
if (any_fn_channel) {
|
|
1215
|
+
try self.print(
|
|
1216
|
+
\\
|
|
1217
|
+
\\/// Unpack a channel entry's bytes envelope — [produced u8][tag u8]
|
|
1218
|
+
\\/// [payload…]: the header says whether a message was produced, the
|
|
1219
|
+
\\/// wire tag picks the arm, and the payload rides the canonical
|
|
1220
|
+
\\/// value encoding of that arm's mirror payload type. Malformed
|
|
1221
|
+
\\/// framing panics with a teaching in shim_rt.channelEnvelope; a
|
|
1222
|
+
\\/// tag past the declared arms panics here.
|
|
1223
|
+
\\fn msgFromEnvelope(envelope: []const u8) ?{f} {{
|
|
1224
|
+
\\ const header = shim_rt.channelEnvelope(envelope);
|
|
1225
|
+
\\ if (!header.produced) return null;
|
|
1226
|
+
\\ switch (header.tag) {{
|
|
1227
|
+
\\
|
|
1228
|
+
, .{ident(self.sidecar.msg.name)});
|
|
1229
|
+
for (self.sidecar.msg.arms, 0..) |arm, tag| {
|
|
1230
|
+
if (arm.payload == .void) {
|
|
1231
|
+
try self.print(" {d} => {{\n shim_rt.assertVoidPayload(header.payload);\n return .{f};\n }},\n", .{ tag, ident(arm.name) });
|
|
1232
|
+
} else {
|
|
1233
|
+
try self.print(" {d} => return .{{ .{f} = shim_rt.decodeExact(@FieldType({f}, \"{f}\"), header.payload, shim_rt.frameAllocator()) }},\n", .{ tag, ident(arm.name), ident(self.sidecar.msg.name), std.zig.fmtString(arm.name) });
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
try self.raw(
|
|
1237
|
+
\\ else => @panic("the compiled core's channel envelope carries a message tag past the declared arms — the core and its contract sidecar disagree; rebuild the app so both come from one compile"),
|
|
1238
|
+
\\ }
|
|
1239
|
+
\\}
|
|
1240
|
+
\\
|
|
1241
|
+
);
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
// ----------------------------------------------------- helper glue
|
|
1246
|
+
|
|
1247
|
+
fn helperPlumbing(self: *Emitter) Error!void {
|
|
1248
|
+
if (self.sidecar.model_helpers.len == 0) return;
|
|
1249
|
+
try self.raw(
|
|
1250
|
+
\\
|
|
1251
|
+
\\/// Call the compiled core's exported helper at `index` (the
|
|
1252
|
+
\\/// sidecar's model_helpers order IS the call index) and decode the
|
|
1253
|
+
\\/// result. An unknown index traps core-side through the panic
|
|
1254
|
+
\\/// sink; there is no status to check. Results are read-arena
|
|
1255
|
+
\\/// resident, the same until-next-dispatch lifetime view builds get
|
|
1256
|
+
\\/// from transpiler output.
|
|
1257
|
+
\\fn callHelper(comptime T: type, index: u32, args: []const u8, allocator: std.mem.Allocator) T {
|
|
1258
|
+
\\ var out_ptr: [*]const u8 = undefined;
|
|
1259
|
+
\\ var out_len: usize = 0;
|
|
1260
|
+
\\ abi.helper_call(index, args.ptr, args.len, &out_ptr, &out_len);
|
|
1261
|
+
\\ return shim_rt.decodeExact(T, out_ptr[0..out_len], allocator);
|
|
1262
|
+
\\}
|
|
1263
|
+
\\
|
|
1264
|
+
);
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
// -------------------------------------------------- snapshot glue
|
|
1268
|
+
|
|
1269
|
+
fn snapshotPlumbing(self: *Emitter) Error!void {
|
|
1270
|
+
try self.print(
|
|
1271
|
+
\\
|
|
1272
|
+
\\/// Decode the committed model from the core's snapshot buffer
|
|
1273
|
+
\\/// (canonical value encoding of the root record, snapshot format
|
|
1274
|
+
\\/// {d}). The decoded mirror lives in the shim's model arena until
|
|
1275
|
+
\\/// the next snapshot — it must survive frame resets, exactly as the
|
|
1276
|
+
\\/// transpiler lane's committed heap does.
|
|
1277
|
+
\\fn snapshotModel() *const {f} {{
|
|
1278
|
+
\\ var snap_ptr: [*]const u8 = undefined;
|
|
1279
|
+
\\ var snap_len: usize = 0;
|
|
1280
|
+
\\ abi.model_snapshot(&snap_ptr, &snap_len);
|
|
1281
|
+
\\ return shim_rt.decodeSnapshot({f}, snap_ptr[0..snap_len]);
|
|
1282
|
+
\\}}
|
|
1283
|
+
\\
|
|
1284
|
+
, .{ self.sidecar.abi.snapshot_format, ident(self.sidecar.model), ident(self.sidecar.model) });
|
|
1285
|
+
}
|
|
1286
|
+
};
|
|
1287
|
+
|
|
1288
|
+
// ------------------------------------------------- shared analysis
|
|
1289
|
+
//
|
|
1290
|
+
// Both projections of the sidecar — the Zig mirror and the TypeScript
|
|
1291
|
+
// facade — must agree on which table entries are synthesized anonymous
|
|
1292
|
+
// records, or the two sides would declare different type surfaces for
|
|
1293
|
+
// one contract.
|
|
1294
|
+
|
|
1295
|
+
/// A table entry whose name follows the schema's synthesized pattern
|
|
1296
|
+
/// for a reference site (`<Container>_<member>`).
|
|
1297
|
+
///
|
|
1298
|
+
/// Current sidecars carry an `origin` for authored declarations and null
|
|
1299
|
+
/// for synthesized names, so provenance decides first. Older sidecars have
|
|
1300
|
+
/// no origins; their null entries retain the name-pattern fallback so the
|
|
1301
|
+
/// reader remains backward compatible with that generation.
|
|
1302
|
+
pub fn isSynthesizedRef(container: []const u8, member: []const u8, name: []const u8) bool {
|
|
1303
|
+
if (name.len != container.len + 1 + member.len) return false;
|
|
1304
|
+
return std.mem.startsWith(u8, name, container) and
|
|
1305
|
+
name[container.len] == '_' and
|
|
1306
|
+
std.mem.endsWith(u8, name, member);
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
/// The table entries emitted inline at their single reference site:
|
|
1310
|
+
/// pattern-matching names referenced exactly once in the whole sidecar.
|
|
1311
|
+
pub fn inlinedTableNames(arena: std.mem.Allocator, sidecar: Sidecar) error{OutOfMemory}![]const []const u8 {
|
|
1312
|
+
var candidates: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
1313
|
+
for (sidecar.types.structs) |entry| {
|
|
1314
|
+
for (entry.fields) |field| {
|
|
1315
|
+
try noteCandidate(&candidates, arena, entry.name, field.name, field.type);
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
for (sidecar.types.unions) |entry| {
|
|
1319
|
+
for (entry.arms) |arm| {
|
|
1320
|
+
try noteCandidate(&candidates, arena, entry.name, arm.name, arm.payload);
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
for (sidecar.msg.arms) |arm| {
|
|
1324
|
+
switch (arm.payload) {
|
|
1325
|
+
.record => |name| if (isSynthesizedRef(sidecar.msg.name, arm.name, name)) {
|
|
1326
|
+
try candidates.append(arena, name);
|
|
1327
|
+
},
|
|
1328
|
+
else => {},
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
var names: std.ArrayListUnmanaged([]const u8) = .empty;
|
|
1332
|
+
for (candidates.items) |name| {
|
|
1333
|
+
if (referenceCount(sidecar, name) != 1) continue;
|
|
1334
|
+
// A genuine synthesized record carries at least two fields (a
|
|
1335
|
+
// one-field inline arm collapses to a bare payload at emission
|
|
1336
|
+
// and a zero-field arm is void, so neither ever gets tabled).
|
|
1337
|
+
// Inlining a smaller pattern-named record would let the facade
|
|
1338
|
+
// compile collapse it while the mirror keeps a record — the two
|
|
1339
|
+
// projections must never diverge, so such records stay named.
|
|
1340
|
+
const entry = sidecar_mod.findStruct(sidecar.types, name) orelse continue;
|
|
1341
|
+
// A declaring module proves this is an authored type even when its
|
|
1342
|
+
// spelling happens to match the synthesized-name convention.
|
|
1343
|
+
if (entry.origin != null) continue;
|
|
1344
|
+
if (entry.fields.len < 2) continue;
|
|
1345
|
+
try names.append(arena, name);
|
|
1346
|
+
}
|
|
1347
|
+
return names.items;
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
fn noteCandidate(names: *std.ArrayListUnmanaged([]const u8), arena: std.mem.Allocator, container: []const u8, member: []const u8, ref: TypeRef) error{OutOfMemory}!void {
|
|
1351
|
+
switch (ref) {
|
|
1352
|
+
.optional => |inner| try noteCandidate(names, arena, container, member, inner.*),
|
|
1353
|
+
.slice => |elem| try noteCandidate(names, arena, container, member, elem.*),
|
|
1354
|
+
// VALUE references only: inlining flattens the record into its
|
|
1355
|
+
// one site, which is a by-value layout — a node-stored payload
|
|
1356
|
+
// keeps its named declaration (and its pointer) however its
|
|
1357
|
+
// name is spelled.
|
|
1358
|
+
.value => |name| if (isSynthesizedRef(container, member, name)) {
|
|
1359
|
+
try names.append(arena, name);
|
|
1360
|
+
},
|
|
1361
|
+
else => {},
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
/// How many reference sites name a table entry, across the whole
|
|
1366
|
+
/// sidecar (model root, table fields and arms, msg descriptors, helper
|
|
1367
|
+
/// signatures).
|
|
1368
|
+
pub fn referenceCount(sidecar: Sidecar, name: []const u8) usize {
|
|
1369
|
+
var count: usize = 0;
|
|
1370
|
+
if (std.mem.eql(u8, sidecar.model, name)) count += 1;
|
|
1371
|
+
for (sidecar.types.structs) |entry| {
|
|
1372
|
+
for (entry.fields) |field| count += refNames(field.type, name);
|
|
1373
|
+
}
|
|
1374
|
+
for (sidecar.types.unions) |entry| {
|
|
1375
|
+
for (entry.arms) |arm| count += refNames(arm.payload, name);
|
|
1376
|
+
}
|
|
1377
|
+
for (sidecar.msg.arms) |arm| {
|
|
1378
|
+
switch (arm.payload) {
|
|
1379
|
+
.record, .union_ref, .enum_ref => |payload_name| {
|
|
1380
|
+
if (std.mem.eql(u8, payload_name, name)) count += 1;
|
|
1381
|
+
},
|
|
1382
|
+
.scalar => |ref| count += refNames(ref, name),
|
|
1383
|
+
else => {},
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
for (sidecar.model_helpers) |helper| {
|
|
1387
|
+
count += refNames(helper.returns, name);
|
|
1388
|
+
for (helper.params) |param| count += refNames(param, name);
|
|
1389
|
+
}
|
|
1390
|
+
return count;
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
/// Whether `ref` contains a by-reference (`node`) reference to `name`.
|
|
1394
|
+
fn refNodeNames(ref: TypeRef, name: []const u8) bool {
|
|
1395
|
+
return switch (ref) {
|
|
1396
|
+
.optional => |inner| refNodeNames(inner.*, name),
|
|
1397
|
+
.slice => |elem| refNodeNames(elem.*, name),
|
|
1398
|
+
.node => |ref_name| std.mem.eql(u8, ref_name, name),
|
|
1399
|
+
else => false,
|
|
1400
|
+
};
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
fn refNames(ref: TypeRef, name: []const u8) usize {
|
|
1404
|
+
return switch (ref) {
|
|
1405
|
+
.optional => |inner| refNames(inner.*, name),
|
|
1406
|
+
.slice => |elem| refNames(elem.*, name),
|
|
1407
|
+
.node, .value, .enum_ref, .union_ref => |ref_name| @intFromBool(std.mem.eql(u8, ref_name, name)),
|
|
1408
|
+
else => 0,
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
fn nameListed(names: []const []const u8, name: []const u8) bool {
|
|
1413
|
+
for (names) |candidate| {
|
|
1414
|
+
if (std.mem.eql(u8, candidate, name)) return true;
|
|
1415
|
+
}
|
|
1416
|
+
return false;
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
/// Sidecar strings quoted inside the generated module's comments:
|
|
1420
|
+
/// control characters would break the comment line, so they become
|
|
1421
|
+
/// spaces (comments are provenance, never load-bearing).
|
|
1422
|
+
fn commentText(arena: std.mem.Allocator, text: []const u8) error{OutOfMemory}![]const u8 {
|
|
1423
|
+
const out = try arena.dupe(u8, text);
|
|
1424
|
+
for (out) |*char| {
|
|
1425
|
+
if (char.* < 0x20 or char.* == 0x7f) char.* = ' ';
|
|
1426
|
+
}
|
|
1427
|
+
// U+2028/U+2029 are line terminators to a TypeScript scanner: blank
|
|
1428
|
+
// their UTF-8 bytes so provenance text cannot end the comment early.
|
|
1429
|
+
var index: usize = 0;
|
|
1430
|
+
while (index + 2 < out.len) : (index += 1) {
|
|
1431
|
+
if (out[index] == 0xe2 and out[index + 1] == 0x80 and (out[index + 2] == 0xa8 or out[index + 2] == 0xa9)) {
|
|
1432
|
+
out[index] = ' ';
|
|
1433
|
+
out[index + 1] = ' ';
|
|
1434
|
+
out[index + 2] = ' ';
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
return out;
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
// -------------------------------------------------- identifier safety
|
|
1441
|
+
|
|
1442
|
+
/// Emit a sidecar name as a Zig identifier, `@"..."`-quoting anything
|
|
1443
|
+
/// that is not a plain identifier (keywords, leading digits, exotic
|
|
1444
|
+
/// characters) — the author's spelling survives either way, and every
|
|
1445
|
+
/// reflecting consumer reads the unquoted name.
|
|
1446
|
+
const Ident = struct {
|
|
1447
|
+
name: []const u8,
|
|
1448
|
+
|
|
1449
|
+
pub fn format(self: Ident, writer: *std.Io.Writer) std.Io.Writer.Error!void {
|
|
1450
|
+
if (isPlainIdentifier(self.name)) {
|
|
1451
|
+
try writer.writeAll(self.name);
|
|
1452
|
+
} else {
|
|
1453
|
+
try writer.print("@\"{f}\"", .{std.zig.fmtString(self.name)});
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
};
|
|
1457
|
+
|
|
1458
|
+
fn ident(name: []const u8) Ident {
|
|
1459
|
+
return .{ .name = name };
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
fn isPlainIdentifier(name: []const u8) bool {
|
|
1463
|
+
if (name.len == 0) return false;
|
|
1464
|
+
// `_` is the discard token, not a declarable identifier.
|
|
1465
|
+
if (std.mem.eql(u8, name, "_")) return false;
|
|
1466
|
+
if (name[0] >= '0' and name[0] <= '9') return false;
|
|
1467
|
+
for (name) |char| {
|
|
1468
|
+
const ok = (char >= 'a' and char <= 'z') or (char >= 'A' and char <= 'Z') or
|
|
1469
|
+
(char >= '0' and char <= '9') or char == '_';
|
|
1470
|
+
if (!ok) return false;
|
|
1471
|
+
}
|
|
1472
|
+
if (std.zig.Token.keywords.has(name)) return false;
|
|
1473
|
+
// Primitive type names shadow poorly; quote them too.
|
|
1474
|
+
if (std.zig.isPrimitive(name)) return false;
|
|
1475
|
+
return true;
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
// --------------------------------------------------------------- tests
|
|
1479
|
+
|
|
1480
|
+
const testing = std.testing;
|
|
1481
|
+
|
|
1482
|
+
fn emitFromJson(arena: std.mem.Allocator, json: []const u8) ![]const u8 {
|
|
1483
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
1484
|
+
const parsed = sidecar_mod.read(arena, json, &diags) catch |err| {
|
|
1485
|
+
var buffer: [4096]u8 = undefined;
|
|
1486
|
+
var writer = std.Io.Writer.fixed(&buffer);
|
|
1487
|
+
diags.write("sidecar", &writer) catch {};
|
|
1488
|
+
std.debug.print("{s}", .{writer.buffered()});
|
|
1489
|
+
return err;
|
|
1490
|
+
};
|
|
1491
|
+
return emit(arena, parsed, &diags) catch |err| {
|
|
1492
|
+
var buffer: [4096]u8 = undefined;
|
|
1493
|
+
var writer = std.Io.Writer.fixed(&buffer);
|
|
1494
|
+
diags.write("sidecar", &writer) catch {};
|
|
1495
|
+
std.debug.print("{s}", .{writer.buffered()});
|
|
1496
|
+
return err;
|
|
1497
|
+
};
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
test "emission is deterministic and carries the mirror surface" {
|
|
1501
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1502
|
+
defer arena_state.deinit();
|
|
1503
|
+
const arena = arena_state.allocator();
|
|
1504
|
+
const first = try emitFromJson(arena, sidecar_mod.minimal_valid_json);
|
|
1505
|
+
const second = try emitFromJson(arena, sidecar_mod.minimal_valid_json);
|
|
1506
|
+
try testing.expectEqualStrings(first, second);
|
|
1507
|
+
try testing.expect(std.mem.indexOf(u8, first, "pub const Model = struct {") != null);
|
|
1508
|
+
try testing.expect(std.mem.indexOf(u8, first, "count: i64,") != null);
|
|
1509
|
+
try testing.expect(std.mem.indexOf(u8, first, "pub const Msg = union(enum) {") != null);
|
|
1510
|
+
try testing.expect(std.mem.indexOf(u8, first, "label_set: []const u8,") != null);
|
|
1511
|
+
try testing.expect(std.mem.indexOf(u8, first, "abi.dispatch_void(0,") != null);
|
|
1512
|
+
try testing.expect(std.mem.indexOf(u8, first, "abi.dispatch_bytes(1,") != null);
|
|
1513
|
+
// A bare-model init emits the pointer-returning shape.
|
|
1514
|
+
try testing.expect(std.mem.indexOf(u8, first, "pub fn initialModel() *const Model {") != null);
|
|
1515
|
+
try testing.expect(std.mem.indexOf(u8, first, "pub const UpdateResult = struct { model: *const Model, cmd: rt.Cmd };") != null);
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
test "u64-attested slots generate the unsigned twin; i64 and f64 slots are untouched" {
|
|
1519
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1520
|
+
defer arena_state.deinit();
|
|
1521
|
+
const arena = arena_state.allocator();
|
|
1522
|
+
// Every slot-path form the grammar can attest, classes mixed: the
|
|
1523
|
+
// attested class decides the mirror's decode width per slot, and
|
|
1524
|
+
// nothing leaks across slots.
|
|
1525
|
+
var source = try std.mem.replaceOwned(
|
|
1526
|
+
u8,
|
|
1527
|
+
arena,
|
|
1528
|
+
sidecar_mod.minimal_valid_json,
|
|
1529
|
+
"{\"name\": \"count\", \"type\": {\"kind\": \"i64\"}}",
|
|
1530
|
+
"{\"name\": \"count\", \"type\": {\"kind\": \"i64\"}},\n {\"name\": \"delta\", \"type\": {\"kind\": \"i64\"}},\n {\"name\": \"ratio\", \"type\": {\"kind\": \"f64\"}}",
|
|
1531
|
+
);
|
|
1532
|
+
source = try std.mem.replaceOwned(
|
|
1533
|
+
u8,
|
|
1534
|
+
arena,
|
|
1535
|
+
source,
|
|
1536
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
1537
|
+
"{\"name\": \"tick\", \"payload\": {\"kind\": \"number\", \"class\": \"i64\"}},\n {\"name\": \"stepped\", \"payload\": {\"kind\": \"number\", \"class\": \"i64\"}},\n {\"name\": \"fetched\", \"payload\": {\"kind\": \"number_bytes\", \"number_field\": \"status\", \"number_class\": \"i64\", \"bytes_field\": \"body\"}}",
|
|
1538
|
+
);
|
|
1539
|
+
source = try std.mem.replaceOwned(
|
|
1540
|
+
u8,
|
|
1541
|
+
arena,
|
|
1542
|
+
source,
|
|
1543
|
+
"\"model_helpers\": []",
|
|
1544
|
+
"\"model_helpers\": [{\"name\": \"peak\", \"params\": [], \"returns\": {\"kind\": \"i64\"}, \"arena\": false}]",
|
|
1545
|
+
);
|
|
1546
|
+
source = try std.mem.replaceOwned(
|
|
1547
|
+
u8,
|
|
1548
|
+
arena,
|
|
1549
|
+
source,
|
|
1550
|
+
"{\"slot\": \"Model.count\", \"class\": \"i64\"}",
|
|
1551
|
+
\\{"slot": "Model.count", "class": "u64"},
|
|
1552
|
+
\\ {"slot": "Model.delta", "class": "i64"},
|
|
1553
|
+
\\ {"slot": "Msg.tick", "class": "u64"},
|
|
1554
|
+
\\ {"slot": "Msg.stepped", "class": "i64"},
|
|
1555
|
+
\\ {"slot": "Msg.fetched.status", "class": "u64"},
|
|
1556
|
+
\\ {"slot": "helpers.peak.return", "class": "u64"}
|
|
1557
|
+
,
|
|
1558
|
+
);
|
|
1559
|
+
const generated = try emitFromJson(arena, source);
|
|
1560
|
+
// Mirror spellings follow the attestation, slot by slot.
|
|
1561
|
+
try testing.expect(std.mem.indexOf(u8, generated, "count: u64,") != null);
|
|
1562
|
+
try testing.expect(std.mem.indexOf(u8, generated, "delta: i64,") != null);
|
|
1563
|
+
try testing.expect(std.mem.indexOf(u8, generated, "ratio: f64,") != null);
|
|
1564
|
+
try testing.expect(std.mem.indexOf(u8, generated, "tick: u64,") != null);
|
|
1565
|
+
try testing.expect(std.mem.indexOf(u8, generated, "stepped: i64,") != null);
|
|
1566
|
+
try testing.expect(std.mem.indexOf(u8, generated, "fetched: struct { status: u64, body: []const u8 },") != null);
|
|
1567
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub fn peak(self: *const Model) u64 {") != null);
|
|
1568
|
+
// Dispatch narrows through the matching exactness guard.
|
|
1569
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi.dispatch_number(0, shim_rt.exactF64Unsigned(payload), &cmd_ptr, &cmd_len)") != null);
|
|
1570
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi.dispatch_number(1, shim_rt.exactF64(payload), &cmd_ptr, &cmd_len)") != null);
|
|
1571
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi.dispatch_number_bytes(2, shim_rt.exactF64Unsigned(payload.status), payload.body.ptr, payload.body.len, &cmd_ptr, &cmd_len)") != null);
|
|
1572
|
+
// The generated module stays valid Zig.
|
|
1573
|
+
const source_z = try arena.dupeZ(u8, generated);
|
|
1574
|
+
const tree = try std.zig.Ast.parse(arena, source_z, .zig);
|
|
1575
|
+
try testing.expectEqual(@as(usize, 0), tree.errors.len);
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
test "u64 attestations on host-supplied signed slots refuse at check time" {
|
|
1579
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1580
|
+
defer arena_state.deinit();
|
|
1581
|
+
const arena = arena_state.allocator();
|
|
1582
|
+
// A scroll-state record with a u64-attested axis: scroll translation
|
|
1583
|
+
// supplies negative offsets and velocities, which the unsigned class
|
|
1584
|
+
// cannot carry — refused before any wiring generates.
|
|
1585
|
+
const scroll_struct =
|
|
1586
|
+
\\ {"name": "Scroll", "fields": [
|
|
1587
|
+
\\ {"name": "offsetX", "type": {"kind": "f64"}},
|
|
1588
|
+
\\ {"name": "offsetY", "type": {"kind": "i64"}},
|
|
1589
|
+
\\ {"name": "velocityX", "type": {"kind": "f64"}},
|
|
1590
|
+
\\ {"name": "velocityY", "type": {"kind": "f64"}},
|
|
1591
|
+
\\ {"name": "viewportExtentX", "type": {"kind": "f64"}},
|
|
1592
|
+
\\ {"name": "viewportExtentY", "type": {"kind": "f64"}},
|
|
1593
|
+
\\ {"name": "contentExtentX", "type": {"kind": "f64"}},
|
|
1594
|
+
\\ {"name": "contentExtentY", "type": {"kind": "f64"}}
|
|
1595
|
+
\\ ]},
|
|
1596
|
+
;
|
|
1597
|
+
var source = try std.mem.replaceOwned(
|
|
1598
|
+
u8,
|
|
1599
|
+
arena,
|
|
1600
|
+
sidecar_mod.minimal_valid_json,
|
|
1601
|
+
"\"structs\": [\n",
|
|
1602
|
+
try std.fmt.allocPrint(arena, "\"structs\": [\n{s}\n", .{scroll_struct}),
|
|
1603
|
+
);
|
|
1604
|
+
source = try std.mem.replaceOwned(
|
|
1605
|
+
u8,
|
|
1606
|
+
arena,
|
|
1607
|
+
source,
|
|
1608
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
1609
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}},\n {\"name\": \"scrolled\", \"payload\": {\"kind\": \"record\", \"name\": \"Scroll\"}}",
|
|
1610
|
+
);
|
|
1611
|
+
source = try std.mem.replaceOwned(u8, arena, source, "{\"slot\": \"Model.count\", \"class\": \"i64\"}", "{\"slot\": \"Model.count\", \"class\": \"i64\"}, {\"slot\": \"Scroll.offsetY\", \"class\": \"u64\"}");
|
|
1612
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
1613
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
1614
|
+
try testing.expectError(error.Refused, emit(arena, parsed, &diags));
|
|
1615
|
+
var found = false;
|
|
1616
|
+
for (diags.list.items) |item| {
|
|
1617
|
+
if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, "scroll-state axis") != null) found = true;
|
|
1618
|
+
}
|
|
1619
|
+
try testing.expect(found);
|
|
1620
|
+
// The same record i64-attested generates: only the unsigned class
|
|
1621
|
+
// is incoherent with the host's signed values.
|
|
1622
|
+
const signed = try std.mem.replaceOwned(u8, arena, source, "{\"slot\": \"Scroll.offsetY\", \"class\": \"u64\"}", "{\"slot\": \"Scroll.offsetY\", \"class\": \"i64\"}");
|
|
1623
|
+
const generated = try emitFromJson(arena, signed);
|
|
1624
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi.dispatch_scroll_state(1,") != null);
|
|
1625
|
+
|
|
1626
|
+
// Extents stay attestable: the host only ever supplies viewport and
|
|
1627
|
+
// content sizes non-negative, so the unsigned class carries them.
|
|
1628
|
+
var extent_source = try std.mem.replaceOwned(u8, arena, source, "{\"name\": \"offsetY\", \"type\": {\"kind\": \"i64\"}}", "{\"name\": \"offsetY\", \"type\": {\"kind\": \"f64\"}}");
|
|
1629
|
+
extent_source = try std.mem.replaceOwned(u8, arena, extent_source, "{\"name\": \"viewportExtentY\", \"type\": {\"kind\": \"f64\"}}", "{\"name\": \"viewportExtentY\", \"type\": {\"kind\": \"i64\"}}");
|
|
1630
|
+
extent_source = try std.mem.replaceOwned(u8, arena, extent_source, "{\"slot\": \"Scroll.offsetY\", \"class\": \"u64\"}", "{\"slot\": \"Scroll.viewportExtentY\", \"class\": \"u64\"}");
|
|
1631
|
+
const extent_generated = try emitFromJson(arena, extent_source);
|
|
1632
|
+
try testing.expect(std.mem.indexOf(u8, extent_generated, "shim_rt.exactF64Unsigned(payload.viewportExtentY)") != null);
|
|
1633
|
+
|
|
1634
|
+
// A scroll-shaped record no message arm routes is not scroll state
|
|
1635
|
+
// to the host: a model-only metrics record keeps its unsigned
|
|
1636
|
+
// attestation even on an axis-named field.
|
|
1637
|
+
var model_only = try std.mem.replaceOwned(
|
|
1638
|
+
u8,
|
|
1639
|
+
arena,
|
|
1640
|
+
source,
|
|
1641
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}},\n {\"name\": \"scrolled\", \"payload\": {\"kind\": \"record\", \"name\": \"Scroll\"}}",
|
|
1642
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
1643
|
+
);
|
|
1644
|
+
model_only = try std.mem.replaceOwned(
|
|
1645
|
+
u8,
|
|
1646
|
+
arena,
|
|
1647
|
+
model_only,
|
|
1648
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
1649
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}, {\"name\": \"metrics\", \"type\": {\"kind\": \"value\", \"name\": \"Scroll\"}}",
|
|
1650
|
+
);
|
|
1651
|
+
const model_only_generated = try emitFromJson(arena, model_only);
|
|
1652
|
+
try testing.expect(std.mem.indexOf(u8, model_only_generated, "offsetY: u64,") != null);
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
test "a u64 attestation on chrome geometry refuses at check time" {
|
|
1656
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1657
|
+
defer arena_state.deinit();
|
|
1658
|
+
const arena = arena_state.allocator();
|
|
1659
|
+
// A wired chrome arm whose buttons record attests x as u64:
|
|
1660
|
+
// embedders report signed content coordinates, which the unsigned
|
|
1661
|
+
// class cannot carry.
|
|
1662
|
+
const source =
|
|
1663
|
+
\\{
|
|
1664
|
+
\\ "format": 1, "wire_version": 3, "abi_version": 1,
|
|
1665
|
+
\\ "compiler_version": "0.0.1", "entry": "src/core.ts",
|
|
1666
|
+
\\ "source_hash": "00000000c0ffee00", "build_id": "00000000b01dface",
|
|
1667
|
+
\\ "types": {
|
|
1668
|
+
\\ "structs": [
|
|
1669
|
+
\\ {"name": "Model", "fields": [{"name": "chromeTop", "type": {"kind": "f64"}}]},
|
|
1670
|
+
\\ {"name": "Insets", "fields": [
|
|
1671
|
+
\\ {"name": "top", "type": {"kind": "f64"}}, {"name": "right", "type": {"kind": "f64"}},
|
|
1672
|
+
\\ {"name": "bottom", "type": {"kind": "f64"}}, {"name": "left", "type": {"kind": "f64"}}
|
|
1673
|
+
\\ ]},
|
|
1674
|
+
\\ {"name": "Buttons", "fields": [
|
|
1675
|
+
\\ {"name": "x", "type": {"kind": "i64"}}, {"name": "y", "type": {"kind": "f64"}},
|
|
1676
|
+
\\ {"name": "width", "type": {"kind": "f64"}}, {"name": "height", "type": {"kind": "f64"}}
|
|
1677
|
+
\\ ]},
|
|
1678
|
+
\\ {"name": "Msg_chrome_changed", "fields": [
|
|
1679
|
+
\\ {"name": "insets", "type": {"kind": "value", "name": "Insets"}},
|
|
1680
|
+
\\ {"name": "buttons", "type": {"kind": "value", "name": "Buttons"}},
|
|
1681
|
+
\\ {"name": "tabsProjected", "type": {"kind": "bool"}}
|
|
1682
|
+
\\ ]}
|
|
1683
|
+
\\ ],
|
|
1684
|
+
\\ "enums": [], "unions": []
|
|
1685
|
+
\\ },
|
|
1686
|
+
\\ "model": "Model", "model_helpers": [], "model_unbound": [],
|
|
1687
|
+
\\ "msg": {"name": "Msg", "arms": [
|
|
1688
|
+
\\ {"name": "chrome_changed", "payload": {"kind": "record", "name": "Msg_chrome_changed"}}
|
|
1689
|
+
\\ ], "unbound": []},
|
|
1690
|
+
\\ "init_returns_cmd": false, "update_returns_cmd": true, "has_subscriptions": false,
|
|
1691
|
+
\\ "channels": {"command_msg": false, "frame_msg": false, "key_msg": false, "pinch_msg": false,
|
|
1692
|
+
\\ "appearance_msg": null, "chrome_msg": "chrome_changed", "env_msgs": []},
|
|
1693
|
+
\\ "abi": {"prefix": "nsc_core_", "exports": ["abi_version", "build_id", "set_panic_sink", "init",
|
|
1694
|
+
\\ "collect", "frame_reset", "boot_cmd", "dispatch_void", "dispatch_bytes", "dispatch_number",
|
|
1695
|
+
\\ "dispatch_number_bytes", "dispatch_bool", "dispatch_enum", "dispatch_record",
|
|
1696
|
+
\\ "dispatch_text_input", "dispatch_scroll_state", "subscriptions", "model_snapshot",
|
|
1697
|
+
\\ "helper_call"], "snapshot_format": 1},
|
|
1698
|
+
\\ "integer_slots": [{"slot": "Buttons.x", "class": "u64"}],
|
|
1699
|
+
\\ "deterministic": true, "async_free": true
|
|
1700
|
+
\\}
|
|
1701
|
+
;
|
|
1702
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
1703
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
1704
|
+
try testing.expectError(error.Refused, emit(arena, parsed, &diags));
|
|
1705
|
+
var found = false;
|
|
1706
|
+
for (diags.list.items) |item| {
|
|
1707
|
+
if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, "window-control cluster's position") != null) found = true;
|
|
1708
|
+
}
|
|
1709
|
+
try testing.expect(found);
|
|
1710
|
+
|
|
1711
|
+
// Insets and cluster sizes are non-negative overlay extents: the
|
|
1712
|
+
// unsigned class carries them.
|
|
1713
|
+
var arena_state2 = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1714
|
+
defer arena_state2.deinit();
|
|
1715
|
+
const arena2 = arena_state2.allocator();
|
|
1716
|
+
var extents = try std.mem.replaceOwned(u8, arena2, source, "{\"name\": \"x\", \"type\": {\"kind\": \"i64\"}}", "{\"name\": \"x\", \"type\": {\"kind\": \"f64\"}}");
|
|
1717
|
+
extents = try std.mem.replaceOwned(u8, arena2, extents, "{\"name\": \"top\", \"type\": {\"kind\": \"f64\"}}", "{\"name\": \"top\", \"type\": {\"kind\": \"i64\"}}");
|
|
1718
|
+
extents = try std.mem.replaceOwned(u8, arena2, extents, "{\"slot\": \"Buttons.x\", \"class\": \"u64\"}", "{\"slot\": \"Insets.top\", \"class\": \"u64\"}");
|
|
1719
|
+
const generated = try emitFromJson(arena2, extents);
|
|
1720
|
+
try testing.expect(std.mem.indexOf(u8, generated, "top: u64,") != null);
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
test "u64 attestations on selection bounds are accepted (the host supplies unsigned offsets)" {
|
|
1724
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1725
|
+
defer arena_state.deinit();
|
|
1726
|
+
const arena = arena_state.allocator();
|
|
1727
|
+
// The full text-input vocabulary with a u64-attested selection
|
|
1728
|
+
// anchor: the engine's selection bounds are unsigned offsets (a
|
|
1729
|
+
// backward selection is anchor > focus, never a negative index), so
|
|
1730
|
+
// the unsigned class carries every host-supplied value.
|
|
1731
|
+
const records =
|
|
1732
|
+
\\ {"name": "Move", "fields": [
|
|
1733
|
+
\\ {"name": "direction", "type": {"kind": "enum", "name": "Dir"}},
|
|
1734
|
+
\\ {"name": "extend", "type": {"kind": "bool"}}
|
|
1735
|
+
\\ ]},
|
|
1736
|
+
\\ {"name": "Sel", "fields": [
|
|
1737
|
+
\\ {"name": "anchor", "type": {"kind": "i64"}},
|
|
1738
|
+
\\ {"name": "focus", "type": {"kind": "i64"}}
|
|
1739
|
+
\\ ]},
|
|
1740
|
+
\\ {"name": "Comp", "fields": [
|
|
1741
|
+
\\ {"name": "text", "type": {"kind": "bytes"}},
|
|
1742
|
+
\\ {"name": "cursor", "type": {"kind": "optional", "inner": {"kind": "i64"}}}
|
|
1743
|
+
\\ ]},
|
|
1744
|
+
;
|
|
1745
|
+
const union_entry =
|
|
1746
|
+
\\"unions": [{"name": "Edit", "arms": [
|
|
1747
|
+
\\ {"name": "insert_text", "payload": {"kind": "bytes"}},
|
|
1748
|
+
\\ {"name": "delete_backward", "payload": {"kind": "void"}},
|
|
1749
|
+
\\ {"name": "delete_forward", "payload": {"kind": "void"}},
|
|
1750
|
+
\\ {"name": "delete_word_backward", "payload": {"kind": "void"}},
|
|
1751
|
+
\\ {"name": "delete_word_forward", "payload": {"kind": "void"}},
|
|
1752
|
+
\\ {"name": "clear", "payload": {"kind": "void"}},
|
|
1753
|
+
\\ {"name": "move_caret", "payload": {"kind": "value", "name": "Move"}},
|
|
1754
|
+
\\ {"name": "set_selection", "payload": {"kind": "value", "name": "Sel"}},
|
|
1755
|
+
\\ {"name": "set_composition", "payload": {"kind": "value", "name": "Comp"}},
|
|
1756
|
+
\\ {"name": "commit_composition", "payload": {"kind": "void"}},
|
|
1757
|
+
\\ {"name": "cancel_composition", "payload": {"kind": "void"}}
|
|
1758
|
+
\\ ]}]
|
|
1759
|
+
;
|
|
1760
|
+
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}));
|
|
1761
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"enums\": []", "\"enums\": [{\"name\": \"Dir\", \"members\": [\"previous\", \"next\", \"previous_word\", \"next_word\", \"start\", \"end\"]}]");
|
|
1762
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"unions\": []", union_entry);
|
|
1763
|
+
source = try std.mem.replaceOwned(
|
|
1764
|
+
u8,
|
|
1765
|
+
arena,
|
|
1766
|
+
source,
|
|
1767
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
1768
|
+
"{\"name\": \"edited\", \"payload\": {\"kind\": \"union\", \"name\": \"Edit\"}}",
|
|
1769
|
+
);
|
|
1770
|
+
source = try std.mem.replaceOwned(
|
|
1771
|
+
u8,
|
|
1772
|
+
arena,
|
|
1773
|
+
source,
|
|
1774
|
+
"{\"slot\": \"Model.count\", \"class\": \"i64\"}",
|
|
1775
|
+
"{\"slot\": \"Model.count\", \"class\": \"i64\"}, {\"slot\": \"Sel.anchor\", \"class\": \"u64\"}, {\"slot\": \"Sel.focus\", \"class\": \"u64\"}, {\"slot\": \"Comp.cursor\", \"class\": \"u64\"}",
|
|
1776
|
+
);
|
|
1777
|
+
const generated = try emitFromJson(arena, source);
|
|
1778
|
+
// The mirror spells the attested classes and the union still routes
|
|
1779
|
+
// through the text-input entry (recognition is spelling-level).
|
|
1780
|
+
try testing.expect(std.mem.indexOf(u8, generated, "anchor: u64,") != null);
|
|
1781
|
+
try testing.expect(std.mem.indexOf(u8, generated, "focus: u64,") != null);
|
|
1782
|
+
try testing.expect(std.mem.indexOf(u8, generated, "cursor: ?u64,") != null);
|
|
1783
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi.dispatch_text_input(0,") != null);
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
test "the empty integer_slots list decodes nothing differently" {
|
|
1787
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1788
|
+
defer arena_state.deinit();
|
|
1789
|
+
const arena = arena_state.allocator();
|
|
1790
|
+
// The f64-only sequencing: every numeric slot spelled f64, the
|
|
1791
|
+
// empty list attested — the mirror carries f64 slots and no
|
|
1792
|
+
// integer narrowing anywhere.
|
|
1793
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "{\"name\": \"count\", \"type\": {\"kind\": \"i64\"}}", "{\"name\": \"count\", \"type\": {\"kind\": \"f64\"}}");
|
|
1794
|
+
source = try std.mem.replaceOwned(u8, arena, source, "{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}", "{\"name\": \"picked\", \"payload\": {\"kind\": \"number\", \"class\": \"f64\"}}");
|
|
1795
|
+
source = try std.mem.replaceOwned(u8, arena, source, "{\"slot\": \"Model.count\", \"class\": \"i64\"}", "");
|
|
1796
|
+
const generated = try emitFromJson(arena, source);
|
|
1797
|
+
try testing.expect(std.mem.indexOf(u8, generated, "count: f64,") != null);
|
|
1798
|
+
try testing.expect(std.mem.indexOf(u8, generated, "picked: f64,") != null);
|
|
1799
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi.dispatch_number(0, payload, &cmd_ptr, &cmd_len)") != null);
|
|
1800
|
+
// No integer narrowing and no integer-spelled mirror slot anywhere
|
|
1801
|
+
// (the identity constants' own u64 plumbing is not a mirror slot).
|
|
1802
|
+
try testing.expect(std.mem.indexOf(u8, generated, "exactF64") == null);
|
|
1803
|
+
try testing.expect(std.mem.indexOf(u8, generated, ": i64,") == null);
|
|
1804
|
+
try testing.expect(std.mem.indexOf(u8, generated, ": u64,") == null);
|
|
1805
|
+
}
|
|
1806
|
+
|
|
1807
|
+
test "the emitted shim parses as Zig" {
|
|
1808
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1809
|
+
defer arena_state.deinit();
|
|
1810
|
+
const arena = arena_state.allocator();
|
|
1811
|
+
const source = try emitFromJson(arena, sidecar_mod.minimal_valid_json);
|
|
1812
|
+
const source_z = try arena.dupeZ(u8, source);
|
|
1813
|
+
const tree = try std.zig.Ast.parse(arena, source_z, .zig);
|
|
1814
|
+
try testing.expectEqual(@as(usize, 0), tree.errors.len);
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
test "a type name colliding with a shim declaration refuses with a teaching" {
|
|
1818
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1819
|
+
defer arena_state.deinit();
|
|
1820
|
+
const arena = arena_state.allocator();
|
|
1821
|
+
// The model root renamed to "rt": layout-legal, but the generated
|
|
1822
|
+
// module must also declare its kernel block under that name.
|
|
1823
|
+
const renamed = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"Model\"", "\"rt\"");
|
|
1824
|
+
const with_slot = try std.mem.replaceOwned(u8, arena, renamed, "\"slot\": \"Model.count\"", "\"slot\": \"rt.count\"");
|
|
1825
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
1826
|
+
const parsed = try sidecar_mod.read(arena, with_slot, &diags);
|
|
1827
|
+
try testing.expectError(error.Refused, emit(arena, parsed, &diags));
|
|
1828
|
+
var found = false;
|
|
1829
|
+
for (diags.list.items) |item| {
|
|
1830
|
+
if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, "collides with a declaration") != null) found = true;
|
|
1831
|
+
}
|
|
1832
|
+
try testing.expect(found);
|
|
1833
|
+
}
|
|
1834
|
+
|
|
1835
|
+
test "a shared authored type spelling like a synthesized name stays a top-level declaration" {
|
|
1836
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1837
|
+
defer arena_state.deinit();
|
|
1838
|
+
const arena = arena_state.allocator();
|
|
1839
|
+
// "Model_user" is referenced from BOTH Model.user (where the
|
|
1840
|
+
// synthesized pattern matches) and Model.backup: inlining it at the
|
|
1841
|
+
// first site would leave the second dangling.
|
|
1842
|
+
const source =
|
|
1843
|
+
\\{
|
|
1844
|
+
\\ "format": 1, "wire_version": 3, "abi_version": 1,
|
|
1845
|
+
\\ "compiler_version": "0.0.1", "entry": "src/core.ts",
|
|
1846
|
+
\\ "source_hash": "00000000c0ffee00", "build_id": "00000000b01dface",
|
|
1847
|
+
\\ "types": {
|
|
1848
|
+
\\ "structs": [
|
|
1849
|
+
\\ {"name": "Model_user", "fields": [{"name": "id", "type": {"kind": "f64"}}]},
|
|
1850
|
+
\\ {"name": "Model", "fields": [
|
|
1851
|
+
\\ {"name": "user", "type": {"kind": "value", "name": "Model_user"}},
|
|
1852
|
+
\\ {"name": "backup", "type": {"kind": "value", "name": "Model_user"}}
|
|
1853
|
+
\\ ]}
|
|
1854
|
+
\\ ],
|
|
1855
|
+
\\ "enums": [], "unions": []
|
|
1856
|
+
\\ },
|
|
1857
|
+
\\ "model": "Model", "model_helpers": [], "model_unbound": [],
|
|
1858
|
+
\\ "msg": {"name": "Msg", "arms": [{"name": "bump", "payload": {"kind": "void"}}], "unbound": []},
|
|
1859
|
+
\\ "init_returns_cmd": false, "update_returns_cmd": true, "has_subscriptions": false,
|
|
1860
|
+
\\ "channels": {"command_msg": false, "frame_msg": false, "key_msg": false, "pinch_msg": false,
|
|
1861
|
+
\\ "appearance_msg": null, "chrome_msg": null, "env_msgs": []},
|
|
1862
|
+
\\ "abi": {"prefix": "nsc_core_", "exports": ["abi_version", "build_id", "set_panic_sink", "init",
|
|
1863
|
+
\\ "collect", "frame_reset", "boot_cmd", "dispatch_void", "dispatch_bytes", "dispatch_number",
|
|
1864
|
+
\\ "dispatch_number_bytes", "dispatch_bool", "dispatch_enum", "dispatch_record",
|
|
1865
|
+
\\ "dispatch_text_input", "dispatch_scroll_state", "subscriptions", "model_snapshot",
|
|
1866
|
+
\\ "helper_call"], "snapshot_format": 1},
|
|
1867
|
+
\\ "integer_slots": [], "deterministic": true, "async_free": true
|
|
1868
|
+
\\}
|
|
1869
|
+
;
|
|
1870
|
+
const generated = try emitFromJson(arena, source);
|
|
1871
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub const Model_user = struct {") != null);
|
|
1872
|
+
try testing.expect(std.mem.indexOf(u8, generated, "user: Model_user,") != null);
|
|
1873
|
+
try testing.expect(std.mem.indexOf(u8, generated, "backup: Model_user,") != null);
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
test "exotic strings in names and env entries emit as valid Zig" {
|
|
1877
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1878
|
+
defer arena_state.deinit();
|
|
1879
|
+
const arena = arena_state.allocator();
|
|
1880
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
1881
|
+
const source = try std.mem.replaceOwned(
|
|
1882
|
+
u8,
|
|
1883
|
+
arena,
|
|
1884
|
+
sidecar_mod.minimal_valid_json,
|
|
1885
|
+
"\"env_msgs\": []",
|
|
1886
|
+
"\"env_msgs\": [{\"env\": \"APP\\\"MODE\\\\X\", \"msg\": \"label_set\"}]",
|
|
1887
|
+
);
|
|
1888
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
1889
|
+
const generated = try emit(arena, parsed, &diags);
|
|
1890
|
+
const source_z = try arena.dupeZ(u8, generated);
|
|
1891
|
+
const tree = try std.zig.Ast.parse(arena, source_z, .zig);
|
|
1892
|
+
try testing.expectEqual(@as(usize, 0), tree.errors.len);
|
|
1893
|
+
try testing.expect(std.mem.indexOf(u8, generated, "APP\\\"MODE\\\\X") != null);
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
test "channel glue speaks the sidecar's message union name" {
|
|
1897
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1898
|
+
defer arena_state.deinit();
|
|
1899
|
+
const arena = arena_state.allocator();
|
|
1900
|
+
// Rename the union to "Event" and wire the key channel.
|
|
1901
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"name\": \"Msg\"", "\"name\": \"Event\"");
|
|
1902
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"key_msg\": false", "\"key_msg\": true");
|
|
1903
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"helper_call\"]", "\"helper_call\", \"key_msg\"]");
|
|
1904
|
+
const generated = try emitFromJson(arena, source);
|
|
1905
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub const Event = union(enum) {") != null);
|
|
1906
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub fn keyMsg(key: KeyEvent) ?Event {") != null);
|
|
1907
|
+
try testing.expect(std.mem.indexOf(u8, generated, "fn msgFromEnvelope(envelope: []const u8) ?Event {") != null);
|
|
1908
|
+
try testing.expect(std.mem.indexOf(u8, generated, "@FieldType(Event, \"label_set\")") != null);
|
|
1909
|
+
// The channel entry returns the bytes envelope on the ordinary
|
|
1910
|
+
// out-pointer pair; the wrapper hands the whole buffer to the
|
|
1911
|
+
// unpacker (no status return, no out-record). The out state is
|
|
1912
|
+
// DEFINED before the call: an entry that returns without writing
|
|
1913
|
+
// yields the zero-length envelope, which the envelope reader
|
|
1914
|
+
// refuses as short.
|
|
1915
|
+
try testing.expect(std.mem.indexOf(u8, generated, "var out_ptr: [*]const u8 = &shim_rt.channel_out_guard;\n var out_len: usize = 0;\n abi.key_msg(key.key.ptr, key.key.len, @intFromBool(key.shift), @intFromBool(key.control), @intFromBool(key.alt), @intFromBool(key.super), &out_ptr, &out_len);") != null);
|
|
1916
|
+
try testing.expect(std.mem.indexOf(u8, generated, "return msgFromEnvelope(shim_rt.channelEnvelopeBytes(out_ptr, out_len));") != null);
|
|
1917
|
+
try testing.expect(std.mem.indexOf(u8, generated, "shim_rt.channelEnvelope(envelope)") != null);
|
|
1918
|
+
try testing.expect(std.mem.indexOf(u8, generated, "if (!header.produced) return null;") != null);
|
|
1919
|
+
const source_z = try arena.dupeZ(u8, generated);
|
|
1920
|
+
const tree = try std.zig.Ast.parse(arena, source_z, .zig);
|
|
1921
|
+
try testing.expectEqual(@as(usize, 0), tree.errors.len);
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
test "optional glue names are reserved only when the glue is emitted" {
|
|
1925
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1926
|
+
defer arena_state.deinit();
|
|
1927
|
+
const arena = arena_state.allocator();
|
|
1928
|
+
// No helpers and no function channels: a reachable type named
|
|
1929
|
+
// "callHelper" collides with nothing the shim declares.
|
|
1930
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"enums\": []", "\"enums\": [{\"name\": \"callHelper\", \"members\": [\"on\", \"off\"]}]");
|
|
1931
|
+
source = try std.mem.replaceOwned(
|
|
1932
|
+
u8,
|
|
1933
|
+
arena,
|
|
1934
|
+
source,
|
|
1935
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
1936
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"enum\", \"name\": \"callHelper\"}}",
|
|
1937
|
+
);
|
|
1938
|
+
const generated = try emitFromJson(arena, source);
|
|
1939
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub const callHelper = enum(u8) {") != null);
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1942
|
+
test "UpdateResult reserves only when the cmd-returning update emits it" {
|
|
1943
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1944
|
+
defer arena_state.deinit();
|
|
1945
|
+
const arena = arena_state.allocator();
|
|
1946
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"update_returns_cmd\": true", "\"update_returns_cmd\": false");
|
|
1947
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"enums\": []", "\"enums\": [{\"name\": \"UpdateResult\", \"members\": [\"a\"]}]");
|
|
1948
|
+
source = try std.mem.replaceOwned(
|
|
1949
|
+
u8,
|
|
1950
|
+
arena,
|
|
1951
|
+
source,
|
|
1952
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
1953
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"enum\", \"name\": \"UpdateResult\"}}",
|
|
1954
|
+
);
|
|
1955
|
+
const generated = try emitFromJson(arena, source);
|
|
1956
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub const UpdateResult = enum(u8) {") != null);
|
|
1957
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub fn update(model: *const Model, msg: Msg) *const Model {") != null);
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
test "a node-stored record as a message arm payload refuses; value-stored passes" {
|
|
1961
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1962
|
+
defer arena_state.deinit();
|
|
1963
|
+
const arena = arena_state.allocator();
|
|
1964
|
+
// Item lives in the model graph AND doubles as an arm payload: the
|
|
1965
|
+
// compiled core stores that arm by reference, which the record
|
|
1966
|
+
// family cannot say — mirroring by value would skew the layout.
|
|
1967
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"structs\": [", "\"structs\": [\n {\"name\": \"Item\", \"fields\": [{\"name\": \"id\", \"type\": {\"kind\": \"f64\"}}]},");
|
|
1968
|
+
source = try std.mem.replaceOwned(
|
|
1969
|
+
u8,
|
|
1970
|
+
arena,
|
|
1971
|
+
source,
|
|
1972
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
1973
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"node\", \"name\": \"Item\"}}",
|
|
1974
|
+
);
|
|
1975
|
+
source = try std.mem.replaceOwned(
|
|
1976
|
+
u8,
|
|
1977
|
+
arena,
|
|
1978
|
+
source,
|
|
1979
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
1980
|
+
"{\"name\": \"picked\", \"payload\": {\"kind\": \"record\", \"name\": \"Item\"}}",
|
|
1981
|
+
);
|
|
1982
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
1983
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
1984
|
+
try testing.expectError(error.Refused, emit(arena, parsed, &diags));
|
|
1985
|
+
var found = false;
|
|
1986
|
+
for (diags.list.items) |item| {
|
|
1987
|
+
if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, "stored by reference in the model graph") != null) found = true;
|
|
1988
|
+
}
|
|
1989
|
+
try testing.expect(found);
|
|
1990
|
+
|
|
1991
|
+
// The value-stored counterpart is exactly how value-promoted model
|
|
1992
|
+
// records (a text-input union's caret and selection payloads) reach
|
|
1993
|
+
// arms in the emitted lane — it must generate.
|
|
1994
|
+
var value_source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"structs\": [", "\"structs\": [\n {\"name\": \"Sel\", \"fields\": [{\"name\": \"anchor\", \"type\": {\"kind\": \"f64\"}}]},");
|
|
1995
|
+
value_source = try std.mem.replaceOwned(
|
|
1996
|
+
u8,
|
|
1997
|
+
arena,
|
|
1998
|
+
value_source,
|
|
1999
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
2000
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"value\", \"name\": \"Sel\"}}",
|
|
2001
|
+
);
|
|
2002
|
+
value_source = try std.mem.replaceOwned(
|
|
2003
|
+
u8,
|
|
2004
|
+
arena,
|
|
2005
|
+
value_source,
|
|
2006
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
2007
|
+
"{\"name\": \"picked\", \"payload\": {\"kind\": \"record\", \"name\": \"Sel\"}}",
|
|
2008
|
+
);
|
|
2009
|
+
const value_generated = try emitFromJson(arena, value_source);
|
|
2010
|
+
try testing.expect(std.mem.indexOf(u8, value_generated, "picked: Sel,") != null);
|
|
2011
|
+
}
|
|
2012
|
+
|
|
2013
|
+
test "a type named view_unbound refuses (the nested tuple would shadow it)" {
|
|
2014
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2015
|
+
defer arena_state.deinit();
|
|
2016
|
+
const arena = arena_state.allocator();
|
|
2017
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"enums\": []", "\"enums\": [{\"name\": \"view_unbound\", \"members\": [\"a\", \"b\"]}]");
|
|
2018
|
+
source = try std.mem.replaceOwned(
|
|
2019
|
+
u8,
|
|
2020
|
+
arena,
|
|
2021
|
+
source,
|
|
2022
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
2023
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"enum\", \"name\": \"view_unbound\"}}",
|
|
2024
|
+
);
|
|
2025
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
2026
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
2027
|
+
try testing.expectError(error.Refused, emit(arena, parsed, &diags));
|
|
2028
|
+
}
|
|
2029
|
+
|
|
2030
|
+
test "a type named after the tag-table capture refuses" {
|
|
2031
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2032
|
+
defer arena_state.deinit();
|
|
2033
|
+
const arena = arena_state.allocator();
|
|
2034
|
+
// The comptime consistency block captures |field, tag_name|; a
|
|
2035
|
+
// module-level type under either spelling would be shadowed.
|
|
2036
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"enums\": []", "\"enums\": [{\"name\": \"field\", \"members\": [\"a\"]}]");
|
|
2037
|
+
source = try std.mem.replaceOwned(
|
|
2038
|
+
u8,
|
|
2039
|
+
arena,
|
|
2040
|
+
source,
|
|
2041
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
2042
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"enum\", \"name\": \"field\"}}",
|
|
2043
|
+
);
|
|
2044
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
2045
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
2046
|
+
try testing.expectError(error.Refused, emit(arena, parsed, &diags));
|
|
2047
|
+
}
|
|
2048
|
+
|
|
2049
|
+
test "a helper taking a generated glue name refuses" {
|
|
2050
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2051
|
+
defer arena_state.deinit();
|
|
2052
|
+
const arena = arena_state.allocator();
|
|
2053
|
+
// Methods shadow file-scope declarations inside the model struct: a
|
|
2054
|
+
// helper named callHelper would capture the forwarders' own calls.
|
|
2055
|
+
const source = try std.mem.replaceOwned(
|
|
2056
|
+
u8,
|
|
2057
|
+
arena,
|
|
2058
|
+
sidecar_mod.minimal_valid_json,
|
|
2059
|
+
"\"model_helpers\": []",
|
|
2060
|
+
"\"model_helpers\": [{\"name\": \"callHelper\", \"params\": [], \"returns\": {\"kind\": \"bool\"}, \"arena\": false}]",
|
|
2061
|
+
);
|
|
2062
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
2063
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
2064
|
+
try testing.expectError(error.Refused, emit(arena, parsed, &diags));
|
|
2065
|
+
var found = false;
|
|
2066
|
+
for (diags.list.items) |item| {
|
|
2067
|
+
if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, "shadow file-scope names") != null) found = true;
|
|
2068
|
+
}
|
|
2069
|
+
try testing.expect(found);
|
|
2070
|
+
}
|
|
2071
|
+
|
|
2072
|
+
test "a helper named view_unbound refuses with a teaching" {
|
|
2073
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2074
|
+
defer arena_state.deinit();
|
|
2075
|
+
const arena = arena_state.allocator();
|
|
2076
|
+
const source = try std.mem.replaceOwned(
|
|
2077
|
+
u8,
|
|
2078
|
+
arena,
|
|
2079
|
+
sidecar_mod.minimal_valid_json,
|
|
2080
|
+
"\"model_helpers\": []",
|
|
2081
|
+
"\"model_helpers\": [{\"name\": \"view_unbound\", \"params\": [], \"returns\": {\"kind\": \"bool\"}, \"arena\": false}]",
|
|
2082
|
+
);
|
|
2083
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
2084
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
2085
|
+
try testing.expectError(error.Refused, emit(arena, parsed, &diags));
|
|
2086
|
+
var found = false;
|
|
2087
|
+
for (diags.list.items) |item| {
|
|
2088
|
+
if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, "opt-out tuple") != null) found = true;
|
|
2089
|
+
}
|
|
2090
|
+
try testing.expect(found);
|
|
2091
|
+
}
|
|
2092
|
+
|
|
2093
|
+
test "a text-input-named union without the payload shapes rides the record entry" {
|
|
2094
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2095
|
+
defer arena_state.deinit();
|
|
2096
|
+
const arena = arena_state.allocator();
|
|
2097
|
+
// Eleven right names, wrong insert_text payload (void): the markup
|
|
2098
|
+
// predicate would not bind this as text input, so dispatch must
|
|
2099
|
+
// not route it to the text_input entry either.
|
|
2100
|
+
var arms: std.ArrayListUnmanaged(u8) = .empty;
|
|
2101
|
+
const tags = [_][]const u8{
|
|
2102
|
+
"insert_text", "delete_backward", "delete_forward", "delete_word_backward",
|
|
2103
|
+
"delete_word_forward", "clear", "move_caret", "set_selection",
|
|
2104
|
+
"set_composition", "commit_composition", "cancel_composition",
|
|
2105
|
+
};
|
|
2106
|
+
for (tags, 0..) |tag, index| {
|
|
2107
|
+
if (index > 0) try arms.appendSlice(arena, ", ");
|
|
2108
|
+
const one = try std.fmt.allocPrint(arena, "{{\"name\": \"{s}\", \"payload\": {{\"kind\": \"void\"}}}}", .{tag});
|
|
2109
|
+
try arms.appendSlice(arena, one);
|
|
2110
|
+
}
|
|
2111
|
+
const union_entry = try std.fmt.allocPrint(arena, "\"unions\": [{{\"name\": \"NotTextInput\", \"arms\": [{s}]}}]", .{arms.items});
|
|
2112
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"unions\": []", union_entry);
|
|
2113
|
+
source = try std.mem.replaceOwned(
|
|
2114
|
+
u8,
|
|
2115
|
+
arena,
|
|
2116
|
+
source,
|
|
2117
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
2118
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"union\", \"name\": \"NotTextInput\"}}",
|
|
2119
|
+
);
|
|
2120
|
+
const generated = try emitFromJson(arena, source);
|
|
2121
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi.dispatch_record(0,") != null);
|
|
2122
|
+
// No arm may route through the text-input entry (the attestation
|
|
2123
|
+
// block still references the symbol; only dispatch matters here).
|
|
2124
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi.dispatch_text_input(0,") == null);
|
|
2125
|
+
}
|
|
2126
|
+
|
|
2127
|
+
test "the two-axis scroll-state record dispatches as direct scalars" {
|
|
2128
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2129
|
+
defer arena_state.deinit();
|
|
2130
|
+
const arena = arena_state.allocator();
|
|
2131
|
+
// The eight per-axis fields in the TS spelling (the emitted-core
|
|
2132
|
+
// mirror keeps the author's names), declaration order: the shape
|
|
2133
|
+
// the markup predicate binds as `on-scroll`, so dispatch must ride
|
|
2134
|
+
// the dedicated scalar entry, never the encoded record entry.
|
|
2135
|
+
const scroll_struct =
|
|
2136
|
+
\\ {"name": "Scroll", "fields": [
|
|
2137
|
+
\\ {"name": "offsetX", "type": {"kind": "f64"}},
|
|
2138
|
+
\\ {"name": "offsetY", "type": {"kind": "f64"}},
|
|
2139
|
+
\\ {"name": "velocityX", "type": {"kind": "f64"}},
|
|
2140
|
+
\\ {"name": "velocityY", "type": {"kind": "f64"}},
|
|
2141
|
+
\\ {"name": "viewportExtentX", "type": {"kind": "f64"}},
|
|
2142
|
+
\\ {"name": "viewportExtentY", "type": {"kind": "f64"}},
|
|
2143
|
+
\\ {"name": "contentExtentX", "type": {"kind": "f64"}},
|
|
2144
|
+
\\ {"name": "contentExtentY", "type": {"kind": "f64"}}
|
|
2145
|
+
\\ ]},
|
|
2146
|
+
;
|
|
2147
|
+
var source = try std.mem.replaceOwned(
|
|
2148
|
+
u8,
|
|
2149
|
+
arena,
|
|
2150
|
+
sidecar_mod.minimal_valid_json,
|
|
2151
|
+
"\"structs\": [\n",
|
|
2152
|
+
try std.fmt.allocPrint(arena, "\"structs\": [\n{s}\n", .{scroll_struct}),
|
|
2153
|
+
);
|
|
2154
|
+
source = try std.mem.replaceOwned(
|
|
2155
|
+
u8,
|
|
2156
|
+
arena,
|
|
2157
|
+
source,
|
|
2158
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
2159
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}},\n {\"name\": \"scrolled\", \"payload\": {\"kind\": \"record\", \"name\": \"Scroll\"}}",
|
|
2160
|
+
);
|
|
2161
|
+
const generated = try emitFromJson(arena, source);
|
|
2162
|
+
try testing.expect(std.mem.indexOf(
|
|
2163
|
+
u8,
|
|
2164
|
+
generated,
|
|
2165
|
+
"abi.dispatch_scroll_state(1, payload.offsetX, payload.offsetY, payload.velocityX, payload.velocityY, payload.viewportExtentX, payload.viewportExtentY, payload.contentExtentX, payload.contentExtentY, &cmd_ptr, &cmd_len)",
|
|
2166
|
+
) != null);
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
test "the retired one-axis scroll quartet rides the record entry" {
|
|
2170
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2171
|
+
defer arena_state.deinit();
|
|
2172
|
+
const arena = arena_state.allocator();
|
|
2173
|
+
// The pre-two-axis shape `{offset, velocity, viewportExtent,
|
|
2174
|
+
// contentExtent}` is an ordinary record now — the markup engines
|
|
2175
|
+
// refuse to bind `on-scroll` to it (with a teaching that names the
|
|
2176
|
+
// per-axis fields), so dispatch must not claim the scalar entry.
|
|
2177
|
+
const legacy_struct =
|
|
2178
|
+
\\ {"name": "Scroll", "fields": [
|
|
2179
|
+
\\ {"name": "offset", "type": {"kind": "f64"}},
|
|
2180
|
+
\\ {"name": "velocity", "type": {"kind": "f64"}},
|
|
2181
|
+
\\ {"name": "viewportExtent", "type": {"kind": "f64"}},
|
|
2182
|
+
\\ {"name": "contentExtent", "type": {"kind": "f64"}}
|
|
2183
|
+
\\ ]},
|
|
2184
|
+
;
|
|
2185
|
+
var source = try std.mem.replaceOwned(
|
|
2186
|
+
u8,
|
|
2187
|
+
arena,
|
|
2188
|
+
sidecar_mod.minimal_valid_json,
|
|
2189
|
+
"\"structs\": [\n",
|
|
2190
|
+
try std.fmt.allocPrint(arena, "\"structs\": [\n{s}\n", .{legacy_struct}),
|
|
2191
|
+
);
|
|
2192
|
+
source = try std.mem.replaceOwned(
|
|
2193
|
+
u8,
|
|
2194
|
+
arena,
|
|
2195
|
+
source,
|
|
2196
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
2197
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}},\n {\"name\": \"scrolled\", \"payload\": {\"kind\": \"record\", \"name\": \"Scroll\"}}",
|
|
2198
|
+
);
|
|
2199
|
+
const generated = try emitFromJson(arena, source);
|
|
2200
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi.dispatch_record(1,") != null);
|
|
2201
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi.dispatch_scroll_state(1,") == null);
|
|
2202
|
+
}
|
|
2203
|
+
|
|
2204
|
+
test "boot references every attested export so the link proves the set" {
|
|
2205
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2206
|
+
defer arena_state.deinit();
|
|
2207
|
+
const arena = arena_state.allocator();
|
|
2208
|
+
const generated = try emitFromJson(arena, sidecar_mod.minimal_valid_json);
|
|
2209
|
+
try testing.expect(std.mem.indexOf(u8, generated, "fn referenceAttestedExports() void {") != null);
|
|
2210
|
+
try testing.expect(std.mem.indexOf(u8, generated, "std.mem.doNotOptimizeAway(abi.collect);") != null);
|
|
2211
|
+
try testing.expect(std.mem.indexOf(u8, generated, "std.mem.doNotOptimizeAway(abi.abi_version_fn);") != null);
|
|
2212
|
+
// Unwired channel entries are NOT attested and must not be
|
|
2213
|
+
// referenced (their absence in the object is the valid state).
|
|
2214
|
+
try testing.expect(std.mem.indexOf(u8, generated, "doNotOptimizeAway(abi.key_msg)") == null);
|
|
2215
|
+
}
|
|
2216
|
+
|
|
2217
|
+
test "sidecar-selected root names get the wiring's fixed spellings as aliases" {
|
|
2218
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2219
|
+
defer arena_state.deinit();
|
|
2220
|
+
const arena = arena_state.allocator();
|
|
2221
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"Model\"", "\"State\"");
|
|
2222
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"slot\": \"Model.count\"", "\"slot\": \"State.count\"");
|
|
2223
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"name\": \"Msg\"", "\"name\": \"Event\"");
|
|
2224
|
+
const generated = try emitFromJson(arena, source);
|
|
2225
|
+
// The host wiring re-exports core.Model/core.Msg by those exact
|
|
2226
|
+
// spellings; the aliases keep a renamed contract compilable.
|
|
2227
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub const Model = State;") != null);
|
|
2228
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub const Msg = Event;") != null);
|
|
2229
|
+
// The default names alias nothing (a self-alias would not compile).
|
|
2230
|
+
const default_generated = try emitFromJson(arena, sidecar_mod.minimal_valid_json);
|
|
2231
|
+
try testing.expect(std.mem.indexOf(u8, default_generated, "pub const Model = Model;") == null);
|
|
2232
|
+
try testing.expect(std.mem.indexOf(u8, default_generated, "pub const Msg = Msg;") == null);
|
|
2233
|
+
}
|
|
2234
|
+
|
|
2235
|
+
test "channel detection names are reserved even when the channel is absent" {
|
|
2236
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2237
|
+
defer arena_state.deinit();
|
|
2238
|
+
const arena = arena_state.allocator();
|
|
2239
|
+
// channels.frame_msg is false in the minimal sidecar, but the host
|
|
2240
|
+
// probes the DECL name — a type called frameMsg would falsely wire
|
|
2241
|
+
// the channel and then fail as a non-function.
|
|
2242
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"enums\": []", "\"enums\": [{\"name\": \"frameMsg\", \"members\": [\"a\"]}]");
|
|
2243
|
+
source = try std.mem.replaceOwned(
|
|
2244
|
+
u8,
|
|
2245
|
+
arena,
|
|
2246
|
+
source,
|
|
2247
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"bytes\"}}",
|
|
2248
|
+
"{\"name\": \"label\", \"type\": {\"kind\": \"enum\", \"name\": \"frameMsg\"}}",
|
|
2249
|
+
);
|
|
2250
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
2251
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
2252
|
+
try testing.expectError(error.Refused, emit(arena, parsed, &diags));
|
|
2253
|
+
var found = false;
|
|
2254
|
+
for (diags.list.items) |item| {
|
|
2255
|
+
if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, "\"frameMsg\" collides") != null) found = true;
|
|
2256
|
+
}
|
|
2257
|
+
try testing.expect(found);
|
|
2258
|
+
}
|
|
2259
|
+
|
|
2260
|
+
test "a chrome arm holding its insets by reference refuses" {
|
|
2261
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2262
|
+
defer arena_state.deinit();
|
|
2263
|
+
const arena = arena_state.allocator();
|
|
2264
|
+
// The host constructs the chrome record BY VALUE field by field; a
|
|
2265
|
+
// node (by-reference) insets record cannot take that construction.
|
|
2266
|
+
const source =
|
|
2267
|
+
\\{
|
|
2268
|
+
\\ "format": 1, "wire_version": 3, "abi_version": 1,
|
|
2269
|
+
\\ "compiler_version": "0.0.1", "entry": "src/core.ts",
|
|
2270
|
+
\\ "source_hash": "00000000c0ffee00", "build_id": "00000000b01dface",
|
|
2271
|
+
\\ "types": {
|
|
2272
|
+
\\ "structs": [
|
|
2273
|
+
\\ {"name": "Model", "fields": [{"name": "chromeTop", "type": {"kind": "f64"}}]},
|
|
2274
|
+
\\ {"name": "Insets", "fields": [
|
|
2275
|
+
\\ {"name": "top", "type": {"kind": "f64"}}, {"name": "right", "type": {"kind": "f64"}},
|
|
2276
|
+
\\ {"name": "bottom", "type": {"kind": "f64"}}, {"name": "left", "type": {"kind": "f64"}}
|
|
2277
|
+
\\ ]},
|
|
2278
|
+
\\ {"name": "Buttons", "fields": [
|
|
2279
|
+
\\ {"name": "x", "type": {"kind": "f64"}}, {"name": "y", "type": {"kind": "f64"}},
|
|
2280
|
+
\\ {"name": "width", "type": {"kind": "f64"}}, {"name": "height", "type": {"kind": "f64"}}
|
|
2281
|
+
\\ ]},
|
|
2282
|
+
\\ {"name": "Msg_chrome_changed", "fields": [
|
|
2283
|
+
\\ {"name": "insets", "type": {"kind": "node", "name": "Insets"}},
|
|
2284
|
+
\\ {"name": "buttons", "type": {"kind": "value", "name": "Buttons"}},
|
|
2285
|
+
\\ {"name": "tabsProjected", "type": {"kind": "bool"}}
|
|
2286
|
+
\\ ]}
|
|
2287
|
+
\\ ],
|
|
2288
|
+
\\ "enums": [], "unions": []
|
|
2289
|
+
\\ },
|
|
2290
|
+
\\ "model": "Model", "model_helpers": [], "model_unbound": [],
|
|
2291
|
+
\\ "msg": {"name": "Msg", "arms": [
|
|
2292
|
+
\\ {"name": "chrome_changed", "payload": {"kind": "record", "name": "Msg_chrome_changed"}}
|
|
2293
|
+
\\ ], "unbound": []},
|
|
2294
|
+
\\ "init_returns_cmd": false, "update_returns_cmd": true, "has_subscriptions": false,
|
|
2295
|
+
\\ "channels": {"command_msg": false, "frame_msg": false, "key_msg": false, "pinch_msg": false,
|
|
2296
|
+
\\ "appearance_msg": null, "chrome_msg": "chrome_changed", "env_msgs": []},
|
|
2297
|
+
\\ "abi": {"prefix": "nsc_core_", "exports": ["abi_version", "build_id", "set_panic_sink", "init",
|
|
2298
|
+
\\ "collect", "frame_reset", "boot_cmd", "dispatch_void", "dispatch_bytes", "dispatch_number",
|
|
2299
|
+
\\ "dispatch_number_bytes", "dispatch_bool", "dispatch_enum", "dispatch_record",
|
|
2300
|
+
\\ "dispatch_text_input", "dispatch_scroll_state", "subscriptions", "model_snapshot",
|
|
2301
|
+
\\ "helper_call"], "snapshot_format": 1},
|
|
2302
|
+
\\ "integer_slots": [], "deterministic": true, "async_free": true
|
|
2303
|
+
\\}
|
|
2304
|
+
;
|
|
2305
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
2306
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
2307
|
+
try testing.expectError(error.Refused, emit(arena, parsed, &diags));
|
|
2308
|
+
var found = false;
|
|
2309
|
+
for (diags.list.items) |item| {
|
|
2310
|
+
if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, "insets: top/right/bottom/left numbers") != null) found = true;
|
|
2311
|
+
}
|
|
2312
|
+
try testing.expect(found);
|
|
2313
|
+
}
|
|
2314
|
+
|
|
2315
|
+
test "the shim restates the module-graph attestations" {
|
|
2316
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2317
|
+
defer arena_state.deinit();
|
|
2318
|
+
const arena = arena_state.allocator();
|
|
2319
|
+
const generated = try emitFromJson(arena, sidecar_mod.minimal_valid_json);
|
|
2320
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub const deterministic: bool = true;") != null);
|
|
2321
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub const async_free: bool = true;") != null);
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
test "number_bytes mirrors number-first; other orders ride the record family" {
|
|
2325
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2326
|
+
defer arena_state.deinit();
|
|
2327
|
+
const arena = arena_state.allocator();
|
|
2328
|
+
// Family 4 carries no order fact, so the mirror emits the one
|
|
2329
|
+
// order every producer of this shape declares (SCHEMA-GAPS.md
|
|
2330
|
+
// records the missing fact and its closure). A bytes-first record
|
|
2331
|
+
// is still fully expressible: the record family's table entry
|
|
2332
|
+
// carries order explicitly — the documented route, pinned here.
|
|
2333
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"structs\": [", "\"structs\": [\n {\"name\": \"Msg_loaded\", \"fields\": [{\"name\": \"body\", \"type\": {\"kind\": \"bytes\"}}, {\"name\": \"status\", \"type\": {\"kind\": \"f64\"}}]},");
|
|
2334
|
+
source = try std.mem.replaceOwned(
|
|
2335
|
+
u8,
|
|
2336
|
+
arena,
|
|
2337
|
+
source,
|
|
2338
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
2339
|
+
"{\"name\": \"loaded\", \"payload\": {\"kind\": \"record\", \"name\": \"Msg_loaded\"}}",
|
|
2340
|
+
);
|
|
2341
|
+
const generated = try emitFromJson(arena, source);
|
|
2342
|
+
// Declared order preserved exactly: bytes first, number second.
|
|
2343
|
+
try testing.expect(std.mem.indexOf(u8, generated, "loaded: struct { body: []const u8, status: f64 },") != null);
|
|
2344
|
+
try testing.expect(std.mem.indexOf(u8, generated, "abi.dispatch_record(0,") != null);
|
|
2345
|
+
}
|
|
2346
|
+
|
|
2347
|
+
test "a single-use pattern-named record inlines to mirror the emitted module" {
|
|
2348
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2349
|
+
defer arena_state.deinit();
|
|
2350
|
+
const arena = arena_state.allocator();
|
|
2351
|
+
// A legacy null-origin sidecar retains the pattern fallback: this is
|
|
2352
|
+
// the shape every real anonymous record in the older conformance
|
|
2353
|
+
// corpus takes, and a named declaration here would skew its artifacts.
|
|
2354
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"structs\": [", "\"structs\": [\n {\"name\": \"Msg_loaded\", \"fields\": [{\"name\": \"status\", \"type\": {\"kind\": \"f64\"}}, {\"name\": \"ok\", \"type\": {\"kind\": \"bool\"}}]},");
|
|
2355
|
+
source = try std.mem.replaceOwned(
|
|
2356
|
+
u8,
|
|
2357
|
+
arena,
|
|
2358
|
+
source,
|
|
2359
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
2360
|
+
"{\"name\": \"loaded\", \"payload\": {\"kind\": \"record\", \"name\": \"Msg_loaded\"}}",
|
|
2361
|
+
);
|
|
2362
|
+
const generated = try emitFromJson(arena, source);
|
|
2363
|
+
try testing.expect(std.mem.indexOf(u8, generated, "loaded: struct { status: f64, ok: bool },") != null);
|
|
2364
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub const Msg_loaded") == null);
|
|
2365
|
+
}
|
|
2366
|
+
|
|
2367
|
+
test "a single-use authored pattern-named record stays declared by provenance" {
|
|
2368
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2369
|
+
defer arena_state.deinit();
|
|
2370
|
+
const arena = arena_state.allocator();
|
|
2371
|
+
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\"}}]},");
|
|
2372
|
+
source = try std.mem.replaceOwned(
|
|
2373
|
+
u8,
|
|
2374
|
+
arena,
|
|
2375
|
+
source,
|
|
2376
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
2377
|
+
"{\"name\": \"loaded\", \"member\": \"payload\", \"payload\": {\"kind\": \"record\", \"name\": \"Msg_loaded\"}}",
|
|
2378
|
+
);
|
|
2379
|
+
const generated = try emitFromJson(arena, source);
|
|
2380
|
+
try testing.expect(std.mem.indexOf(u8, generated, "pub const Msg_loaded = struct") != null);
|
|
2381
|
+
try testing.expect(std.mem.indexOf(u8, generated, "loaded: Msg_loaded,") != null);
|
|
2382
|
+
}
|
|
2383
|
+
|
|
2384
|
+
test "keywords and exotic names are quoted" {
|
|
2385
|
+
try testing.expect(!isPlainIdentifier("error"));
|
|
2386
|
+
try testing.expect(!isPlainIdentifier("test"));
|
|
2387
|
+
try testing.expect(!isPlainIdentifier("u8"));
|
|
2388
|
+
try testing.expect(!isPlainIdentifier("1abc"));
|
|
2389
|
+
try testing.expect(!isPlainIdentifier("_"));
|
|
2390
|
+
try testing.expect(isPlainIdentifier("_x"));
|
|
2391
|
+
try testing.expect(isPlainIdentifier("super"));
|
|
2392
|
+
try testing.expect(isPlainIdentifier("chatScrollTop"));
|
|
2393
|
+
}
|
|
2394
|
+
|
|
2395
|
+
test "an appearance channel on a wrong-shaped arm refuses with a teaching" {
|
|
2396
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2397
|
+
defer arena_state.deinit();
|
|
2398
|
+
const arena = arena_state.allocator();
|
|
2399
|
+
// An enum payload passes the schema's named-type-family rule (V9)
|
|
2400
|
+
// but the host cannot build the appearance record into it.
|
|
2401
|
+
var source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "\"enums\": []", "\"enums\": [{\"name\": \"Phase\", \"members\": [\"a\", \"b\"]}]");
|
|
2402
|
+
source = try std.mem.replaceOwned(
|
|
2403
|
+
u8,
|
|
2404
|
+
arena,
|
|
2405
|
+
source,
|
|
2406
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"void\"}}",
|
|
2407
|
+
"{\"name\": \"bump\", \"payload\": {\"kind\": \"enum\", \"name\": \"Phase\"}}",
|
|
2408
|
+
);
|
|
2409
|
+
source = try std.mem.replaceOwned(u8, arena, source, "\"appearance_msg\": null", "\"appearance_msg\": \"bump\"");
|
|
2410
|
+
var diags = sidecar_mod.Diagnostics{ .arena = arena };
|
|
2411
|
+
const parsed = try sidecar_mod.read(arena, source, &diags);
|
|
2412
|
+
try testing.expectError(error.Refused, emit(arena, parsed, &diags));
|
|
2413
|
+
var found = false;
|
|
2414
|
+
for (diags.list.items) |item| {
|
|
2415
|
+
if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, "colorScheme: a light/dark enum") != null) found = true;
|
|
2416
|
+
}
|
|
2417
|
+
try testing.expect(found);
|
|
2418
|
+
}
|