diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Addon.zig | 13 | ||||
| -rw-r--r-- | core/Core.zig | 9 | ||||
| -rw-r--r-- | core/Kind.zig | 60 | ||||
| -rw-r--r-- | core/dump.zig | 2 | ||||
| -rw-r--r-- | core/tape.zig | 4 |
5 files changed, 45 insertions, 43 deletions
diff --git a/core/Addon.zig b/core/Addon.zig index 5c23ed51..d52462e0 100644 --- a/core/Addon.zig +++ b/core/Addon.zig @@ -21,7 +21,6 @@ const std = @import("std"); const Kind = @import("Kind.zig"); const dump = @import("dump.zig"); const pardes = @import("root.zig"); -const tape = @import("tape.zig"); const Pardes = pardes.Pardes; const Why = Kind.Buf(160); @@ -342,7 +341,7 @@ fn listed(data: []const u8, path: []const u8, n: usize, why: *Why) Loaded { /// Kind `k`'s code, a session's (a replica's is `plainOps`'s or none): its /// library loaded at the first pane of its kind, from the file the start /// listed it from (`listed`); null, and why, when it will not load. -pub fn need(r: *Kind.Registry, k: u8, why: *Why, distrusted: ?*const Distrusted) ?*const Kind.Ops { +pub fn need(r: *Kind.Registry, tape: *pardes.tape.Tape, k: u8, why: *Why, distrusted: ?*const Distrusted) ?*const Kind.Ops { if (r.ops[k] != null) return r.ops[k]; // A client's: its own code for a plain kind, loaded at its first pane. if (tape.replaying()) { @@ -612,7 +611,7 @@ pub fn leader(r: *const Kind.Registry, keys: []const u8) union(enum) { none, pre /// `l` taken in, its words and extensions with it, or listed refused, /// and why: collisions with the core's names and the kinds here /// (`except`'s own looked past, a reload's old one). -pub fn take(r: *Kind.Registry, l: Loaded, except: []const u8) ?[]const u8 { +pub fn take(r: *Kind.Registry, tape: *pardes.tape.Tape, l: Loaded, except: []const u8) ?[]const u8 { if (r.n == Kind.max_kinds) return "refused: no room for another kind"; const e = &r.kinds[r.n]; e.* = .{ .version = l.version, .abi = l.abi, .hash = l.hash, .caps = l.caps }; @@ -775,7 +774,7 @@ pub fn landed(p: *Pardes, op: Op, pane: u8, l: Loaded) void { while (i < p.kinds.n) { if (p.kinds.kinds[i].refused and std.mem.eql(u8, p.kinds.kinds[i].path.slice(), l.path)) p.kinds.drop(i) else i += 1; } - if (p.kinds.take(l, "")) |why| { + if (p.kinds.take(p.tape, l, "")) |why| { if (l.why.len == 0) release(p, l.hash); return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon load: {s}: {s}", .{ base, why }) catch "Addon load: refused"); } @@ -812,7 +811,7 @@ fn swap(p: *Pardes, l: Loaded, why: *Why) bool { if (p.kinds.collides(l, name, &scratch)) |r| return fail(why, "{s}", .{r}); if (!std.mem.eql(u8, l.name, name)) return fail(why, "refused: its kind is now {s}, not {s}", .{ l.name, name }); const old_ops = p.kinds.ops[k]; - const new_ops: ?*const Kind.Ops = if (tape.replaying()) null else @ptrFromInt(l.ops); + const new_ops: ?*const Kind.Ops = if (p.tape.replaying()) null else @ptrFromInt(l.ops); const gen = p.kinds.kinds[k].gen +% 1; var fresh: [pardes.MAX_PANES]?*Kind.Slot = @splat(null); defer for (fresh) |f| if (f) |s| { @@ -857,7 +856,7 @@ fn swap(p: *Pardes, l: Loaded, why: *Why) bool { }; const old_hash = p.kinds.kinds[k].hash; p.kinds.drop(k); - _ = p.kinds.take(l, ""); + _ = p.kinds.take(p.tape, l, ""); p.kinds.kinds[p.kinds.n - 1].gen = gen; release(p, old_hash); return true; @@ -899,7 +898,7 @@ test "addon reload gives migration the old record version and saves the new vers var old = std.mem.zeroInit(Kind.Ops, .{ .head = std.mem.zeroInit(Kind.c.pa_head, .{ .size = @sizeOf(Kind.c.pa_head), .abi = Kind.abi, .version = 1 }), .open = Fake.open, .paint = Fake.paint, .dump = Fake.save, .load = Fake.load }); @memcpy(old.head.name[0..7], "migrate"); var words: [64]LoadedWord = undefined; - try std.testing.expect(p.kinds.take(loadedOf(&old, "test.so", @splat(0), &words), "") == null); + try std.testing.expect(p.kinds.take(p.tape, loadedOf(&old, "test.so", @splat(0), &words), "") == null); const pane = try Kind.open(p, 1, p.kinds.byName("migrate").?, null, null); var new = old; new.head.version = 2; diff --git a/core/Core.zig b/core/Core.zig index b4745b24..9684de26 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -514,16 +514,17 @@ pub const perform = @import("Effect.zig").perform; /// second copy of this drifted once, leaving restored tags without an /// allocator and every 9P mtime 0. pub fn create(gpa: std.mem.Allocator, opts: Options) !*Pardes { + // The global still set beside the field (P20): a core made while + // a tape records (PARDES_RECORD, a replica's step) is its. + const tape = opts.tape orelse pardes.tape.on orelse &pardes.tape.none; const p = try gpa.create(Pardes); p.* = .{ .gpa = gpa, .image_gpa = opts.image_allocator orelse gpa, - .kinds = .started(opts.addons), + .kinds = .started(tape, opts.addons), .tree_sitter_gpa = opts.tree_sitter_allocator orelse gpa, .opts = opts, - // The global still set beside the field (P20): a core made while - // a tape records (PARDES_RECORD, a replica's step) is its. - .tape = opts.tape orelse pardes.tape.on orelse &pardes.tape.none, + .tape = tape, .tty_toggle = opts.tty_toggle, .views = .{ View{ .frontend = .{ .windowed = opts.windowed }, diff --git a/core/Kind.zig b/core/Kind.zig index 9844369d..500319a5 100644 --- a/core/Kind.zig +++ b/core/Kind.zig @@ -19,7 +19,6 @@ const builtin = @import("builtin"); pub const c = @import("pardes_addon"); const pardes = @import("root.zig"); const walk = @import("walk.zig"); -const tape = @import("tape.zig"); const surface = @import("surface.zig"); const views = @import("views.zig"); const Addon = @import("Addon.zig"); @@ -104,10 +103,10 @@ pub const Registry = struct { nexts: u8 = 0, ops: [max_kinds]?*const Ops = @splat(null), - /// What the scan at start found. - pub fn started(found: []const Loaded) Registry { + /// What the scan at start found, taken on `tape` (a replica's holds no code). + pub fn started(tape: *pardes.tape.Tape, found: []const Loaded) Registry { var r: Registry = .{}; - for (found) |l| _ = r.take(l, ""); + for (found) |l| _ = r.take(tape, l, ""); return r; } @@ -297,25 +296,25 @@ pub fn openFile(p: *Pardes, id: usize, k: u8, path: []const u8) !*Pane { /// The slot's kind state made (`open`, then `load` with its record, or /// `address` with its place and its words' last values): its refusal, /// if the kind refused, in `slot.refused`. One call for both cores. -pub fn reopen(p: ?*Pardes, id: usize, s: *Slot, bytes: ?[]const u8) bool { +pub fn reopen(p: *Pardes, id: usize, s: *Slot, bytes: ?[]const u8) bool { // A replica has no kind's code: what the open says is the authority's. - if (tape.authorityOnly("a kind's open")) return false; + if (p.tape.authorityOnly("a kind's open")) return false; var cl: Call = .{ .p = p, .id = id, .slot = s }; const prev = cl.begin("open"); - if (p) |core| if (core.kinds.byName(s.nameOf())) |k| { + if (p.kinds.byName(s.nameOf())) |k| { // Its code: its library loaded at the first pane of its kind. - if (s.ops == null) s.ops = Addon.need(&core.kinds, k, &cl.refused, core.opts.distrusted); + if (s.ops == null) s.ops = Addon.need(&p.kinds, p.tape, k, &cl.refused, p.opts.distrusted); // Its block made afresh by this open. - if (core.kinds.kinds[k].caps.plain) { + if (p.kinds.kinds[k].caps.plain) { if (s.plain) |b| if (b.len > 0) s.gpa.free(b); s.plain = &.{}; } var w: std.Io.Writer = .fixed(&s.tail.bytes); w.writeAll(" Term") catch {}; - for (core.kinds.words[0..core.kinds.nwords]) |*kw| if (kw.kind == k and kw.opens == 0) w.print(" {s}", .{kw.name.slice()}) catch {}; + for (p.kinds.words[0..p.kinds.nwords]) |*kw| if (kw.kind == k and kw.opens == 0) w.print(" {s}", .{kw.name.slice()}) catch {}; w.writeAll(" Collapse Del") catch {}; s.tail.len = @intCast(w.buffered().len); - }; + } s.opened = if (bytes) |b| std.hash.Wyhash.hash(0, b) else 0; // Its file not read (not here): it waits for it, a placeholder. if (s.ops != null and bytes == null and s.path.len > 0) _ = cl.refused.print("{s} is not here", .{s.path}) else if (s.ops) |ops| { @@ -346,7 +345,7 @@ pub fn reopen(p: ?*Pardes, id: usize, s: *Slot, bytes: ?[]const u8) bool { if (s.record) |r| s.gpa.free(r); s.record = null; s.live = true; - if (p) |core| poke(core, s); + poke(p, s); return true; } @@ -397,7 +396,7 @@ pub fn close(p: *Pardes, pane: *Pane) void { // A client's pane of a plain kind closed in a speculation: its kind's // own memory (its pictures) is left be, as the rollback brings the pane // back; the session's order closes it. - const kept = s.plain != null and s.ops != null and s.ops.?.close != null and tape.authorityOnly("a kind's close"); + const kept = s.plain != null and s.ops != null and s.ops.?.close != null and p.tape.authorityOnly("a kind's close"); if (!kept) { var i: usize = 0; while (i < ready.items.len) if (ready.items[i].draw == p and ready.items[i].serial == pane.serial) ready.orderedRemove(i).drop() else { @@ -559,7 +558,7 @@ fn colorOf(v: u32, default: surface.Color) surface.Color { pub fn prefix(p: *const Pardes, arena: std.mem.Allocator, pane: *const Pane) ![]const u8 { const slot = pane.addon.?; var buf: [400]u8 = undefined; - return arena.dupe(u8, tape.value(.addon, prefixOf, .{ p, slot, &buf })); + return arena.dupe(u8, p.tape.value(.addon, prefixOf, .{ p, slot, &buf })); } fn prefixOf(p: *const Pardes, slot: *const Slot, buf: *[400]u8) []const u8 { @@ -584,7 +583,7 @@ fn prefixOf(p: *const Pardes, slot: *const Slot, buf: *[400]u8) []const u8 { pub fn key(p: *Pardes, id: usize, k: pardes.Key) bool { const slot = (p.panes[id] orelse return false).addon orelse return false; if (!caps(p, slot).key) return false; - if (!runsHere(slot) and tape.authorityOnly("a key into a kind's pane")) return true; + if (!runsHere(slot) and p.tape.authorityOnly("a key into a kind's pane")) return true; const pk: c.pa_key = .{ .cp = k.cp, .ctrl = @intFromBool(k.ctrl), .alt = @intFromBool(k.alt), .shift = @intFromBool(k.shift), .view = p.live + 1 }; var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("key"); @@ -608,7 +607,7 @@ pub fn takesKeys(p: *const Pardes, s: *const Slot) bool { pub fn search(p: *Pardes, id: usize, pat: []const u8) ?[]const u8 { const slot = (p.panes[id] orelse return null).addon orelse return null; if (!caps(p, slot).search) return null; - if (tape.authorityOnly("a kind's search")) return null; + if (p.tape.authorityOnly("a kind's search")) return null; var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("search"); if (slot.ops) |o| o.search.?(slot.st, pat.ptr, @intCast(pat.len)); @@ -624,7 +623,7 @@ pub fn address(p: *Pardes, id: usize, text: []const u8, said: *Buf(160)) bool { const slot = (p.panes[id] orelse return false).addon orelse return false; // By its caps, not its code: a replica without it calls as the authority does. if (!caps(p, slot).address) return false; - if (tape.authorityOnly("a kind's address")) return true; + if (p.tape.authorityOnly("a kind's address")) return true; var cl: Call = .{ .p = p, .id = id, .slot = slot, .missing = said }; const prev = cl.begin("address"); if (slot.ops) |o| cl.ret = o.address.?(slot.st, text.ptr, @intCast(text.len)); @@ -641,7 +640,7 @@ pub fn takesMouse(p: *const Pardes, s: *const Slot) bool { pub fn mouse(p: *Pardes, id: usize, m: pardes.Mouse, col: u16, row: u16) bool { const slot = (p.panes[id] orelse return false).addon orelse return false; if (!caps(p, slot).mouse) return false; - if (!runsHere(slot) and tape.authorityOnly("the mouse in a kind's pane")) return true; + if (!runsHere(slot) and p.tape.authorityOnly("the mouse in a kind's pane")) return true; const pm: c.pa_mouse = .{ .button = switch (m.button) { .left => c.PA_LEFT, @@ -684,8 +683,8 @@ pub fn step(p: *Pardes, due: bool) void { // A kind's call is the authority's alone but a plain kind's here: a // speculation's tick after a call into the pane waits with that call's // input (it stopped there). - const speculating = tape.speculating(); - if (speculating) for (p.panes) |pslot| if (pslot) |pane| if (pane.addon) |slot| if (slot.next_tick == 1 and !runsHere(slot)) return tape.poison("a kind's tick"); + const speculating = p.tape.speculating; + if (speculating) for (p.panes) |pslot| if (pslot) |pane| if (pane.addon) |slot| if (slot.next_tick == 1 and !runsHere(slot)) return p.tape.poison("a kind's tick"); for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |slot| { if (speculating and !runsHere(slot)) continue; if (due and caps(p, slot).view) for (0..views.max) |k| { @@ -875,7 +874,7 @@ pub fn ctl(p: *Pardes, id: usize, line: []const u8, reply: *[160]u8) ?[]const u8 const slot = (p.panes[id] orelse return "no such pane").addon orelse return "not a kind's pane"; if (!slot.live) return "its addon is not loaded"; if (!caps(p, slot).ctl) return "its kind takes no words"; - if (tape.authorityOnly("a kind's word")) return null; + if (p.tape.authorityOnly("a kind's word")) return null; var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("ctl"); @memset(reply, 0); @@ -975,19 +974,20 @@ const Call = struct { current = prev; if (checked) overBudget(cl); if (cl.quiet) return .{}; + // One that is not quiet is a step's, made with its core. + const t = cl.p.?.tape; var place: [32]u8 = undefined; // A refused open closed its state: no `where` to ask. const at: ?[]const u8 = if (cl.refused.len == 0) where(cl.slot, &place) else null; // Hashed while it records or replays: no client, no cost. - const state: u64 = if (tape.on == null) 0 else if (cl.slot.plain) |b| std.hash.Wyhash.hash(0, b) else 0; - const o = tape.value(.addon, same, .{Outcome{ .state = state, .ret = cl.ret, .text = cl.refused.slice(), .said = cl.said.slice(), .err = cl.err, .look = cl.look.slice(), .pane_name = cl.pane_name.slice(), .pane_text = cl.pane_text, .exec = cl.exec.slice(), .selected = cl.selected, .rows = cl.rows, .prefix = cl.prefix, .where = at, .jobs = cl.jobs, .cancels = cl.cancels }}); + const state: u64 = if (t.through) 0 else if (cl.slot.plain) |b| std.hash.Wyhash.hash(0, b) else 0; + const o = t.value(.addon, same, .{Outcome{ .state = state, .ret = cl.ret, .text = cl.refused.slice(), .said = cl.said.slice(), .err = cl.err, .look = cl.look.slice(), .pane_name = cl.pane_name.slice(), .pane_text = cl.pane_text, .exec = cl.exec.slice(), .selected = cl.selected, .rows = cl.rows, .prefix = cl.prefix, .where = at, .jobs = cl.jobs, .cancels = cl.cancels }}); if (o.where) |w| cl.slot.where_text.set(w); // Run here, it came out otherwise than the session's: it broke its // promise (PA_PLAIN), and the client joins again without it. - if (cl.slot.ops != null and cl.slot.plain != null and tape.replaying() and o.state != state) { + if (cl.slot.ops != null and cl.slot.plain != null and t.replaying() and o.state != state) { if (cl.p) |p| if (p.opts.distrusted) |d| d.add(cl.slot.nameOf()); - var why: Buf(160) = .{}; - tape.diverged(why.print("addon {s}: its state after {s} is not the session's (it breaks PA_PLAIN): the session's alone now", .{ cl.slot.nameOf(), cl.name })); + if (t.replay) t.diverge("addon {s}: its state after {s} is not the session's (it breaks PA_PLAIN): the session's alone now", .{ cl.slot.nameOf(), cl.name }); } cl.slot.state = o.state; // Its last job's token the session's, on a core whose kind asked @@ -1120,7 +1120,7 @@ pub const api: c.pa_api = .{ const asked = std.mem.indexOfScalar(u32, &cl.jobs, 0) orelse return 0; // A client's (a plain kind's code here) runs none: its token // alone, as the session's; the answer comes on the tape. - const free = if (tape.replaying()) null else for (&s.jobs) |*j| { + const free = if (cl.p.?.tape.replaying()) null else for (&s.jobs) |*j| { if (j.* == null) break j; } else return 0; const copy = if (free != null) s.gpa.dupe(u8, if (in) |b| @as([*]const u8, @ptrCast(b))[0..n] else "") catch return 0 else ""; @@ -1314,7 +1314,7 @@ pub const test_kind = struct { pub fn of(p: *Pardes) u8 { if (p.kinds.byName("test")) |k| return k; var words: [64]LoadedWord = undefined; - _ = p.kinds.take(loadedOf(&ops, "", @splat(0), &words), ""); + _ = p.kinds.take(p.tape, loadedOf(&ops, "", @splat(0), &words), ""); return p.kinds.n - 1; } }; @@ -1353,10 +1353,10 @@ test "a kind's head is checked before a core takes it, and a core's kinds are it var a: Registry = .{}; var b: Registry = .{}; var words: [64]LoadedWord = undefined; - try std.testing.expect(a.take(loadedOf(&ops, "", @splat(1), &words), "") == null); + try std.testing.expect(a.take(&pardes.tape.none, loadedOf(&ops, "", @splat(1), &words), "") == null); try std.testing.expect(a.byName("zzzab") != null); try std.testing.expect(b.byName("zzzab") == null); - try std.testing.expectEqualStrings("refused: kind zzzab is here already", a.take(loadedOf(&ops, "", @splat(1), &words), "").?); + try std.testing.expectEqualStrings("refused: kind zzzab is here already", a.take(&pardes.tape.none, loadedOf(&ops, "", @splat(1), &words), "").?); a.drop(a.byName("zzzab").?); try std.testing.expect(a.byName("zzzab") == null); } diff --git a/core/dump.zig b/core/dump.zig index 4775ad63..31496674 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -572,7 +572,7 @@ fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previ errdefer p.deinit(); // Its kinds are this process's, not the dump's: the session's it // replaces, or what the scan found (a dump of another session's). - p.kinds = if (previous) |old| old.kinds else .started(opts.addons); + p.kinds = if (previous) |old| old.kinds else .started(p.tape, opts.addons); for (p.panes, on_disk, 0..) |slot, was_on_disk, i| if (slot) |pane| { if (kept) |k| if (k[i] and pane.terminal != null) continue; revivePane(p, i, pane, was_on_disk); diff --git a/core/tape.zig b/core/tape.zig index a2cd1e3d..e0bc95e7 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -491,7 +491,9 @@ pub const Tape = struct { return bytes; } - fn diverge(t: *Tape, comptime why: []const u8, args: anytype) void { + /// A replay came to another state than the authority's (`why`, the + /// first said): its replica joins again. + pub fn diverge(t: *Tape, comptime why: []const u8, args: anytype) void { if (t.diverged != null) return; t.diverged = std.fmt.allocPrint(t.gpa, why ++ " (tape byte {d}, in {s})", args ++ .{ t.in.at, t.applying }) catch why; } |
