diff options
| -rw-r--r-- | core/Core.zig | 9 | ||||
| -rw-r--r-- | core/Stream.zig | 2 | ||||
| -rw-r--r-- | core/builtins.zig | 6 | ||||
| -rw-r--r-- | core/exec.zig | 2 | ||||
| -rw-r--r-- | core/tape.zig | 66 | ||||
| -rw-r--r-- | dev/Host.zig | 46 |
6 files changed, 92 insertions, 39 deletions
diff --git a/core/Core.zig b/core/Core.zig index 351c3626..db0c5794 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -1322,18 +1322,15 @@ fn nextQueued(p: *Pardes) ?Event { } pub fn hostForegroundName(p: *const Pardes, id: usize, buf: []u8) ?[]const u8 { - const f = p.host.vtable.fg_name orelse return null; - return f(p.host.ctx, .at(id), buf); + return p.tape.host(p, .fg_name, .{ .at(id), buf }) orelse null; } pub fn hostTtyTaken(p: *const Pardes, id: usize) bool { - const f = p.host.vtable.tty_taken orelse return false; - return f(p.host.ctx, .at(id)); + return p.tape.host(p, .tty_taken, .{.at(id)}) orelse false; } pub fn hostKillJob(p: *const Pardes, id: usize) bool { - const f = p.host.vtable.kill_job orelse return false; - return f(p.host.ctx, .at(id)); + return p.tape.host(p, .kill_job, .{.at(id)}) orelse false; } /// A terminal whose shell could not be started: said, and a run waiting diff --git a/core/Stream.zig b/core/Stream.zig index 5960d249..dae61a8b 100644 --- a/core/Stream.zig +++ b/core/Stream.zig @@ -343,7 +343,7 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont pub fn askAhead(p: *Pardes) void { const gen = p.stream.ahead_ask orelse return; p.stream.ahead_ask = null; - if (p.host.vtable.search) |f| f(p.host.ctx, gen); + _ = p.tape.host(p, .search, .{gen}); } /// A file's colours for the query rows that show its lines, the whole diff --git a/core/builtins.zig b/core/builtins.zig index a7340352..7122cd02 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -547,8 +547,7 @@ pub const Adopt = struct { pub const scope: Scope = .session; pub const enabled = pardes.can_attach; pub fn run(c: Ctx) void { - const adopt = c.p.host.vtable.adopt orelse return c.p.reportFailure(c.id, "Adopt: no pardes daemon is keeping shells for this session"); - adopt(c.p.host.ctx, .at(c.id)); + c.p.tape.host(c.p, .adopt, .{.at(c.id)}) orelse c.p.reportFailure(c.id, "Adopt: no pardes daemon is keeping shells for this session"); } }; @@ -559,8 +558,7 @@ pub const Upgrade = struct { pub const scope: Scope = .session; pub const enabled = pardes.can_attach; pub fn run(c: Ctx) void { - const upgrade = c.p.host.vtable.upgrade orelse return c.p.reportFailure(c.id, "Upgrade: this pardes cannot move to another build in place"); - upgrade(c.p.host.ctx, .at(c.id)); + c.p.tape.host(c.p, .upgrade, .{.at(c.id)}) orelse c.p.reportFailure(c.id, "Upgrade: this pardes cannot move to another build in place"); } }; diff --git a/core/exec.zig b/core/exec.zig index 1ec47eb3..197827bb 100644 --- a/core/exec.zig +++ b/core/exec.zig @@ -535,7 +535,7 @@ const repl_quiet_ns: u64 = 100 * std.time.ns_per_ms; /// The host's clock now; core time (`now_ns`, `stepped_ns`) is as old as /// the last frame, seconds in an idle session. pub fn clockNow(p: *Pardes) u64 { - return if (p.host.vtable.now) |clock| clock(p.host.ctx) else p.clock.now_ns; + return p.tape.host(p, .now, .{}) orelse p.clock.now_ns; } /// Sends the Enters a paste into pane `id`'s REPL is owed: when the REPL diff --git a/core/tape.zig b/core/tape.zig index 61bdcc08..b74915e5 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -25,7 +25,8 @@ const pardes = @import("root.zig"); const walk = @import("walk.zig"); const host_io = @import("dev").host_io; const Pardes = pardes.Pardes; -const VTable = pardes.Host.VTable; +const Host = pardes.Host; +const VTable = Host.VTable; const libc = std.c; pub var on: ?*Tape = null; @@ -178,7 +179,7 @@ fn ErrorOf(comptime f: anytype) type { pub const shape = blk: { var types: []const type = &.{ Input, Site, []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 (host_fns) |f| types = types ++ .{Host.Ret(@field(Host.Fn, f))}; for (watched) |name| types = types ++ .{Watched(name)}; break :blk walk.shape(types); }; @@ -388,10 +389,7 @@ pub const Tape = struct { return; } t.real = p.host; - var mask: u64 = 0; - inline for (host_fns, 0..) |f, i| if (@field(t.real.vtable.*, f) != null) { - mask |= @as(u64, 1) << i; - }; + const mask = p.host.mask(); p.host = t.hostOf(mask); const set: struct { u8, u64 } = .{ set_mask, mask }; t.record(.set, @TypeOf(set), &set); @@ -641,8 +639,8 @@ pub const Tape = struct { fn applySet(t: *Tape, r: *walk.Reader) void { const which = (r.take(1) catch return)[0]; if (which == set_mask) { - const host = t.hostOf(std.mem.bytesToValue(u64, r.take(8) catch return)); - if (t.core) |p| p.host = host; + const h = t.hostOf(std.mem.bytesToValue(u64, r.take(8) catch return)); + if (t.core) |p| p.host = h; return; } const p = t.core orelse return; @@ -835,6 +833,19 @@ pub const Tape = struct { return r; } + /// One call out to the session's host, `f(args)`: made and recorded + /// with what it answered; answered from the tape on a replay; refused + /// to a speculation, or recorded as made (`keeps_stopping`). Null: the + /// session's host has no such function. + pub inline fn host(t: *Tape, p: *const Pardes, comptime f: Host.Fn, args: Host.Args(f)) ?Host.Ret(f) { + // Nobody records: the call it always was. (Until the wrappers go, + // a core's host is this tape's table while it records or replays, + // and any other is called as it is.) + if (t.through or p.host.vtable != &t.vtable) return p.host.call(f, args); + if (!p.host.has(f)) return null; + return hostCall(t, f, args); + } + /// Asked by `turn.yield`: a syscall a replica must never make. pub fn replaying(t: *const Tape) bool { return t.replay or t.speculating; @@ -1123,34 +1134,34 @@ fn tagOf(comptime entry: anytype, comptime Made: type) [:0]const u8 { /// The Host function `i`, through the tape. fn Wrap(comptime i: usize) type { - const Fn = @typeInfo(@typeInfo(@FieldType(VTable, host_fns[i])).optional.child).pointer.child; - const info = @typeInfo(Fn).@"fn"; - const R = info.return_type.?; - const P = info.param_types; - return switch (P.len) { + const h = @field(Host.Fn, host_fns[i]); + const R = Host.Ret(h); + const P = @typeInfo(@typeInfo(@FieldType(VTable, host_fns[i])).optional.child).pointer.child; + const A = @typeInfo(P).@"fn".param_types; + return switch (A.len) { 1 => struct { fn f(ctx: ?*anyopaque) R { - return hostCall(i, R, ctx, .{}); + return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{}); } }, 2 => struct { - fn f(ctx: ?*anyopaque, a: P[1].?) R { - return hostCall(i, R, ctx, .{a}); + fn f(ctx: ?*anyopaque, a: A[1].?) R { + return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{a}); } }, 3 => struct { - fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?) R { - return hostCall(i, R, ctx, .{ a, b }); + fn f(ctx: ?*anyopaque, a: A[1].?, b: A[2].?) R { + return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{ a, b }); } }, 4 => struct { - fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?, c: P[3].?) R { - return hostCall(i, R, ctx, .{ a, b, c }); + fn f(ctx: ?*anyopaque, a: A[1].?, b: A[2].?, c: A[3].?) R { + return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{ a, b, c }); } }, 5 => struct { - fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?, c: P[3].?, d: P[4].?) R { - return hostCall(i, R, ctx, .{ a, b, c, d }); + fn f(ctx: ?*anyopaque, a: A[1].?, b: A[2].?, c: A[3].?, d: A[4].?) R { + return hostCall(@ptrCast(@alignCast(ctx.?)), h, .{ a, b, c, d }); } }, else => @compileError("tape: a host function of more arguments"), @@ -1202,9 +1213,10 @@ 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]; +/// The one body of a call out to the host through the tape (`Tape.host`). +fn hostCall(t: *Tape, comptime f: Host.Fn, args: Host.Args(f)) Host.Ret(f) { + const R = Host.Ret(f); + const name = @tagName(f); // A terminal's answers and what is typed at it from its output are the // authority's to write, never a replica's: off the tape on both sides. if (comptime std.mem.eql(u8, name, "pty_write")) if (t.screen_state and !t.speculating) { @@ -1214,7 +1226,7 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype if (t.replay) { // A replica that never draws is never watched: it asks no one. if (comptime std.mem.eql(u8, name, "watched")) if (t.skips_render) return false; - if (!t.expectHost(i, name)) return std.mem.zeroes(R); + if (!t.expectHost(@backingInt(f), name)) return std.mem.zeroes(R); var ret = t.last_ret; if (R == ?[]const u8) { // fg_name's answer is written into the caller's buffer. @@ -1226,7 +1238,7 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype } return walk.decode(R, &ret, t.arena.allocator()) catch std.mem.zeroes(R); } - const id: u16 = i; + const id: u16 = @backingInt(f); if (t.speculating) { // Nobody watches a replica's step: its frames are its own, drawn // outside it (and the authority's host says so too, or the diff --git a/dev/Host.zig b/dev/Host.zig index a23c9ef5..cb7aefe1 100644 --- a/dev/Host.zig +++ b/dev/Host.zig @@ -1,6 +1,8 @@ +const std = @import("std"); const pardes = @import("core"); const host_io = @import("host_io.zig"); const Lsp = host_io.Lsp; +const Host = @This(); ctx: ?*anyopaque = null, vtable: *const VTable = &.{}, @@ -64,3 +66,47 @@ pub const VTable = struct { /// absent, it runs on the loop. job: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, serial: u32, token: u32) void = null, }; + +// What a call names, gives and gets, derived from the table: the core calls +// out through its tape, `p.tape.host(p, .pty_write, .{ pane, bytes })`. + +/// One of the table's functions, by its field's name: `.pty_write`. The +/// tape numbers a call by it (the table's order). +pub const Fn = std.meta.FieldEnum(VTable); + +/// What `f` is given after `ctx`, in order. +pub fn Args(comptime f: Fn) type { + const params = @typeInfo(Of(f)).@"fn".param_types; + var types: [params.len - 1]type = undefined; + for (params[1..], &types) |P, *T| T.* = P.?; + return @Tuple(&types); +} + +/// What `f` answers. +pub fn Ret(comptime f: Fn) type { + return @typeInfo(Of(f)).@"fn".return_type.?; +} + +fn Of(comptime f: Fn) type { + return @typeInfo(@typeInfo(@FieldType(VTable, @tagName(f))).optional.child).pointer.child; +} + +/// Whether this host has `f`. +pub fn has(h: Host, comptime f: Fn) bool { + return @field(h.vtable.*, @tagName(f)) != null; +} + +/// Which functions it has, a bit each in the table's order. +pub fn mask(h: Host) u64 { + var m: u64 = 0; + inline for (@typeInfo(VTable).@"struct".field_names, 0..) |name, i| { + if (@field(h.vtable.*, name) != null) m |= @as(u64, 1) << i; + } + return m; +} + +/// `f(args)` of this host; null: it has no such function. +pub inline fn call(h: Host, comptime f: Fn, args: Args(f)) ?Ret(f) { + const func = @field(h.vtable.*, @tagName(f)) orelse return null; + return @call(.auto, func, .{h.ctx} ++ args); +} |
