diff options
Diffstat (limited to 'core/tape.zig')
| -rw-r--r-- | core/tape.zig | 204 |
1 files changed, 125 insertions, 79 deletions
diff --git a/core/tape.zig b/core/tape.zig index dc80887c..bb55ce5a 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -35,55 +35,56 @@ pub var on: ?*Tape = null; pub var none: Tape = .{ .gpa = std.heap.page_allocator, .replay = false, .through = true, .scratch = .init(std.heap.page_allocator), .arena = .init(std.heap.page_allocator) }; /// Every call into the core a host makes (a 9P connection task's too). +/// An entry written once (`taped`) has the variant `Args(<the entry>)`, +/// its arguments after the core in order; the six written by hand say +/// what they carry, and each has its arm in `apply`. pub const Input = union(enum) { init: pardes.Options, init_from_dump: struct { opts: pardes.Options, bytes: []const u8 }, - restore: struct { bytes: []const u8, from: []const u8 }, + restore: Args(pardes.dump.restore), /// Whether it was the session's own core, or one a Restore replaced. deinit: bool, - update: pardes.Event, - serve_fs: pardes.ctlfs.Req, + update: Args(Pardes.update), + serve_fs: Args(Pardes.serveFs), pump, - step, - perform: pardes.Effect, - next_effect, - advance: u64, + step: Args(Pardes.step), + perform: Args(Pardes.perform), + next_effect: Args(Pardes.nextEffect), + advance: Args(Pardes.advance), render, - take_restore, - set_cwd: struct { id: usize, cwd: []const u8 }, - acknowledge_shell: struct { id: usize, executable: []const u8, prompt_marks: bool }, + take_restore: Args(Pardes.takeRestore), + set_cwd: Args(Pardes.setCwd), + acknowledge_shell: Args(Pardes.acknowledgeShell), /// A dead session's shell taken in (Adopt), and a terminal's reader /// that failed: what a host writes into the core, made inputs. - adopt_shell, - reader_failed: struct { id: u8, err: anyerror }, - shell_failed: struct { id: u8, err: anyerror }, - restart_failed: struct { id: u8, err: anyerror }, - save_failed: struct { id: u8, path: []const u8, err: anyerror }, - dump_failed: struct { path: []const u8, err: anyerror }, - set_last_dump: []const u8, - set_message: struct { id: usize, text: []const u8 }, - set_status: struct { id: usize, text: []const u8 }, - report_error: struct { id: usize, operation: []const u8, err: anyerror }, - reload_watched_file: struct { id: u8, bytes: []const u8 }, - note_late_failure: usize, - invalidate, - presented, - run_closed_lines, - file_deleted: u8, - open_view, - close_view: u8, - leave_view: u8, - away_view: u8, - rejoin_view: u8, + adopt_shell: Args(Pardes.adoptShell), + reader_failed: Args(Pardes.readerFailed), + shell_failed: Args(Pardes.shellFailed), + restart_failed: Args(Pardes.restartFailed), + save_failed: Args(Pardes.saveFailed), + dump_failed: Args(Pardes.dumpFailed), + set_last_dump: Args(Pardes.setLastDump), + set_message: Args(Pardes.setMessage), + set_status: Args(Pardes.setStatus), + report_error: Args(Pardes.reportError), + reload_watched_file: Args(Pardes.reloadWatchedFile), + note_late_failure: Args(Pardes.noteLateFailure), + invalidate: Args(Pardes.invalidate), + presented: Args(Pardes.presented), + run_closed_lines: Args(pardes.ctlfs.runClosedLines), + file_deleted: Args(pardes.panes.File.deleted), + open_view: Args(Pardes.openView), + close_view: Args(Pardes.closeView), + leave_view: Args(Pardes.leaveView), + away_view: Args(Pardes.awayView), + rejoin_view: Args(Pardes.rejoinView), to_view: u8, /// What a window says of its font and its opacity (gui.zig). - take_font_request, - observe_font: Font, - acknowledge_font: Font, - reject_font, - window_opacity_done: struct { applied: u8, failure: ?[]const u8 }, - - pub const Font = struct { name: []const u8, size: u16, unit: pardes.config.Runtime.FontSizeUnit }; + take_font_request: Args(Pardes.takeFontRequest), + observe_font: Args(Pardes.observeFont), + acknowledge_font: Args(Pardes.acknowledgeFont), + reject_font: Args(Pardes.rejectFont), + window_opacity_done: Args(Pardes.windowOpacityDone), }; /// The core's own syscalls (each `turn.yield()`s around itself), numbered @@ -419,9 +420,9 @@ pub const Tape = struct { t.wrap(core); } // A terminal's output crosses as screen state, not as its bytes. - if (t.screen_state and input.* == .update and input.update == .output) { + if (t.screen_state and input.* == .update and input.update.args[0] == .output) { var bare = input.*; - bare.update.output.bytes = &.{}; + bare.update.args[0].output.bytes = &.{}; t.record(.input, Input, &bare); } else t.record(.input, Input, input); return .{ .outer = true }; @@ -659,7 +660,7 @@ pub const Tape = struct { const input = walk.decode(Input, r, if (kept) keep else t.arena.allocator()) catch return t.diverge("a short input", .{}); const outer = t.applying; t.applying = switch (input) { - .update => |ev| @tagName(ev), + .update => |a| @tagName(a.args[0]), else => @tagName(input), }; defer t.applying = outer; @@ -690,8 +691,8 @@ pub const Tape = struct { const p = t.core orelse return t.diverge("an input before the core", .{}); switch (input) { .init, .init_from_dump => unreachable, - .restore => |rs| { - const nc = pardes.dump.restore(p, rs.bytes, rs.from) catch |err| return t.diverge("restore failed: {t}", .{err}); + .restore => |a| { + const nc = pardes.dump.restore(p, a.args[0], a.args[1]) catch |err| return t.diverge("restore failed: {t}", .{err}); t.core = nc; nc.tape = t; nc.host = t.hostOf(t.mask); @@ -705,47 +706,14 @@ pub const Tape = struct { } return t.skip(); }, - .update => |ev| p.update(ev), - .serve_fs => |req| _ = p.serveFs(req), .pump => p.pump(t.hostOf(t.mask)) catch {}, - .step => p.step(), - .perform => |e| p.perform(e), - .next_effect => _ = p.nextEffect(), - .advance => |now| p.advance(now), .render => if (t.skips_render) return t.skip() else { _ = p.frame_arena.reset(.retain_capacity); _ = p.render(p.frame_arena.allocator()) catch {}; }, - .take_restore => _ = p.takeRestore(), - .set_cwd => |a| p.setCwd(a.id, a.cwd), - .acknowledge_shell => |a| p.acknowledgeShell(a.id, a.executable, a.prompt_marks), - .adopt_shell => _ = p.adoptShell(), - .reader_failed => |a| p.readerFailed(a.id, a.err), - .shell_failed => |a| p.shellFailed(a.id, a.err), - .restart_failed => |a| p.restartFailed(a.id, a.err), - .save_failed => |a| p.saveFailed(a.id, a.path, a.err), - .dump_failed => |a| p.dumpFailed(a.path, a.err), - .set_last_dump => |path| p.setLastDump(path), - .set_message => |a| p.setMessage(a.id, a.text), - .set_status => |a| p.setStatus(a.id, a.text), - .report_error => |a| p.reportError(a.id, a.operation, a.err), - .reload_watched_file => |a| _ = p.reloadWatchedFile(a.id, a.bytes), - .note_late_failure => |id| p.noteLateFailure(id), - .invalidate => p.invalidate(), - .presented => p.presented(), - .run_closed_lines => pardes.ctlfs.runClosedLines(p), - .file_deleted => |id| pardes.panes.File.deleted(p, id), - .open_view => _ = p.openView(), - .close_view => |k| p.closeView(k), - .leave_view => |k| p.leaveView(k), - .away_view => |k| p.awayView(k), - .rejoin_view => |k| _ = p.rejoinView(k), .to_view => |k| p.toView(k), - .take_font_request => _ = p.takeFontRequest(), - .observe_font => |f| _ = p.observeFont(f.name, f.size, f.unit), - .acknowledge_font => |f| _ = p.acknowledgeFont(f.name, f.size, f.unit), - .reject_font => p.rejectFont(), - .window_opacity_done => |w| p.windowOpacityDone(w.applied, w.failure), + // Every entry written once: the one its variant names. + inline else => |a| _ = @call(.auto, @TypeOf(a).entry, .{p} ++ a.args), } t.finishInput(); } @@ -1065,6 +1033,84 @@ pub fn forget(p: *Pardes) void { (on orelse &none).forget(p); } +/// An entry written once (P22): its public name made from the private +/// body directly under it, `pub const setCwd = pardes.tape.taped(setCwdInner);`. +/// The same parameters; the call recorded as the variant of `Input` whose +/// line names the entry (`set_cwd: Args(Pardes.setCwd)`), which the entry +/// finds by itself; the body inlined, so the entry compiles to what one +/// opening with its own `enter` and deferred `leave` did. +pub fn taped(comptime inner: anytype) @TypeOf(inner) { + const T = Taped(inner); + return switch (@typeInfo(@TypeOf(inner)).@"fn".param_types.len) { + 1 => T.f1, + 2 => T.f2, + 3 => T.f3, + 4 => T.f4, + else => @compileError("tape: an entry of more arguments"), + }; +} + +/// One function for each number of parameters, of which only the one +/// `taped` picks is analysed; a profile names it `tape.Taped((function +/// '<inner>')).f<n>`. The variant is found in the body, not in the +/// declaration: there it would need `Input`, whose line needs the entry +/// (a dependency loop). +fn Taped(comptime inner: anytype) type { + const F = @typeInfo(@TypeOf(inner)).@"fn"; + const P = F.param_types; + const R = F.return_type.?; + return struct { + fn f1(p: P[0].?) R { + const entered = enter(p, @unionInit(Input, tagOf(f1, @This()), .{ .args = .{} })); + defer leave(p, entered); + return @call(.always_inline, inner, .{p}); + } + fn f2(p: P[0].?, a: P[1].?) R { + const entered = enter(p, @unionInit(Input, tagOf(f2, @This()), .{ .args = .{a} })); + defer leave(p, entered); + return @call(.always_inline, inner, .{ p, a }); + } + fn f3(p: P[0].?, a: P[1].?, b: P[2].?) R { + const entered = enter(p, @unionInit(Input, tagOf(f3, @This()), .{ .args = .{ a, b } })); + defer leave(p, entered); + return @call(.always_inline, inner, .{ p, a, b }); + } + fn f4(p: P[0].?, a: P[1].?, b: P[2].?, c: P[3].?) R { + const entered = enter(p, @unionInit(Input, tagOf(f4, @This()), .{ .args = .{ a, b, c } })); + defer leave(p, entered); + return @call(.always_inline, inner, .{ p, a, b, c }); + } + }; +} + +/// What an entry's variant carries: its arguments after the core, and +/// the entry itself, which makes the type the entry's own (two entries of +/// the same parameters are two types): the entry finds its variant by it +/// (`tagOf`) and the replay calls the entry from it (`apply`). +pub fn Args(comptime f: anytype) type { + const params = @typeInfo(@TypeOf(f)).@"fn".param_types; + var types: [params.len - 1]type = undefined; + for (params[1..], &types) |P, *T| T.* = P.?; + const Tuple = @Tuple(&types); + return struct { + args: Tuple, + pub const entry = f; + }; +} + +/// The variant whose line names `entry`, which `Made` (`Taped(inner)`) made. +fn tagOf(comptime entry: anytype, comptime Made: type) [:0]const u8 { + const u = @typeInfo(Input).@"union"; + var found: ?[:0]const u8 = null; + for (u.field_names, u.field_types) |name, T| { + if (@typeInfo(T) != .@"struct" or !@hasDecl(T, "entry")) continue; + if (@TypeOf(T.entry) != @TypeOf(entry) or T.entry != entry) continue; + if (found) |other| @compileError("tape: two variants of Input name the entry " ++ @typeName(Made) ++ ": " ++ other ++ " and " ++ name); + found = name; + } + return found orelse @compileError("tape: no variant of Input names the entry " ++ @typeName(Made) ++ " (a line `<tag>: Args(<the entry>)` in tape.Input)"); +} + /// 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; @@ -1501,7 +1547,7 @@ pub fn describe(bytes: []const u8, w: *std.Io.Writer) void { var room: [4096]u8 = undefined; var fixed: std.heap.FixedBufferAllocator = .init(&room); var in: walk.Reader = .{ .bytes = payload }; - if (tag == .update) if (walk.decode(Input, &in, fixed.allocator())) |input| switch (input.update) { + if (tag == .update) if (walk.decode(Input, &in, fixed.allocator())) |input| switch (input.update.args[0]) { .key => |k| w.print(" {any}", .{k}) catch {}, .mouse => |m| w.print(" {t} {t} {d},{d} body {any} tag {any}", .{ m.button, m.kind, m.col, m.row, m.body_hit != null, m.tag_hit != null }) catch {}, else => {}, |
