diff options
Diffstat (limited to 'src/dump.zig')
| -rw-r--r-- | src/dump.zig | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/dump.zig b/src/dump.zig index 3c5decdd..3c31c805 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -19,6 +19,7 @@ const effect_path_cap = pardes.effect_path_cap; const Options = pardes.Options; const Pardes = pardes.Pardes; const views = pardes.views; +const Kind = @import("Kind.zig"); /// A Dump's label in the state file (Db.zig): `pardes-<date>-<time>`, UTC /// (another in the same second is `-2` and on there), in `buf`. @@ -494,6 +495,9 @@ fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previ var on_disk: OnDisk = @splat(false); const p = try loadKnowing(gpa, opts, bytes, ¬es, &on_disk); 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); 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); @@ -565,6 +569,7 @@ fn reviveSession(p: *Pardes, previous: ?*Pardes, resumed: bool) !void { /// disk since comes back as it is now, one unsaved is marked changed), a /// PDF opened again. fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool) void { + if (pane.addon) |slot| return reviveSlot(p, id, slot); if (pane.terminal != null) { // A command pane comes back finished, showing what it said: its // command is not run again. A host that runs shells starts one @@ -884,3 +889,84 @@ test "an old dump's file, a byte dump, is read by -l and by a Restore of its pat defer gpa.free(again); try std.testing.expectEqualSlices(u8, bytes, again); } + +// ---- a pane of a kind (Kind.Slot) ---- + +/// What a dump keeps of a pane of a kind: the kind, its path, its place, +/// the art bytes, its words' last values, whether its kind was here, and +/// the kind's own record when it has one. +const SlotRecord = struct { + name: []const u8 = "", + version: u32 = 0, + path: []const u8 = "", + where: []const u8 = "", + art: @import("surface.zig").Placement.Art = .{}, + record: ?[]const u8 = null, + values: []const u8 = "", + live: bool = false, +}; + +pub fn slotWrite(s: *const Kind.Slot, gpa: std.mem.Allocator, w: *std.Io.Writer) !void { + var place: [32]u8 = undefined; + var r: SlotRecord = .{ .name = s.nameOf(), .version = s.version, .path = s.path, .art = s.art, .record = s.record, .where = s.where_text.slice(), .values = s.values.slice(), .live = s.live }; + var owned: ?[]u8 = null; + defer if (owned) |o| s.gpa.free(o); + if (s.ops != null) { + r.where = Kind.where(@constCast(s), &place); + owned = try slotRecord(@constCast(s)); + r.record = owned; + } + const bytes = try walk.save(SlotRecord, gpa, &r); + defer gpa.free(bytes); + try w.writeAll(bytes); +} + +/// The kind's record of a slot's state (`s.gpa`'s), or null for none: a +/// dump of at most 1 MiB, then as many bytes as it asked for. +pub fn slotRecord(s: *Kind.Slot) !?[]u8 { + const f = (s.ops orelse return null).dump orelse return null; + var cap: usize = 1 << 20; + while (true) { + const buf = try s.gpa.alloc(u8, cap); + const n = Kind.dumpCall(s, f, buf); + if (n == 0) { + s.gpa.free(buf); + return null; + } + if (n <= cap) return s.gpa.realloc(buf, n) catch buf[0..n]; + s.gpa.free(buf); + cap = n; + } +} + +/// The pane back from its record, a placeholder holding it until its +/// kind opens it (`revivePane`; a replica's core keeps it a Shadow). +pub fn slotRead(out: *Kind.Slot, gpa: std.mem.Allocator, bytes: []const u8) !void { + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const r = try walk.load(SlotRecord, arena.allocator(), bytes, null); + out.* = .{ .gpa = gpa, .arena = .init(gpa), .version = r.version, .art = r.art, .live = r.live }; + errdefer out.free(); + @memcpy(out.name[0..@min(15, r.name.len)], r.name[0..@min(15, r.name.len)]); + out.path = try gpa.dupe(u8, r.path); + out.where_text.set(r.where); + out.values.set(r.values); + if (r.record) |rec| out.record = try gpa.dupe(u8, rec); +} + +pub fn slotFree(s: *Kind.Slot, gpa: std.mem.Allocator) void { + _ = gpa; + s.free(); +} + +/// A loaded pane of a kind opened by its kind, if this core has it, else +/// a placeholder saying what it waits for. +pub fn reviveSlot(p: *Pardes, id: usize, s: *Kind.Slot) void { + const k = p.kinds.byName(s.nameOf()) orelse { + s.live = false; + return; + }; + s.ops = p.kinds.ops[k]; + s.gen = p.kinds.kinds[k].gen; + _ = Kind.reopen(p, id, s, null); +} |
