summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/detached/wire.zig14
-rw-r--r--src/fs.zig16
-rw-r--r--src/ninep/pty.zig2
-rw-r--r--src/tape.zig52
-rw-r--r--src/terminal.zig2
-rw-r--r--src/walk.zig132
6 files changed, 199 insertions, 19 deletions
diff --git a/src/detached/wire.zig b/src/detached/wire.zig
index 119601fc..1c9e554e 100644
--- a/src/detached/wire.zig
+++ b/src/detached/wire.zig
@@ -29,9 +29,13 @@ const family = switch (config.platform) {
/// What makes two cores step differently, hashed: a replica's hello carries
/// its own, and one of another build is refused. The protocol, the release
/// and commit, the platform and build options, every dependency's content
-/// hash (grammars, ghostty, Unicode) and the walker codec's assumptions
-/// (pointer width, byte order). Not the OS: a Linux client of a FreeBSD
-/// session is the main case, and what differs there is the host's, taped.
+/// hash (grammars, ghostty, Unicode), the walker codec's assumptions
+/// (pointer width, byte order) and the tape's shape (tape.shape: what each
+/// input and each answer of the host's is laid out as). Not the OS: a Linux
+/// client of a FreeBSD session is the main case, and what differs there is
+/// the host's, taped, in a shape every OS shares (an error by its name, a
+/// stat as fs.OsStat). Nor the build mode: a Debug build steps as a
+/// release one does (regexp.zig's one budget).
/// Nor the terminal's build against the window's: their cores step alike,
/// the GUI's own words by the view's frontend (builtins.zig `here`), so a
/// window replicates a terminal's session and a terminal a window's.
@@ -41,14 +45,14 @@ const family = switch (config.platform) {
pub const semantics: u64 = blk: {
@setEvalBranchQuota(1_000_000);
break :blk std.hash.Wyhash.hash(0, std.fmt.comptimePrint(
- "wire {d}\npardes {s} {s}\n{s} grammars {} {} {} {} mupdf {} themes {} workspace_tag {} sources {} board {d}x{d}\nusize {d} {t}\n{s}",
+ "wire {d}\npardes {s} {s}\n{s} grammars {} {} {} {} mupdf {} themes {} workspace_tag {} sources {} board {d}x{d}\nusize {d} {t}\n{s}\n{s}",
.{
version, config.version, config.commit orelse "unstamped",
family, config.syntax_highlighting, config.syntax_zig_grammar,
config.syntax_minimal_grammars, config.syntax_full_grammars, config.mupdf,
config.theme_animation, config.workspace_tag, config.embed_sources,
config.esp32p4_cols, config.esp32p4_rows, @sizeOf(usize),
- builtin.cpu.arch.endian(), config.deps,
+ builtin.cpu.arch.endian(), config.deps, pardes.tape.shape,
},
));
};
diff --git a/src/fs.zig b/src/fs.zig
index 4bbf09bb..733418da 100644
--- a/src/fs.zig
+++ b/src/fs.zig
@@ -171,8 +171,8 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply {
const path = osPath(p, req.node) orelse return Reply.fail(req.tag, E.NOENT);
if (req.op == .release) return .{ .tag = req.tag };
const io = std.Io.Threaded.global_single_threaded.io();
- const stat = pardes.tape.site(.os_stat, null, statPath, .{ io, path, std.Io.Dir.StatFileOptions{} }) catch return Reply.fail(req.tag, E.NOENT);
- const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 };
+ const stat = pardes.tape.site(.os_stat, null, osStat, .{ io, path }) catch return Reply.fail(req.tag, E.NOENT);
+ const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = stat.mtime };
switch (req.op) {
.getattr => return .{ .tag = req.tag, .attr = attr },
.open => {
@@ -223,6 +223,16 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply {
}
}
+/// What /os keeps of a stat, as the tape carries it to a replica: the same
+/// bytes on every OS, which std.Io.File.Stat is not (a FreeBSD one's
+/// `nlink` and `permissions` are another width than a Linux one's).
+pub const OsStat = struct { kind: std.Io.File.Kind, size: u64, mtime: u32 };
+
+fn osStat(io: std.Io, path: []const u8) !OsStat {
+ const stat = try statPath(io, path, .{});
+ return .{ .kind = stat.kind, .size = stat.size, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 };
+}
+
/// A directory's listing under /os, the entries from `off` until `size`
/// bytes of them are staged, into `listing`: the syscalls of a readdir.
fn osListing(io: std.Io, path: []const u8, off: u64, size: u32, listing: *[16 * 1024]u8) error{ NotDir, Io }![]const u8 {
@@ -249,7 +259,7 @@ fn osListing(io: std.Io, path: []const u8, off: u64, size: u32, listing: *[16 *
return listed.items;
}
-const OsReadWrite = union(enum) { reply: Reply, read: []const u8 };
+pub const OsReadWrite = union(enum) { reply: Reply, read: []const u8 };
/// The syscalls of an /os read, write or truncate: the reply, or the bytes
/// read into `buf`.
diff --git a/src/ninep/pty.zig b/src/ninep/pty.zig
index e68ad84f..53fc5c68 100644
--- a/src/ninep/pty.zig
+++ b/src/ninep/pty.zig
@@ -411,7 +411,7 @@ fn finish(p: *Pardes, slot: *Run, pane: *Pane, status_code: ?i32) void {
answer(p, slot, "exit {s}", .{status});
}
-const Printed = struct { text: []const u8, whole: bool };
+pub const Printed = struct { text: []const u8, whole: bool };
/// What pane `pane`'s command printed (terminal.commandOutput), in `arena`.
fn printedBy(pane: *Pane, arena: std.mem.Allocator) ?Printed {
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);
diff --git a/src/terminal.zig b/src/terminal.zig
index acff68f4..63ce4f36 100644
--- a/src/terminal.zig
+++ b/src/terminal.zig
@@ -1188,7 +1188,7 @@ fn pointerRowCount(pane: *const Pane) usize {
/// erased it, whether the screen was cleared, whether its pages lived and
/// whether a pin could not be made. Taped: a screen-state replica, which
/// ingests nothing, is told the authority's.
-const Told = struct { rows: []const i32, erased: []const bool, cleared: bool, alive: bool, failed: bool };
+pub const Told = struct { rows: []const i32, erased: []const bool, cleared: bool, alive: bool, failed: bool };
fn told(t: Told) Told {
return t;
diff --git a/src/walk.zig b/src/walk.zig
index 1c5bbbfc..88f62736 100644
--- a/src/walk.zig
+++ b/src/walk.zig
@@ -388,9 +388,14 @@ fn hashMap(comptime T: type, v: *const T, h: *Hasher) void {
h.update(std.mem.asBytes(&sum));
}
-/// The value as bytes, the same walk: what the tape carries (src/tape.zig).
-/// Only for the process that wrote it, or one of the same build: native
-/// byte order, and an error as its number in this build.
+/// The value as bytes, the same walk: what the tape carries (src/tape.zig),
+/// to a replica of another build too (a window of a terminal's session, a
+/// Linux client of a FreeBSD one). Native byte order, which every machine
+/// pardes runs on shares, and an error by its name: its number is its
+/// build's own (a Linux terminal's FileNotFound is a window's
+/// NoDefaultValue). What a field is laid out as (a std.Io.File.Stat's
+/// `nlink` is another width on FreeBSD) is the tape's shape, which
+/// wire.semantics hashes (`shape`).
pub fn encode(comptime T: type, v: *const T, w: *std.Io.Writer) std.Io.Writer.Error!void {
switch (@typeInfo(T)) {
.void, .null, .undefined => {},
@@ -424,18 +429,91 @@ pub fn encode(comptime T: type, v: *const T, w: *std.Io.Writer) std.Io.Writer.Er
inline else => |*payload| try encode(@TypeOf(payload.*), payload, w),
}
},
- .error_set => try w.writeAll(std.mem.asBytes(&@as(u16, @intFromError(v.*)))),
+ .error_set => try encodeError(T, v.*, w),
.error_union => |e| if (v.*) |*payload| {
try w.writeByte(1);
try encode(e.payload, payload, w);
} else |err| {
try w.writeByte(0);
- try w.writeAll(std.mem.asBytes(&@as(u16, @intFromError(err))));
+ try encodeError(e.error_set, err, w);
},
else => @compileError("walk: cannot encode " ++ @typeName(T)),
}
}
+/// What `encode` lays each of `types` out as, in words: each field, element
+/// and tag where it goes, an enum's names and numbers, an error as an
+/// error (it goes by its name). wire.semantics hashes the tape's: builds
+/// whose tapes differ in shape, as a FreeBSD std.Io.File.Stat differs from
+/// a Linux one, refuse each other's replicas in words, not read each other
+/// out of step.
+pub fn shape(comptime types: []const type) []const u8 {
+ comptime {
+ @setEvalBranchQuota(10_000_000);
+ var out: []const u8 = "";
+ for (types) |T| out = out ++ shapeOf(T, 0) ++ "\n";
+ return out;
+ }
+}
+
+fn shapeOf(comptime T: type, comptime depth: u8) []const u8 {
+ comptime {
+ if (depth > 32) @compileError("walk: the tape's shape nests past 32 deep at " ++ @typeName(T) ++ ": a type that holds itself?");
+ return switch (@typeInfo(T)) {
+ .void, .null, .undefined => "v",
+ .bool => "b",
+ .int => |i| std.fmt.comptimePrint("{t}{d}", .{ i.signedness, i.bits }),
+ .float => |f| std.fmt.comptimePrint("f{d}", .{f.bits}),
+ .@"enum" => |e| blk: {
+ var out: []const u8 = shapeOf(e.tag_type, depth + 1) ++ "{";
+ for (e.field_names, e.field_values) |n, v| out = out ++ std.fmt.comptimePrint("{s}={d},", .{ n, v });
+ break :blk out ++ "}";
+ },
+ .optional => |o| if (machinery(o.child)) "0" else "?" ++ shapeOf(o.child, depth + 1),
+ .array => |a| std.fmt.comptimePrint("[{d}]", .{a.len}) ++ shapeOf(a.child, depth + 1),
+ .pointer => |p| switch (p.size) {
+ .one => shapeOf(p.child, depth + 1),
+ .slice => "[]" ++ shapeOf(p.child, depth + 1),
+ else => @compileError("walk: a many-pointer has no length: " ++ @typeName(T)),
+ },
+ .@"struct" => |s| blk: {
+ if (machinery(T)) break :blk "";
+ var out: []const u8 = if (s.layout == .@"packed") "p{" else "{";
+ for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| {
+ if (attr.@"comptime" or excluded(T, name)) continue;
+ out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ",";
+ }
+ break :blk out ++ "}";
+ },
+ .@"union" => |u| blk: {
+ var out: []const u8 = shapeOf(u.tag_type.?, depth + 1) ++ "(";
+ for (u.field_names, u.field_types) |name, F| out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ",";
+ break :blk out ++ ")";
+ },
+ .error_set => "error",
+ .error_union => |e| "!" ++ shapeOf(e.payload, depth + 1),
+ else => @compileError("walk: cannot encode " ++ @typeName(T)),
+ };
+ }
+}
+
+/// An error by its name. An `anyerror` must be one `decode` can read back
+/// (`errorNamed`): a Debug build stops at one that is not, as it is taped.
+fn encodeError(comptime E: type, err: E, w: *std.Io.Writer) std.Io.Writer.Error!void {
+ if (comptime E == anyerror and builtin.mode == .debug) if (errorNamed(E, @errorName(err)) == null)
+ std.debug.panic("walk: error.{s} is taped as an anyerror, which a replica reads back only as one of tape.Failure's: add the set it comes from", .{@errorName(err)});
+ try writeName(w, @errorName(err));
+}
+
+/// The error of `E` called `name`. `anyerror` keeps no list of its names
+/// to look one up in: one is read back as one of tape.Failure's, what a
+/// host reports as failed (the tape's only anyerrors).
+fn errorNamed(comptime E: type, name: []const u8) ?E {
+ const Named = if (E == anyerror) @import("tape.zig").Failure else E;
+ inline for (@typeInfo(Named).error_set.error_names.?) |n| if (std.mem.eql(u8, name, n)) return @field(Named, n);
+ return null;
+}
+
pub const Reader = struct {
bytes: []const u8,
at: usize = 0,
@@ -451,9 +529,13 @@ pub const Reader = struct {
}
};
+/// What an error read back is when this build has none of that name (an
+/// OS's own, a newer build's): the tape is another build's, and diverges.
+pub const TapeError = error{ EndOfTape, OutOfMemory, NoSuchError };
+
/// The inverse of `encode`; slices (and what a pointer points at) are made
/// in `a`. A field `encode` left out is its default.
-pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTape, OutOfMemory }!T {
+pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) TapeError!T {
switch (@typeInfo(T)) {
.void, .null, .undefined => return {},
.bool => return (try r.take(1))[0] != 0,
@@ -500,7 +582,7 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTa
@field(out, name) = @as(*const F, @ptrCast(@alignCast(default))).*;
} else if (comptime @typeInfo(F) == .error_set) {
// No `!anyerror` to return it through: read in place.
- @field(out, name) = @errorCast(@errorFromInt(try r.scalar(u16)));
+ try decodeError(F, r, &@field(out, name));
} else @field(out, name) = try decode(F, r, a);
}
return out;
@@ -512,17 +594,24 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTa
};
unreachable;
},
- .error_set => return @errorCast(@errorFromInt(try r.scalar(u16))),
.error_union => |e| {
if ((try r.take(1))[0] != 0) return try decode(e.payload, r, a);
// The value's error, not decode's own.
- const err: e.error_set = @errorCast(@errorFromInt(try r.scalar(u16)));
+ var err: e.error_set = undefined;
+ try decodeError(e.error_set, r, &err);
return @as(T, err);
},
else => @compileError("walk: cannot decode " ++ @typeName(T)),
}
}
+/// An error by its name, as `encode` wrote it, into `out`: no error union
+/// holds an error set.
+fn decodeError(comptime E: type, r: *Reader, out: *E) TapeError!void {
+ const len = try r.scalar(u16);
+ out.* = errorNamed(E, try r.take(len)) orelse return error.NoSuchError;
+}
+
test "a value comes back from its bytes" {
const E = union(enum) { key: struct { cp: u21, text: []const u8 }, tick, failed: anyerror!u32 };
var buf: [256]u8 = undefined;
@@ -536,6 +625,31 @@ test "a value comes back from its bytes" {
try std.testing.expectEqual(r.bytes.len, r.at);
}
+test "an error is taped by its name, which another build reads back as its own" {
+ // The build's number for an error is its own: 451 of 505 names were
+ // numbered otherwise in a Linux window's build than a terminal's, so a
+ // window replica read a terminal session's FileNotFound as another.
+ var buf: [64]u8 = undefined;
+ var w: std.Io.Writer = .fixed(&buf);
+ const failed: error{ FileNotFound, NotDir }!u32 = error.NotDir;
+ try encode(@TypeOf(failed), &failed, &w);
+ try std.testing.expectEqualSlices(u8, "\x00\x06\x00NotDir", w.buffered());
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ var r: Reader = .{ .bytes = w.buffered() };
+ try std.testing.expectError(error.NotDir, try decode(@TypeOf(failed), &r, arena.allocator()));
+ // A name this build's set lacks (an OS's own): the tape is another
+ // build's, and the replay diverges rather than read some other error.
+ r = .{ .bytes = "\x00\x0c\x00FileTooLarge" };
+ try std.testing.expectError(error.NoSuchError, decode(@TypeOf(failed), &r, arena.allocator()));
+ // An anyerror (a host's report) is read back as one of tape.Failure's.
+ const Report = struct { err: anyerror };
+ w = .fixed(&buf);
+ try encode(Report, &.{ .err = error.FileNotFound }, &w);
+ r = .{ .bytes = w.buffered() };
+ try std.testing.expectEqual(error.FileNotFound, (try decode(Report, &r, arena.allocator())).err);
+}
+
test "a digest is of values: the same text at two addresses is one digest" {
const S = struct { name: []const u8, n: u32, next: ?*const u32 };
const a = "same text";