diff options
Diffstat (limited to 'src/Addon.zig')
| -rw-r--r-- | src/Addon.zig | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/src/Addon.zig b/src/Addon.zig index cadf28d7..80d8fdd8 100644 --- a/src/Addon.zig +++ b/src/Addon.zig @@ -313,7 +313,6 @@ fn readSmall(path: [*:0]const u8) Kind.Buf(4096) { return out; } - // ---- the tail: what a library brings, and a core's tables of it ---- /// What a library brings, as the host read it off its head (or why it @@ -356,7 +355,12 @@ pub fn loadedOf(ops: *const Kind.Ops, path: []const u8, hash: [32]u8, words: *[6 .words = words[0..head_words.len], .ops = @intFromPtr(ops), .caps = .{ - .tick = ops.tick != null, .key = ops.key != null, .mouse = ops.mouse != null, .ctl = ops.ctl != null, .jobs = ops.job_run != null, .load = ops.load != null, + .tick = ops.tick != null, + .key = ops.key != null, + .mouse = ops.mouse != null, + .ctl = ops.ctl != null, + .jobs = ops.job_run != null, + .load = ops.load != null, .search = ops.search != null, .address = ops.address != null, .view = ops.view != null, @@ -483,7 +487,6 @@ fn coreWord(name: []const u8) bool { return std.meta.stringToEnum(builtins.registry.Builtin(), name) != null or builtins.absentWhy(name) != null; } - // ---- the core's half ---- /// `Addon load <path>` (relative to the pane's directory), `Addon unload @@ -602,7 +605,8 @@ fn swap(p: *Pardes, l: Loaded, why: *Why) bool { }; for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |s| if (s.live and s.is(name)) { const n = s.gpa.create(Kind.Slot) catch return fail(why, "out of memory", .{}); - n.* = .{ .ops = new_ops, .live = true, .gpa = s.gpa, .arena = .init(s.gpa), .name = s.name, .version = l.version, .gen = gen, .art = s.art, .values = s.values }; + // load receives the record's version, not the replacement library's. + n.* = .{ .ops = new_ops, .live = true, .gpa = s.gpa, .arena = .init(s.gpa), .name = s.name, .version = s.version, .gen = gen, .art = s.art, .values = s.values }; fresh[id] = n; n.path = s.gpa.dupe(u8, s.path) catch return fail(why, "out of memory", .{}); // The old state's record, by the old library. @@ -658,6 +662,44 @@ pub fn list(p: *const Pardes, out: *std.ArrayList(u8), gpa: std.mem.Allocator) ! } } +test "addon reload gives migration the old record version and saves the new version" { + const Fake = struct { + var state: u8 = 7; + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + return &state; + } + fn paint(_: ?*anyopaque, _: [*c]const Kind.c.pa_window, _: [*c]Kind.c.pa_paint) callconv(.c) void {} + fn save(_: ?*anyopaque, out: [*c]u8, n: u32) callconv(.c) u32 { + if (n > 0) out[0] = state; + return 1; + } + fn load(_: ?*anyopaque, bytes: [*c]const u8, n: u32, version: u32) callconv(.c) c_int { + return if (version == 1 and n == 1 and bytes[0] == 7) Kind.c.PA_OK else 1; + } + }; + const p = try @import("ninep/testing.zig").withFile(std.testing.allocator, "x\n"); + defer p.deinit(); + 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); + const pane = try Kind.open(p, 1, p.kinds.byName("migrate").?, null, null); + var new = old; + new.head.version = 2; + var why: Why = .{}; + try std.testing.expect(swap(p, loadedOf(&new, "test.so", @splat(1), &words), &why)); + try std.testing.expectEqual(@as(u32, 2), pane.addon.?.version); + try std.testing.expectEqual(@as(u32, 2), p.kinds.kinds[p.kinds.byName("migrate").?].version); + // A saved v1 placeholder revived by v2 must also save future records as v2. + const slot = pane.addon.?; + slot.version = 1; + slot.live = false; + slot.record = try slot.gpa.dupe(u8, &.{7}); + dump.reviveSlot(p, 1, slot); + try std.testing.expect(slot.live); + try std.testing.expectEqual(@as(u32, 2), slot.version); +} + test "a load that never finished is skipped at the next start, and every start after, until a load asks for it" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); |
