summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Core.zig11
-rw-r--r--core/Effect.zig21
-rw-r--r--core/replica.zig139
-rw-r--r--core/tape.zig66
4 files changed, 229 insertions, 8 deletions
diff --git a/core/Core.zig b/core/Core.zig
index a19453af..188df34c 100644
--- a/core/Core.zig
+++ b/core/Core.zig
@@ -175,6 +175,17 @@ pub const View = struct {
/// press during an active middle select-drag; consumed on middle release
chord_arg: ?[]u8 = null,
clip_pending: ?edit.ClipRequest = null,
+ /// What the frontend showing this view does on its own machine for an
+ /// input made in it (the user's ruling: a client's yank fills the
+ /// client's clipboard): each count moves once a request, and that
+ /// frontend acts when one moves; the session's host does it only for a
+ /// view no client draws (tape.hostCall). `yanks`: register `+` to its
+ /// clipboard; `pastes`: its clipboard read and sent back as a `paste`
+ /// (`clip_pending` says where it goes); `links`: `link` opened.
+ yanks: u32 = 0,
+ pastes: u32 = 0,
+ links: u32 = 0,
+ link: pardes.Effect.Buf(256) = .{},
};
gpa: std.mem.Allocator,
diff --git a/core/Effect.zig b/core/Effect.zig
index da6e1f7d..89a9b2e2 100644
--- a/core/Effect.zig
+++ b/core/Effect.zig
@@ -132,10 +132,14 @@ pub fn perform(p: *Pardes, e: Effect) void {
// step, would reach another pane's child.
.signal_pty => |s| if (if (p.panes[s.pane]) |pane| pane.serial != s.serial else true) {} else if (v.pty_signal) |f| f(p.host.ctx, s.pane, s.sig),
.close_pty => |c| if (v.close_pty) |f| f(p.host.ctx, c.pane),
- .open_link => |u| if (v.open_link) |f|
- f(p.host.ctx, u.slice())
- else
- p.fallback.setLink(u.slice()),
+ // The view's frontend opens it, yanks and pastes, on its own
+ // machine (View.yanks); the host here only for a view no client
+ // draws (tape.hostCall).
+ .open_link => |u| {
+ p.view().links +%= 1;
+ p.view().link = u;
+ if (v.open_link) |f| f(p.host.ctx, u.slice()) else p.fallback.setLink(u.slice());
+ },
// Plan 9's plumber's, detached (host_io.startDetached); in-process
// (a test), kept to look at.
.start => |st| {
@@ -195,15 +199,16 @@ pub fn perform(p: *Pardes, e: Effect) void {
}
},
.set_clipboard => {
+ p.view().yanks +%= 1;
const text = p.registers.text(p.gpa, '+') orelse "";
if (v.set_clipboard) |f| f(p.host.ctx, text) else p.fallback.setClipboard(text);
},
// No desktop to ask: answer from the in-process clipboard at once,
// which is the same shape as a host answering later.
- .read_clipboard => if (v.read_clipboard) |f|
- f(p.host.ctx)
- else
- p.update(.{ .paste = p.fallback.clipboard.items }),
+ .read_clipboard => {
+ p.view().pastes +%= 1;
+ if (v.read_clipboard) |f| f(p.host.ctx) else p.update(.{ .paste = p.fallback.clipboard.items });
+ },
.lsp => |q| if (v.lsp) |f|
f(p.host.ctx, .{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() })
else
diff --git a/core/replica.zig b/core/replica.zig
index 8b63ed51..dc5d99a4 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -5024,3 +5024,142 @@ test "a far client's own Del of a pane whose kind's state is not plain, rolled b
try agrees(a, r);
try std.testing.expect(r.core().panes[id] == null);
}
+
+/// A session's host whose fire-and-forget entries a test counts: a
+/// clipboard set, a paste asked, a link opened; with `tell`, a clipboard
+/// set says so into the core during the call, as a host's `saved` does.
+/// A client joined from the session's dump in its view, the unit after the
+/// join taken (the session's host said to it, as at any join's first), at
+/// the start of a second (`freshSecond`).
+fn joinedFresh(a: *Authority) !*Replica {
+ _ = try a.unit(0, 0);
+ const r = try joinedNow(a, 1, 0);
+ errdefer r.deinit();
+ r.confirm(try a.local(.tick));
+ r.settle();
+ freshSecond();
+ return r;
+}
+
+const SeenHost = struct {
+ core: ?*Pardes = null,
+ yanked: u32 = 0,
+ asked: u32 = 0,
+ linked: u32 = 0,
+ tell: bool = false,
+ fn of(ctx: ?*anyopaque) *SeenHost {
+ return @ptrCast(@alignCast(ctx.?));
+ }
+ fn setClipboard(ctx: ?*anyopaque, _: []const u8) void {
+ const h = of(ctx);
+ h.yanked += 1;
+ if (h.tell) h.core.?.setMessage(h.core.?.view().active, "yanked");
+ }
+ fn readClipboard(ctx: ?*anyopaque) void {
+ of(ctx).asked += 1;
+ }
+ fn openLink(ctx: ?*anyopaque, _: []const u8) void {
+ of(ctx).linked += 1;
+ }
+ fn watchTheme(_: ?*anyopaque, _: u32, _: bool) void {}
+ const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .set_clipboard = setClipboard, .read_clipboard = readClipboard, .open_link = openLink, .watch_theme = watchTheme };
+ fn host(h: *SeenHost) pardes.Host {
+ return .{ .ctx = h, .vtable = &vtable };
+ }
+};
+
+test "a client's step that asks the session's host for something that returns nothing goes on at once, and the session's unit confirms it: nothing rolled back" {
+ const gpa = std.testing.allocator;
+ var h: SeenHost = .{};
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host(), false);
+ defer a.deinit();
+ h.core = a.core;
+ const r = try joinedFresh(a);
+ defer r.deinit();
+ const msg = try gpa.dupe(u8, try r.input(.{ .command = "ClipYank" }));
+ defer gpa.free(msg);
+ try std.testing.expectEqual(@as(usize, 1), r.applied);
+ try std.testing.expectEqual(@as(u32, 0), r.stats.stopped);
+ r.confirm(try a.take(msg));
+ r.settle();
+ try std.testing.expectEqual(@as(u32, 1), h.yanked);
+ try std.testing.expectEqual(@as(u32, 1), r.stats.fast);
+ try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted);
+ try agrees(a, r);
+}
+
+test "a client's step whose session's host says something into the core during the call it made: its speculation rolled back, then the session's state" {
+ const gpa = std.testing.allocator;
+ var h: SeenHost = .{ .tell = true };
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host(), false);
+ defer a.deinit();
+ h.core = a.core;
+ const r = try joinedFresh(a);
+ defer r.deinit();
+ const msg = try gpa.dupe(u8, try r.input(.{ .command = "ClipYank" }));
+ defer gpa.free(msg);
+ try std.testing.expectEqual(@as(u32, 0), r.stats.stopped);
+ r.confirm(try a.take(msg));
+ r.settle();
+ try std.testing.expectEqual(@as(u32, 1), r.stats.mispredicted);
+ try agrees(a, r);
+}
+
+test "a client's ThemeFile waits for the session, whose host loads the file into the core; a Theme after it lets the file go at once" {
+ const gpa = std.testing.allocator;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "t.zon", .data = ".{ .name = \"seen\" }\n" });
+ var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
+ var line: [std.Io.Dir.max_path_bytes + 16]u8 = undefined;
+ var h: SeenHost = .{};
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host(), false);
+ defer a.deinit();
+ h.core = a.core;
+ const r = try joinedFresh(a);
+ defer r.deinit();
+ for ([_][]const u8{ try std.fmt.bufPrint(&line, "ThemeFile {s}/t.zon", .{dir}), "Theme acme" }, [_]u32{ 1, 0 }) |cmd, stops| {
+ const before = r.stats.stopped;
+ const msg = try gpa.dupe(u8, try r.input(.{ .command = cmd }));
+ defer gpa.free(msg);
+ try std.testing.expectEqual(stops, r.stats.stopped - before);
+ r.confirm(try a.take(msg));
+ r.settle();
+ try agrees(a, r);
+ }
+ try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted);
+}
+
+test "a client's yank and paste of the system clipboard are its own frontend's: its view counts them, at once and as the session does, and the session's host is asked neither" {
+ const gpa = std.testing.allocator;
+ var h: SeenHost = .{};
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host(), false);
+ defer a.deinit();
+ h.core = a.core;
+ _ = try a.unit(0, 0);
+ const j = (try a.join(1, null)) orelse return error.NoView;
+ defer gpa.free(j.dump);
+ const r = try Replica.initDump(gpa, 1, j.view, j.dump, .{ .cols = 80, .rows = 24 });
+ defer r.deinit();
+ r.confirm(try a.local(.tick));
+ r.settle();
+ freshSecond();
+ for ([_][]const u8{ "ClipYank", "ClipPaste" }) |cmd| {
+ const msg = try gpa.dupe(u8, try r.input(.{ .command = cmd }));
+ defer gpa.free(msg);
+ r.confirm(try a.take(msg));
+ r.settle();
+ }
+ try std.testing.expectEqual(@as(u32, 0), r.stats.stopped);
+ try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted);
+ try std.testing.expectEqual(@as(u32, 0), h.yanked + h.asked);
+ for ([_]*Pardes{ a.core, r.core() }) |c| {
+ try std.testing.expectEqual(@as(u32, 1), c.views[j.view].?.yanks);
+ try std.testing.expectEqual(@as(u32, 1), c.views[j.view].?.pastes);
+ }
+ try agrees(a, r);
+ // The session's own view's yank is its host's, as before.
+ _ = try a.own(.{ .command = "ClipYank" });
+ try std.testing.expectEqual(@as(u32, 1), h.yanked);
+}
diff --git a/core/tape.zig b/core/tape.zig
index 07bf4fff..0f0678d0 100644
--- a/core/tape.zig
+++ b/core/tape.zig
@@ -878,6 +878,55 @@ fn Wrap(comptime i: usize) type {
};
}
+/// The Host entries that return nothing and still stop a speculation, each
+/// with why; a speculation records any other as made and goes on (`hostCall`).
+/// The entries that answer stop by nature: the answer is the session's.
+const keeps_stopping = .{
+ // A shell's input: a device by the user's line (a far shell may feel
+ // like ssh), and off the tape with screen-state terminals.
+ "pty_write",
+ // Its host says into the core on every call that the shell started
+ // and where (host_io.forkShell: acknowledgeShell, setCwd), and gives
+ // the turn away: a rollback every time, nothing gained.
+ "spawn",
+ // Its host writes the core outside any record (exec.reapplyInitFile,
+ // Plumb.saved, fs.writeSelf) besides `saved` or the failure, every call.
+ "write_file",
+ // Their hosts say done or failed into the core on every call
+ // (setLastDump, dumpFailed; setMessage, reportError): a rollback every
+ // time, nothing gained.
+ "write_dump",
+ "dump_themes",
+ // The session's own: it execs itself after the step, carries a window
+ // away, or lets its frontend go (writing `frontend` and `unattended`).
+ "upgrade",
+ "hand_over",
+ "detach",
+ // A host with no worker runs the search on its loop and writes the
+ // rows outside any record (Stream.runHere, from tui's and gui's
+ // `search`); whether the session has one is not the client's to know.
+ "search",
+ // The loop's, never a step's.
+ "wait_input",
+ "present",
+ "post_present",
+ "poll_frame",
+};
+
+fn keepsStopping(comptime name: []const u8, args: anytype) bool {
+ // Turned on, its host loads the theme file into the core outside any
+ // record (colors.loadThemeFile: custom_theme, theme_file_loaded);
+ // turned off it only lets the file go.
+ if (comptime std.mem.eql(u8, name, "watch_theme")) return args[1];
+ inline for (keeps_stopping) |s| if (comptime std.mem.eql(u8, s, name)) return true;
+ return false;
+}
+
+/// The entries a view's frontend makes on its own machine (P16).
+fn forTheView(comptime name: []const u8) bool {
+ return std.mem.eql(u8, name, "set_clipboard") or std.mem.eql(u8, name, "read_clipboard") or std.mem.eql(u8, name, "open_link");
+}
+
fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype) R {
const t: *Tape = @ptrCast(@alignCast(ctx.?));
const name = host_fns[i];
@@ -913,10 +962,27 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype
t.record(.ret, R, &r);
return r;
}
+ // Nothing to wait for: recorded as made, and the speculation goes
+ // on. The session's unit records the same, or more where its host
+ // wrote the core during the call (an input it made, a watched field
+ // set), and the client rolls back.
+ if (R == void and !keepsStopping(name, args)) {
+ t.record(.host, u16, &id);
+ t.record(.ret, void, &{});
+ return;
+ }
t.poisoned = true;
t.poisoned_by = name;
return poisonOf(R);
}
+ // A client's yank, paste or link is its own frontend's, made on its own
+ // machine from its view's record (Pardes.View.yanks): never the
+ // session's, whose host makes them for a view no client draws.
+ if (comptime forTheView(name)) if (t.core) |p| if (p.undrawn & (@as(u8, 1) << @intCast(p.live)) != 0) {
+ t.record(.host, u16, &id);
+ t.record(.ret, void, &{});
+ return;
+ };
const real = @field(t.real.vtable.*, name).?;
t.record(.host, u16, &id);
t.seeFields();