diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 00:30:35 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 01:12:53 -0300 |
| commit | 2bbcc0a9aca11a418b8e51fd5d3158f2bc632d4b (patch) | |
| tree | beff90286c8cbccab11bcf3037037a75a0aca48a /core/tape.zig | |
| parent | 3a3601f984d25f1e03cee3ac47da7eeb2548e511 (diff) | |
| download | pardes-2bbcc0a9aca11a418b8e51fd5d3158f2bc632d4b.tar.gz pardes-2bbcc0a9aca11a418b8e51fd5d3158f2bc632d4b.zip | |
tape: the calls out to the host by the Host table's own names, step 6a (the design of reports/pardes-handle-69-design.md, P33): Host gains what a call names, gives and gets, derived from its table (Fn, Args, Ret, has, mask, call), the tape one method, p.tape.host(p, .tty_taken, .{.at(id)}), whose absent arm is the caller's after orelse, and the 7 calls made straight on the table (hostForegroundName, hostTtyTaken, hostKillJob, Adopt, Upgrade, clockNow, askAhead) go through it; hostCall is the one body (the wrappers forward to it until 6c deletes them), its text in place; the tape's bytes do not move (semantics 32640d4a722b811c unmoved, cross_replay both ways)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/tape.zig')
| -rw-r--r-- | core/tape.zig | 66 |
1 files changed, 39 insertions, 27 deletions
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 |
