diff options
Diffstat (limited to 'src/tape.zig')
| -rw-r--r-- | src/tape.zig | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/tape.zig b/src/tape.zig index 9f1cde42..3c8dd765 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -20,6 +20,8 @@ const std = @import("std"); const pardes = @import("pardes.zig"); const walk = @import("walk.zig"); +const host_io = @import("host_io.zig"); +const terminal = @import("terminal.zig"); const Pardes = pardes.Pardes; const VTable = pardes.Host.VTable; const libc = std.c; @@ -107,6 +109,50 @@ pub const Site = enum(u16) { prompt, }; +/// What the sites return. wire.semantics hashes their shape with the +/// rest of the tape's (`shape`), so two builds whose tapes differ in it +/// refuse each other's replicas in words, and a site returning a type not +/// listed is a compile error (`call`). An error union is listed as +/// `anyerror!` its payload: an error goes by its name (walk.encode). +pub const results = .{ + void, bool, + u32, i64, + [8]u8, ?u64, + []const u8, ?[]const u8, + ?std.Io.File.Kind, ?pardes.Presentation.CellPosition, + ?pardes.filesystem.Resolved, pardes.filesystem.OsReadWrite, + anyerror!pardes.filesystem.OsStat, anyerror![]const u8, + anyerror![]u8, anyerror!void, + terminal.Told, ?pardes.ctlfs.pty.Printed, + @typeInfo(@TypeOf(terminal.greeting)).@"fn".return_type.?, +}; + +/// What a host reports to the core as failed (`shell_failed`, `save_failed`, +/// `dump_failed`, `report_error`), as a replica reads it back: an error is +/// taped by its name, and `anyerror` keeps no list of names to look one up +/// in. ponytail: kept by hand, the sets of what a host reports from; one +/// outside them is a Debug build's panic as it is taped (walk.encode) and a +/// release build's divergence (the replica joins again). No host, none. +pub const Failure = if (!pardes.hosted) error{} else ErrorOf(host_io.forkShell) || ErrorOf(pardes.filesystem.write) || + ErrorOf(pardes.filesystem.readRestore) || ErrorOf(pardes.dump.restore) || + @typeInfo(@TypeOf(pardes.config.User.dumpThemes(undefined, undefined, undefined, pardes.themes))).error_union.error_set || + ErrorOf(host_io.Lsp.snapshot) || + ErrorOf(host_io.Inbox.startReader) || std.Io.File.ReadStreamingError || + error{ NoDumpDirectory, ListenFailed, WorkersUnavailable, WorkersBusy }; + +fn ErrorOf(comptime f: anytype) type { + return @typeInfo(@typeInfo(@TypeOf(f)).@"fn".return_type.?).error_union.error_set; +} + +/// Every type the tape carries, as walk.shape says it: wire.semantics's. +pub const shape = blk: { + var types: []const type = &.{ Input, []const u64, struct { u8, u64 } }; + for (results) |R| types = types ++ .{R}; + for (host_fns) |f| types = types ++ .{@typeInfo(@typeInfo(@typeInfo(@FieldType(VTable, f)).optional.child).pointer.child).@"fn".return_type.?}; + for (watched) |name| types = types ++ .{@FieldType(Pardes, name)}; + break :blk walk.shape(types); +}; + /// Fields a host writes straight into the core between its calls. const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active", "unattended" }; @@ -769,6 +815,12 @@ pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@Ty fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? { const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?; + comptime { + const Listed = if (@typeInfo(R) == .error_union) anyerror!@typeInfo(R).error_union.payload else R; + for (results) |T| { + if (T == Listed) break; + } else @compileError("tape: the site ." ++ @tagName(s) ++ " returns " ++ @typeName(R) ++ ", which tape.results does not list"); + } // No tape where there is no host to record or replay one. if (comptime !pardes.hosted) return if (yields) out(f, args) else @call(.auto, f, args); const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); |
