summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Core.zig33
-rw-r--r--core/Effect.zig62
-rw-r--r--core/tape.zig67
3 files changed, 112 insertions, 50 deletions
diff --git a/core/Core.zig b/core/Core.zig
index db0c5794..3fc5b192 100644
--- a/core/Core.zig
+++ b/core/Core.zig
@@ -1466,25 +1466,22 @@ pub fn animationActive(p: *const Pardes) bool {
}
pub fn pump(p: *Pardes, host: Host) !void {
- // By hand, not `taped`: the host is set before the enter, which wraps it, and is no input.
+ // By hand, not `taped`: the host is set before the enter, which says it if it is another, and is no input.
p.host = host;
const taped = pardes.tape.enter(p, .pump);
defer pardes.tape.leave(p, taped);
- // The host as the tape sees it, while it records.
- const h = p.host;
- const v = h.vtable;
- if (v.wait_input) |f| {
+ if (p.host.has(.wait_input)) {
// Asleep until input or until the core's next deadline
// (nextWake); 0 is no timeout at all.
// The clock read whether or not it is wanted: what a replica
// waits on is its own (a picture decoding wakes only a frame
// that draws it), and the tape has the same calls either way.
- const now = if (v.now) |clock| clock(h.ctx) else p.clock.now_ns;
+ const now = p.tape.host(p, .now, .{}) orelse p.clock.now_ns;
const timeout: u32 = if (p.nextWake()) |due| wake: {
const ms = ((due -| now) + std.time.ns_per_ms - 1) / std.time.ns_per_ms;
break :wake @intCast(std.math.clamp(ms, 1, std.math.maxInt(u32)));
} else 0;
- f(h.ctx, timeout);
+ _ = p.tape.host(p, .wait_input, .{timeout});
}
// Detached, nobody's key dismisses a message: they go on the clock,
// here in the step, where the clock read is taped (the host's wait
@@ -1492,46 +1489,54 @@ pub fn pump(p: *Pardes, host: Host) !void {
// go).
Messages.expireUnattended(p);
p.frames.mark_start = FrameStats.nowNs();
- if (v.now) |clock| p.advance(clock(h.ctx));
+ if (p.tape.host(p, .now, .{})) |now| p.advance(now);
p.settle();
// A quitting frame has already freed what it would draw.
if (p.quit) return;
- if (v.poll_frame) |f| f(h.ctx);
+ _ = p.tape.host(p, .poll_frame, .{});
// Nothing to draw: do not spend a frame, and above all do not block on
// the display for one. This is what keeps the frame rate governing
// rendering and nothing else.
// A shell with a clock draws what `advance` stepped; one without
// steps its own `.tick`s and draws while a deadline is due.
- if (!p.needs_frame and (v.now != null or !p.animationActive())) return p.frames.idle();
+ if (!p.needs_frame and (p.host.has(.now) or !p.animationActive())) return p.frames.idle();
// No one to see it: the frame stays owed (needs_frame), not drawn.
// A pty streaming into an unwatched session spent most of its time
// drawing what nobody looked at.
- if (v.watched) |f| if (!f(h.ctx)) return;
+ if (!(p.tape.host(p, .watched, .{}) orelse true)) return;
// The one view this process shows (a replica client's is its own
// process's to draw), drawn as it is if another's is live.
// Not shown by the frontend (Host.present): owed.
var skipped = false;
const shown = Views.shown(p);
if (shown != p.live) {
- if (shown) |k| skipped = !try Views.drawOne(p, k, .{ .ctx = h.ctx, .f = v.present });
+ if (shown) |k| skipped = !try Views.drawOne(p, k, .{ .ctx = p, .f = presentTaped });
} else {
_ = p.frame_arena.reset(.{ .retain_with_limit = memory.limits.frame_kept });
p.frames.mark_update = FrameStats.nowNs();
p.frames.view = if (p.panes[p.view().active]) |pane| pane.scroll() else 0;
const surface = try p.render(p.frame_arena.allocator());
p.frames.mark_render = FrameStats.nowNs();
- if (v.present) |f| skipped = !f(h.ctx, surface);
+ skipped = !(p.tape.host(p, .present, .{surface}) orelse true);
Stream.askAhead(p);
}
// Settled before post_present: a shell ticks there, and what that
// tick moves owes the next frame.
p.needs_frame = skipped;
- if (v.post_present) |f| f(h.ctx);
+ _ = p.tape.host(p, .post_present, .{});
// Still moving: the next frame is owed a period from now.
const owed = if (p.nextWake()) |due| due <= p.clock.stepped_ns + frame_ns else false;
p.frames.record(FrameStats.nowNs(), owed, skipped);
}
+/// The pump's other view presented as its own is, through the tape:
+/// `present` stays inside `views.within`, whose live view the host's
+/// `presented` reads.
+fn presentTaped(ctx: ?*anyopaque, surface: *const pardes.Surface) bool {
+ const p: *Pardes = @ptrCast(@alignCast(ctx.?));
+ return p.tape.host(p, .present, .{surface}) orelse true;
+}
+
/// What a pump does between its wait and its frame: the queued inputs
/// and the effects. Also the session at a step boundary, taken now (an
/// Upgrade's dump, upgrade.zig).
diff --git a/core/Effect.zig b/core/Effect.zig
index 5e821065..a6fcc0e5 100644
--- a/core/Effect.zig
+++ b/core/Effect.zig
@@ -116,34 +116,33 @@ pub const perform = tape.taped(performInner);
fn performInner(p: *Pardes, e: Effect) void {
// A Save performed on one twin is every twin's (File.syncTwins).
defer panes.File.syncTwins(p);
- const v = p.host.vtable;
switch (e) {
// The host reads the pane (its command, its shell) as it forks,
// so the slot must still hold the pane the spawn was for.
- .spawn => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else if (v.spawn) |f| f(p.host.ctx, s.pane, if (s.cwd.len == 0) (p.spawn_dirs[s.pane.index()] orelse "") else s.cwd.slice()) else {
+ .spawn => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else p.tape.host(p, .spawn, .{ s.pane, if (s.cwd.len == 0) (p.spawn_dirs[s.pane.index()] orelse "") else s.cwd.slice() }) orelse {
p.fallback.spawned[s.pane.index()] = true;
},
// A pane with no child is silent: nothing invents output on its
// screen, and the bytes are dropped rather than transcribed.
- .write => |w| if (v.pty_write) |f| f(p.host.ctx, w.pane, w.bytes.slice()),
- .resize_pty => |r| if (v.pty_resize) |f| f(p.host.ctx, r.pane, r.cols, r.rows),
+ .write => |w| _ = p.tape.host(p, .pty_write, .{ w.pane, w.bytes.slice() }),
+ .resize_pty => |r| _ = p.tape.host(p, .pty_resize, .{ r.pane, r.cols, r.rows }),
// A signal for a pane that closed, its slot retaken in the same
// step, would reach another pane's child.
- .signal_pty => |s| if (if (p.panes[s.pane.index()]) |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),
+ .signal_pty => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else p.tape.host(p, .pty_signal, .{ s.pane, s.sig }) orelse {},
+ .close_pty => |c| _ = p.tape.host(p, .close_pty, .{c.pane}),
// 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());
+ p.tape.host(p, .open_link, .{u.slice()}) orelse p.fallback.setLink(u.slice());
},
// Plan 9's plumber's, detached (host_io.startDetached); in-process
// (a test), kept to look at.
.start => |st| {
var why_buf: [64]u8 = undefined;
- const why = if (v.start) |f| f(p.host.ctx, st.line.slice(), &why_buf) else p.fallback.start(st.line.slice());
+ const why = p.tape.host(p, .start, .{ st.line.slice(), &why_buf }) orelse p.fallback.start(st.line.slice());
const w = why orelse return;
var words = std.mem.splitScalar(u8, st.line.slice(), 0);
_ = words.next(); // its directory
@@ -191,37 +190,31 @@ fn performInner(p: *Pardes, e: Effect) void {
},
.write_dump => {
const out = p.dump_out orelse return;
- if (v.write_dump) |f| {
- f(p.host.ctx, out);
- } else {
+ p.tape.host(p, .write_dump, .{out}) orelse {
_ = p.fallback.writeFile(host_io.Fallback.dump_path, out);
p.setLastDump(host_io.Fallback.dump_path);
- }
+ };
},
.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);
+ p.tape.host(p, .set_clipboard, .{text}) orelse 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 => {
p.view().pastes +%= 1;
- if (v.read_clipboard) |f| f(p.host.ctx) else p.update(.{ .paste = p.fallback.clipboard.items });
+ p.tape.host(p, .read_clipboard, .{}) orelse 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
+ .lsp => |q| p.tape.host(p, .lsp, .{.{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }}) orelse
p.update(.{ .lsp_resp = .{ .id = q.id, .rows = "" } }),
- .pipe => |q| if (v.pipe) |f|
- f(p.host.ctx, q.id)
- else
+ .pipe => |q| p.tape.host(p, .pipe, .{q.id}) orelse
p.update(.{ .pipe_resp = .{ .id = q.id, .success = false, .outputs = &.{} } }),
// No workers here (a test, a board): it runs on the loop.
- .search => |q| if (v.search) |f| f(p.host.ctx, q.gen) else Stream.runHere(p, q.gen, false),
+ .search => |q| p.tape.host(p, .search, .{q.gen}) orelse Stream.runHere(p, q.gen, false),
// No windows here (a test, the tty): it stays.
- .hand_over => |h| if (v.hand_over) |f| {
- if (p.handover.bytes) |bytes| f(p.host.ctx, h.seq, bytes);
+ .hand_over => |h| if (p.host.has(.hand_over)) {
+ if (p.handover.bytes) |bytes| _ = p.tape.host(p, .hand_over, .{ h.seq, bytes });
} else p.update(.{ .handed = .{ .seq = h.seq, .failure = "no other windows here" } }),
.edit_write => {
// Each write takes the bytes `w` took when it ran: the text
@@ -247,24 +240,21 @@ fn performInner(p: *Pardes, e: Effect) void {
} else ctlfs.events.noteLogAs(p, .save, pane, w.path);
}
},
- .watch => |w| if (v.watch_file) |f| {
+ .watch => |w| {
// What a watch is about: a real file, or a kind's (a PDF's).
const path: []const u8 = if (p.panes[w.pane.index()]) |pane| (if (pane.file) |file| (if (file.output == null) file.path else "") else if (pane.addon) |k| k.path else "") else "";
- f(p.host.ctx, w.pane, path, w.on, w.mode);
- } else {
- p.fallback.watched[w.pane.index()] = w.on;
+ p.tape.host(p, .watch_file, .{ w.pane, path, w.on, w.mode }) orelse {
+ p.fallback.watched[w.pane.index()] = w.on;
+ };
},
- .theme_file => |t| if (v.watch_theme) |f| f(p.host.ctx, t.generation, t.on),
- .dump_themes => |d| if (v.dump_themes) |f| f(p.host.ctx, d.pane),
- .detach => |d| if (v.detach) |f|
- f(p.host.ctx)
- else
- p.reportError(d.pane, "detach", error.NotAttached),
+ .theme_file => |t| _ = p.tape.host(p, .watch_theme, .{ t.generation, t.on }),
+ .dump_themes => |d| _ = p.tape.host(p, .dump_themes, .{d.pane}),
+ .detach => |d| p.tape.host(p, .detach, .{}) orelse p.reportError(d.pane, "detach", error.NotAttached),
// dlopen is the same on every host, so none has a method of its
// own; a replica's core holds no library (what it brought comes in
// the event this taped).
- .addon => |a| if (!tape.replaying()) @import("Addon.zig").perform(p, a.op, a.pane.index(), a.arg.slice()),
- .job => |j| if (v.job) |f| f(p.host.ctx, j.pane, j.serial, j.token) else if (!tape.replaying()) host_io.KindJob.runHere(p, j.pane.index(), j.serial, j.token),
+ .addon => |a| if (!p.tape.replaying()) @import("Addon.zig").perform(p, a.op, a.pane.index(), a.arg.slice()),
+ .job => |j| p.tape.host(p, .job, .{ j.pane, j.serial, j.token }) orelse if (!p.tape.replaying()) host_io.KindJob.runHere(p, j.pane.index(), j.serial, j.token),
// the loop's own condition; a host tears down after its own loop
.quit => p.endSession(),
}
@@ -282,7 +272,7 @@ pub fn writeChecked(p: *Pardes, id: u8, serial: u32, path: []const u8, bytes: []
if (filesystem.localPath(path)) |local| filesystem.makeDirs(p.tape, std.Io.Dir.path.dirname(local) orelse "/");
// The in-process filesystem reports the same way a real host does,
// so an OOM there leaves the pane dirty rather than looking saved.
- if (p.host.vtable.write_file) |f| f(p.host.ctx, .at(id), path, bytes) else if (!p.fallback.writeFile(path, bytes)) p.saveFailed(.at(id), path, error.OutOfMemory);
+ p.tape.host(p, .write_file, .{ .at(id), path, bytes }) orelse if (!p.fallback.writeFile(path, bytes)) p.saveFailed(.at(id), path, error.OutOfMemory);
const pane = p.panes[id] orelse return null;
if (pane.serial != serial) return null;
if (p.fs.failures == failures) return pane;
diff --git a/core/tape.zig b/core/tape.zig
index b74915e5..18b5e296 100644
--- a/core/tape.zig
+++ b/core/tape.zig
@@ -1783,6 +1783,73 @@ test "every place that sets the global tape aside sets the core's field aside: a
try std.testing.expectEqual(@as(usize, 0), wrong);
}
+/// The pump's own calls: a host with a clock and a frontend that shows
+/// every frame; `more` has one function besides.
+const Pumped = struct {
+ ns: u64,
+ fn now(ctx: ?*anyopaque) u64 {
+ const h: *Pumped = @ptrCast(@alignCast(ctx.?));
+ h.ns += std.time.ns_per_ms;
+ return h.ns;
+ }
+ fn seen(_: ?*anyopaque) bool {
+ return true;
+ }
+ fn present(_: ?*anyopaque, _: *const pardes.Surface) bool {
+ return true;
+ }
+ fn poll(_: ?*anyopaque) void {}
+ const one: VTable = .{ .now = now, .watched = seen, .present = present };
+ const more: VTable = .{ .now = now, .watched = seen, .present = present, .poll_frame = poll };
+
+ /// Recorded `bytes`: how many times which functions the host has was
+ /// said, the last said, and the calls of each function.
+ fn said(bytes: []const u8) struct { sets: usize = 0, mask: u64 = 0, calls: [host_fns.len]usize = @splat(0) } {
+ var got: @typeInfo(@TypeOf(said)).@"fn".return_type.? = .{};
+ var r: walk.Reader = .{ .bytes = bytes };
+ while (nextRecord(&r)) |rec| {
+ if (rec.kind == .set and rec.payload[0] == set_mask) {
+ got.sets += 1;
+ got.mask = std.mem.bytesToValue(u64, rec.payload[1..9]);
+ }
+ if (rec.kind == .host) {
+ const id = std.mem.bytesToValue(u16, rec.payload[0..2]);
+ if (id < host_fns.len) got.calls[id] += 1;
+ }
+ }
+ return got;
+ }
+};
+
+test "a core pumped twice with one host and once with another: which functions its host has is said at the first pump and the third, its clock, watch and present at each; a replica replays the three units to the same digest" {
+ const gpa = std.testing.allocator;
+ const R = pardes.replica;
+ var first: Pumped = .{ .ns = 0 };
+ var second: Pumped = .{ .ns = 1 << 40 };
+ const a = try R.Authority.init(gpa, .{ .cols = 80, .rows = 24 }, .{ .ctx = &first, .vtable = &Pumped.one }, false);
+ defer a.deinit();
+ const boot = Pumped.said(a.t.buf.items);
+ try std.testing.expectEqual(Host.mask(.{ .vtable = &Pumped.one }), boot.mask);
+ const r = try R.Replica.init(gpa, 1, try a.unit(0, 0), false);
+ defer r.deinit();
+ const hosts = [_]Host{ .{ .ctx = &first, .vtable = &Pumped.one }, .{ .ctx = &second, .vtable = &Pumped.more } };
+ for (hosts, [_]usize{ 0, 1 }, 1..) |h, sets, seq| {
+ a.host = h;
+ a.apply(.{ .resize = .{ .cols = @intCast(80 + seq), .rows = 24 } });
+ const got = Pumped.said(a.t.buf.items);
+ try std.testing.expectEqual(sets, got.sets);
+ if (sets > 0) try std.testing.expectEqual(h.mask(), got.mask);
+ inline for (.{ .now, .watched, .present }) |f| try std.testing.expect(got.calls[@backingInt(@as(Host.Fn, f))] > 0);
+ try std.testing.expectEqual(@intFromBool(h.has(.poll_frame)), got.calls[@backingInt(Host.Fn.poll_frame)]);
+ a.seq += 1;
+ r.confirm(try a.unit(0, a.seq));
+ }
+ r.settle();
+ if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why});
+ try std.testing.expect(r.diverged == null);
+ try std.testing.expectEqual(R.digestOf(a), r.digest());
+}
+
test "a client's frame drawn between its steps records nothing into its idle tape" {
const gpa = std.testing.allocator;
const p = try @import("ninep/harness.zig").withFile(gpa, "x\n");