summaryrefslogtreecommitdiff
path: root/src/Addon.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 02:31:55 -0300
committerGabriel Schneider <[email protected]>2026-11-06 04:04:25 -0300
commit9c66cdc9049b49f128336dcf5046e2bb1321e281 (patch)
tree7ff664677eb7291558f71a8f824d0ce6a922779a /src/Addon.zig
parenta6f294ef5b50aeb5b69e486838378654e318ed04 (diff)
downloadpardes-9c66cdc9049b49f128336dcf5046e2bb1321e281.tar.gz
pardes-9c66cdc9049b49f128336dcf5046e2bb1321e281.zip
kinds #5, ABI 2 and the seam: src/kinds/pardes_addon.h is ABI 2 as architecture §16.9 has it (pa_window 24 bytes and no art bytes, pa_placement 80 with at, from and dirty_y/dirty_h and no stride, pa_word 32 with no size and PA_WORD, tick(st, now_ns, flags) with PA_FOCUSED, PA_OK and the conventions, the dump and load rules, pa_put with attrs, pa_copy and PA_POD, whose fragment gcc -Werror refused as written and is ternaries here) and fake.c and image's table speak it; tick is called at its deadline and once after open, a taken key or click, a word, a job's answer, a load and a change of PA_FOCUSED (the pane has the keyboard in some view, its window focused), nextWake folds the deadlines, and a Debug build hashes the kind's allocations across paint and panics naming it; paint runs once a frame, the slot keeps its prefix and whether it placed a picture, the tag is that prefix, the core's art words when it did and the path, and a frame that changes it owes a step to lay the tag out again; Addon load and reload are an addon effect the host performs (memfd, BLAKE3, dlopen, the head checks and init, the bytes a process holds shared and counted) answered by an addon event carrying what the library brings as data, whose update does the collisions, the reload's validate-then-swap at a new generation and the waiting panes deterministically, while unload is the update's alone and lets the library go by effect; a kind's job is Effect.job run on the host's workers (host_io.KindJob: tui and gui on Io.concurrent, macOS and tests on the loop) answered by Event.job, stale generations dropped and a library closed after its last job; each core has its own Registry (16 kinds, their words, leaders and extensions, digested and dumped; their code a local side array a replica has none of), so a test session no longer unloads the world and Restore and -l keep the process's kinds; every call into a kind whose answer the core acts on is taped as an Outcome (its answer, refusal, message, Look, text pane) and the prefix a step reads is taped too, so a replica's core, which runs no kind's code, steps alike; a pane of a kind on a replica is a Shadow drawn from the authority's frames, painted for the peer's view and sent by Screens as row hashes with the rows that changed and each raster once a key (screen_state.Shadow), its keys going to the authority; the tail, reload and the Addon word and event are Addon.zig's, the slot's record dump.zig's, a tail word's run exec.zig's; --addon-test gains focus, blur, dump, body, ctl and the window line, its jobs done as a host does them; fake.out gains its window line, loses a body row once the tag carries the art words and lands FakeJob at once; tests rewritten: the image dump test (the replica-style load keeps the record), the Petscii/Palette/Ascii tag test in ctl.zig and test/panes.zig's image tag test (a picture decoded and shown first), fs_namespace's decode test (the job performed as a host would), Kind.zig's registry test and Addon.zig's crash-mark test, the addons suite's ABI numbers; new: a job under way across a reload (T1), a Shadow's frame codec, and a replica with no code for a kind showing its pane and passing its keys; gates: core-test 958/958 and behaviour-test 249/249 on both, unit-test 1490/1491 tui and 1533/1534 gui, pane-test 171/172, snap all 105 ok, process-test 0 of 37 failed on both, addon-test ok, hxdiff 19 and hxparity 6 mismatches by the base's names, hxgolf 0, replicas, gui-golden, monkey-gui, daemon.py main and window-clients, soak local pass, perf medium within 1% of the base
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/Addon.zig')
-rw-r--r--src/Addon.zig652
1 files changed, 491 insertions, 161 deletions
diff --git a/src/Addon.zig b/src/Addon.zig
index 2a963cb2..61b8327d 100644
--- a/src/Addon.zig
+++ b/src/Addon.zig
@@ -1,82 +1,111 @@
-//! Addons: shared libraries that each bring a kind of pane (Kind.zig),
-//! the host's side of it. Each `*.so` of `$XDG_DATA_HOME/pardes/addons`
-//! and then `<prefix>/lib/pardes/addons` at start, and `Addon load
-//! <path>` after. An addon exports `pardes_addon`, a `pa_ops`
-//! (src/kinds/pardes_addon.h); Kind.register checks it and takes it in.
+//! Addons: shared libraries that each bring a kind of pane (Kind.zig).
//!
-//! An addon runs in this process: one that crashes takes the session
-//! with it. So a load is marked first (`addon-loading` in the data
-//! directory, holding its path) and unmarked after; a start that finds
-//! the mark moves it to `addon-crashed`, and skips that addon at every
-//! start until `Addon load` asks for it by path again.
+//! The host's half: the libraries this process holds (each a copy of its
+//! file's bytes in a memfd, its BLAKE3, its head checked and its init
+//! run), shared by every core that took the same bytes and closed when no
+//! core and no job holds one; the scan of `$XDG_DATA_HOME/pardes/addons`
+//! and `<prefix>/lib/pardes/addons` at start; the crash mark. An addon
+//! runs in this process: one that crashes takes the session with it. So a
+//! load is marked first (`addon-loading` in the data directory, holding
+//! its path) and unmarked after; a start that finds the mark moves it to
+//! `addon-crashed`, and skips that addon at every start until `Addon load`
+//! asks for it by path again.
+//!
+//! The core's half, deterministic: the `Addon` word, the `addon` event's
+//! update (the collisions, a reload's validate-then-swap, the panes that
+//! waited), unload, and /addons. Nothing there reads a host fact: what a
+//! library brought comes in the event, as data a replica's core takes too.
const std = @import("std");
const Kind = @import("Kind.zig");
const dump = @import("dump.zig");
const pardes = @import("pardes.zig");
+const tape = @import("tape.zig");
+const Pardes = pardes.Pardes;
const Why = Kind.Buf(160);
-/// The core's `addon` effect performed, its answer the `addon` event:
-/// `arg` a path to load (or reload), or the kind's name to let go.
-pub fn perform(p: *pardes.Pardes, op: Kind.AddonOp, pane: u8, arg: []const u8) void {
- var why: Why = .{};
- const ok = switch (op) {
- .load => load(arg, &why),
- .unload => ok: {
- why.set(arg);
- break :ok unload(arg);
- },
- .reload => ok: {
- if (!reload(p, arg, &why)) break :ok false;
- why.set(arg);
- break :ok true;
- },
- };
- p.update(.{ .addon = .{ .op = op, .pane = pane, .ok = ok, .text = why.slice() } });
+/// What the effect asks: a library loaded (its path), loaded again for a
+/// kind's reload (the kind's path), or let go (its BLAKE3, the arg's bytes).
+pub const Op = enum { load, reload, release };
+
+// ---- the libraries this process holds (the host's) ----
+
+const Lib = struct { hash: [32]u8, handle: *anyopaque, ops: *const Kind.Ops, fd: c_int, refs: u32 = 1, jobs: u32 = 0 };
+var libs: [32]?Lib = @splat(null);
+
+fn libOf(ops: *const Kind.Ops) ?*Lib {
+ for (&libs) |*l| if (l.*) |*lib| if (lib.ops == ops) return lib;
+ return null;
}
-/// The addon at `path` loaded and its kind taken in: true, or false
-/// with why not in `why`.
-pub fn load(path: []const u8, why: *Why) bool {
- var data_buf: [4096]u8 = undefined;
- return loadMarked(dataDir(&data_buf), path, why);
+/// A job of `ops` under way (true), or answered: its library stays until
+/// the last of them, so no worker is unmapped under its own code.
+pub fn holdJob(ops: *const Kind.Ops, held: bool) void {
+ const lib = libOf(ops) orelse return;
+ if (held) lib.jobs += 1 else lib.jobs -|= 1;
+ closeIdle(lib);
}
-/// Where the crash mark goes: the data directory, none in a test (a
-/// test's loads are no user's).
-fn dataDir(buf: []u8) []const u8 {
- if (@import("builtin").is_test) return "";
- return dump.defaultDirectory(buf) orelse "";
+fn closeIdle(lib: *Lib) void {
+ if (lib.refs > 0 or lib.jobs > 0) return;
+ _ = std.c.dlclose(lib.handle);
+ _ = std.c.close(lib.fd);
+ for (&libs) |*l| if (l.*) |*x| if (x == lib) {
+ l.* = null;
+ };
}
-fn loadMarked(data: []const u8, path: []const u8, why: *Why) bool {
- if (path.len >= 4096) return fail(why, "{s}: the path is too long", .{path});
- Kind.forgetRefused(path);
+/// The host's side of the `addon` effect: the library read and checked,
+/// and what it brought (or why not) the core's `addon` event.
+pub fn perform(p: *Pardes, op: Op, pane: u8, arg: []const u8) void {
+ if (op == .release) {
+ for (&libs) |*l| if (l.*) |*lib| if (arg.len == 32 and std.mem.eql(u8, &lib.hash, arg)) {
+ lib.refs -|= 1;
+ return closeIdle(lib);
+ };
+ return;
+ }
+ var data_buf: [4096]u8 = undefined;
+ var words: [64]LoadedWord = undefined;
+ var why: Why = .{};
+ const l = read(dataDir(&data_buf), arg, &words, &why);
+ p.update(.{ .addon = .{ .op = op, .pane = pane, .kind = &l } });
+}
+
+/// The library at `path`, held (a second hold of the same bytes is the
+/// first's library) and checked: what it brings, or why not.
+fn read(data: []const u8, path: []const u8, words: *[64]LoadedWord, why: *Why) Loaded {
mark(data, path);
defer unmark();
var hash: [32]u8 = undefined;
- const lib = open(path, &hash, why) orelse {
- Kind.note(path, why.slice());
- return false;
+ const lib = hold(path, &hash, why) orelse return .{ .path = path, .why = why.slice() };
+ const l = loadedOf(lib.ops, path, hash, words);
+ const refused: ?[]const u8 = refused: {
+ const h = &lib.ops.head;
+ if (h.size < @sizeOf(Kind.c.pa_head)) break :refused "refused: its head is too short for this pardes";
+ if (h.abi >> 16 != Kind.abi_major) break :refused why.print("refused: wants abi {d}, this pardes speaks {d}", .{ h.abi >> 16, Kind.abi_major });
+ if (lib.ops.paint == null) break :refused "refused: it has no paint";
+ for (l.words) |w| if (w.opens == 0 and lib.ops.ctl == null) break :refused "refused: it has words for its panes and no ctl";
+ if ((lib.ops.job_run == null) != (lib.ops.job_done == null)) break :refused "refused: a job needs job_run and job_done";
+ if (h.nwords > words.len) break :refused "refused: no room for its words";
+ if (lib.ops.init) |init| if (init(&Kind.api) != Kind.c.PA_OK) break :refused "refused: its init failed";
+ break :refused null;
};
- const sym = std.c.dlsym(lib, "pardes_addon") orelse {
- _ = std.c.dlclose(lib);
- Kind.note(path, "refused: it has no pardes_addon");
- return fail(why, "{s}: refused: it has no pardes_addon", .{std.fs.path.basename(path)});
- };
- const ops: *const Kind.Ops = @ptrCast(@alignCast(sym));
- if (Kind.registerHashed(ops, path, lib, hash)) |refused| {
- _ = std.c.dlclose(lib);
- return fail(why, "{s}: {s}", .{ std.fs.path.basename(path), refused });
- }
- why.set(Kind.headName(ops));
- return true;
+ const r = refused orelse return l;
+ if (r.ptr != &why.bytes) why.set(r);
+ // What is listed of it outlives its library: its name, in `words`.
+ const name: []u8 = @as([*]u8, @ptrCast(words))[0..@min(15, l.name.len)];
+ @memcpy(name, l.name[0..name.len]);
+ lib.refs -= 1;
+ closeIdle(lib);
+ return .{ .name = name, .version = l.version, .abi = l.abi, .hash = hash, .path = path, .why = why.slice() };
}
-/// The library at `path` opened from a copy of its bytes in memory, so a
-/// reload of a path already open is the file as it is now (dlopen hands
-/// back the handle it has for a path it has), and their BLAKE3 in `hash`.
-fn open(path: []const u8, hash: *[32]u8, why: *Why) ?*anyopaque {
+/// The library at `path` held: opened from a copy of its bytes in a
+/// memfd, so a reload of a path already open is the file as it is now
+/// (dlopen hands back the handle it has for a path it has), unless this
+/// process holds those very bytes already.
+fn hold(path: []const u8, hash: *[32]u8, why: *Why) ?*Lib {
var z: [4096]u8 = undefined;
const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return failNull(why, "the path is too long", .{});
const f = std.c.fopen(path_z, "r") orelse return failNull(why, "cannot read it", .{});
@@ -92,97 +121,56 @@ fn open(path: []const u8, hash: *[32]u8, why: *Why) ?*anyopaque {
if (bytes.items.len > 256 << 20) return failNull(why, "it is over 256 MiB", .{});
}
std.crypto.hash.Blake3.hash(bytes.items, hash, .{});
+ for (&libs) |*l| if (l.*) |*lib| if (std.mem.eql(u8, &lib.hash, hash)) {
+ lib.refs += 1;
+ return lib;
+ };
+ const free = for (&libs) |*l| {
+ if (l.* == null) break l;
+ } else return failNull(why, "this process holds 32 libraries already", .{});
+ // The fd stays open while its library is: /proc/self/fd/<n> of a closed
+ // fd is the next load's path too, and dlopen would hand back this one.
const fd = std.c.memfd_create("pardes-addon", 0);
if (fd < 0) return failNull(why, "no memfd for it", .{});
- // shortcut: the fd stays open while the process lives. dlopen hands
- // back the library it has for a path it has, and /proc/self/fd/<n> of
- // a closed fd is the next load's path too: an fd kept is a name kept
- // (one an addon load; a dev loop of a hundred reloads is a hundred).
- errdefer _ = std.c.close(fd);
var off: usize = 0;
while (off < bytes.items.len) {
const n = std.c.write(fd, bytes.items.ptr + off, bytes.items.len - off);
- if (n <= 0) return failNull(why, "cannot copy it", .{});
+ if (n <= 0) {
+ _ = std.c.close(fd);
+ return failNull(why, "cannot copy it", .{});
+ }
off += @intCast(n);
}
var proc: [64]u8 = undefined;
const at = std.fmt.bufPrintSentinel(&proc, "/proc/self/fd/{d}", .{fd}, 0) catch unreachable;
- return std.c.dlopen(at, .{ .NOW = true }) orelse {
+ const handle = std.c.dlopen(at, .{ .NOW = true }) orelse {
_ = std.c.close(fd);
return failNull(why, "{s}", .{if (std.c.dlerror()) |e| std.mem.span(e) else "cannot load it"});
};
+ const sym = std.c.dlsym(handle, "pardes_addon") orelse {
+ _ = std.c.dlclose(handle);
+ _ = std.c.close(fd);
+ return failNull(why, "refused: it has no pardes_addon", .{});
+ };
+ free.* = .{ .hash = hash.*, .handle = handle, .ops = @ptrCast(@alignCast(sym)), .fd = fd };
+ return &free.*.?;
}
-fn failNull(why: *Why, comptime fmt: []const u8, args: anytype) ?*anyopaque {
+fn failNull(why: *Why, comptime fmt: []const u8, args: anytype) ?*Lib {
_ = why.print(fmt, args);
return null;
}
-/// `Addon reload <name>`: its library loaded again from its path and
-/// swapped in, or the old one left running and why not.
-pub fn reload(p: *pardes.Pardes, name: []const u8, why: *Why) bool {
- var path: [256]u8 = undefined;
- const at = for (Kind.kinds()) |*l| {
- if (l.ops != null and std.mem.eql(u8, l.nameOf(), name)) {
- @memcpy(path[0..l.path.len], l.path.slice());
- break path[0..l.path.len];
- }
- } else return fail(why, "no addon brought kind {s}", .{name});
- var data_buf: [4096]u8 = undefined;
- mark(dataDir(&data_buf), at);
- defer unmark();
- var hash: [32]u8 = undefined;
- const lib = open(at, &hash, why) orelse return false;
- const sym = std.c.dlsym(lib, "pardes_addon") orelse {
- _ = std.c.dlclose(lib);
- return fail(why, "refused: it has no pardes_addon", .{});
- };
- var old: ?*anyopaque = null;
- if (!Kind.replace(p, name, @ptrCast(@alignCast(sym)), lib, hash, &old, why)) {
- _ = std.c.dlclose(lib);
- return false;
- }
- if (old) |o| _ = std.c.dlclose(o);
- return true;
-}
-
-/// Every addon let go, and the refused forgotten: the kinds a process
-/// starts with (a test's session ends so, the kinds being the process's).
-pub fn unloadAll() void {
- for (0..Kind.max_kinds) |_| {
- const l = for (Kind.kinds()) |*k| {
- if (k.lib != null and k.ops != null) break k;
- } else break;
- var name: [16]u8 = l.name;
- _ = unload(std.mem.sliceTo(&name, 0));
- }
- for (0..Kind.max_kinds) |_| {
- const l = for (Kind.kinds()) |*k| {
- if (k.ops == null) break k;
- } else break;
- var path: [256]u8 = undefined;
- @memcpy(path[0..l.path.len], l.path.slice());
- Kind.forgetRefused(path[0..l.path.len]);
- }
-}
-
-/// The kind `name` out, its library closed: false when none is loaded.
-/// The caller has seen that no pane of it is open.
-pub fn unload(name: []const u8) bool {
- const gone = Kind.unregister(name) orelse return false;
- if (gone.lib) |lib| _ = std.c.dlclose(lib);
- return true;
-}
-
-fn fail(why: *Why, comptime fmt: []const u8, args: anytype) bool {
- _ = why.print(fmt, args);
- return false;
-}
+/// What the scan at start found, which every core this process makes
+/// starts with (Options.addons).
+var found: [Kind.max_kinds]Loaded = undefined;
+var found_words: [Kind.max_kinds][64]LoadedWord = undefined;
+var found_text: [Kind.max_kinds][2][4096]u8 = undefined;
+var nfound: usize = 0;
/// Every library of the two directories, in name order each, the user's
-/// first (a kind of a name taken is refused, and listed so). A library
-/// that crashed at its last load is listed, not loaded.
-pub fn scan() void {
+/// first. A library that crashed at its last load is listed, not loaded.
+pub fn scan() []const Loaded {
var data_buf: [4096]u8 = undefined;
const data = dataDir(&data_buf);
const crashed = crashedPath(data);
@@ -190,15 +178,12 @@ pub fn scan() void {
if (data.len > 0) scanDir(data, std.fmt.bufPrint(&dir_buf, "{s}/addons", .{data}) catch "", crashed.slice());
var exe: [4096]u8 = undefined;
const n = std.c.readlink("/proc/self/exe", &exe, exe.len);
- if (n > 0) {
- const bin_dir = std.fs.path.dirname(exe[0..@intCast(n)]) orelse return;
- const prefix = std.fs.path.dirname(bin_dir) orelse return;
+ if (n > 0) if (std.fs.path.dirname(exe[0..@intCast(n)])) |bin| if (std.fs.path.dirname(bin)) |prefix|
scanDir(data, std.fmt.bufPrint(&dir_buf, "{s}/lib/pardes/addons", .{prefix}) catch "", crashed.slice());
- }
+ return found[0..nfound];
}
fn scanDir(data: []const u8, dir: []const u8, crashed: []const u8) void {
- if (dir.len == 0) return;
var z: [4096]u8 = undefined;
const dz = std.fmt.bufPrintSentinel(&z, "{s}", .{dir}, 0) catch return;
const d = std.c.opendir(dz) orelse return;
@@ -215,26 +200,36 @@ fn scanDir(data: []const u8, dir: []const u8, crashed: []const u8) void {
}
var order: [32]usize = undefined;
for (0..count) |i| order[i] = i;
+ const Sort = struct {
+ names: *const [32][256]u8,
+ lens: *const [32]usize,
+ fn lt(s: @This(), a: usize, b: usize) bool {
+ return std.mem.lessThan(u8, s.names[a][0..s.lens[a]], s.names[b][0..s.lens[b]]);
+ }
+ };
std.mem.sort(usize, order[0..count], Sort{ .names = &names, .lens = &lens }, Sort.lt);
for (order[0..count]) |i| {
- var full: [4096]u8 = undefined;
- const path = std.fmt.bufPrint(&full, "{s}/{s}", .{ dir, names[i][0..lens[i]] }) catch continue;
- if (crashed.len > 0 and std.mem.eql(u8, path, crashed)) {
- Kind.note(path, "skipped: it crashed its last load (Addon load asks for it again)");
- continue;
- }
+ if (nfound == found.len) return;
+ const text = &found_text[nfound];
+ const path = std.fmt.bufPrint(&text[0], "{s}/{s}", .{ dir, names[i][0..lens[i]] }) catch continue;
var why: Why = .{};
- _ = loadMarked(data, path, &why);
+ found[nfound] = if (crashed.len > 0 and std.mem.eql(u8, path, crashed))
+ .{ .path = path, .why = "skipped: it crashed its last load (Addon load asks for it again)" }
+ else
+ read(data, path, &found_words[nfound], &why);
+ const w = found[nfound].why;
+ @memcpy(text[1][0..w.len], w);
+ found[nfound].why = text[1][0..w.len];
+ nfound += 1;
}
}
-const Sort = struct {
- names: *const [32][256]u8,
- lens: *const [32]usize,
- fn lt(s: Sort, a: usize, b: usize) bool {
- return std.mem.lessThan(u8, s.names[a][0..s.lens[a]], s.names[b][0..s.lens[b]]);
- }
-};
+/// Where the crash mark goes: the data directory, none in a test (a
+/// test's loads are no user's).
+fn dataDir(buf: []u8) []const u8 {
+ if (@import("builtin").is_test) return "";
+ return dump.defaultDirectory(buf) orelse "";
+}
// ---- the crash mark ----
@@ -258,8 +253,7 @@ fn mark(data: []const u8, path: []const u8) void {
// `Addon load` of the one that crashed asks for it again.
var cbuf: [4096]u8 = undefined;
const crashed = markPath(&cbuf, data, "addon-crashed") orelse return;
- const was = readSmall(crashed);
- if (std.mem.eql(u8, was.slice(), path)) _ = std.c.unlink(crashed);
+ if (std.mem.eql(u8, readSmall(crashed).slice(), path)) _ = std.c.unlink(crashed);
}
fn unmark() void {
@@ -270,7 +264,7 @@ fn unmark() void {
}
/// The library that crashed its session's load: a mark left by a start
-/// that never finished one becomes `kind-crashed`, which it stays.
+/// that never finished one becomes `addon-crashed`, which it stays.
fn crashedPath(data: []const u8) Kind.Buf(4096) {
var out: Kind.Buf(4096) = .{};
if (data.len == 0) return out;
@@ -287,11 +281,347 @@ fn readSmall(path: [*:0]const u8) Kind.Buf(4096) {
var out: Kind.Buf(4096) = .{};
const f = std.c.fopen(path, "r") orelse return out;
defer _ = std.c.fclose(f);
- const n = std.c.fread(&out.bytes, 1, out.bytes.len, f);
- out.len = @intCast(n);
+ out.len = @intCast(std.c.fread(&out.bytes, 1, out.bytes.len, f));
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
+/// brought nothing): the `addon` event's, and the scan's at start.
+pub const Loaded = struct {
+ name: []const u8 = "",
+ version: u32 = 0,
+ abi: u32 = 0,
+ /// The library file's BLAKE3; zero for a kind compiled in.
+ hash: [32]u8 = @splat(0),
+ caps: Kind.Caps = .{},
+ path: []const u8 = "",
+ /// Empty: taken; else why not ("refused: ...").
+ why: []const u8 = "",
+ exts: []const u8 = "",
+ words: []const LoadedWord = &.{},
+ /// Its `pa_ops` in this process; a replica's update reads none.
+ ops: usize = 0,
+};
+pub const LoadedWord = struct { name: []const u8, leader: []const u8 = "", arg: u8 = 0, opens: u8 = 0, doc: []const u8 = "" };
+
+/// `ops`'s head as a `Loaded` (its words borrowed from it, in `words`).
+pub fn loadedOf(ops: *const Kind.Ops, path: []const u8, hash: [32]u8, words: *[64]LoadedWord) Loaded {
+ const h = &ops.head;
+ const head_words = if (h.nwords == 0 or h.words == null) &.{} else h.words[0..@min(h.nwords, words.len)];
+ for (head_words, words[0..head_words.len]) |*w, *out| out.* = .{
+ .name = if (w.name == null) "" else std.mem.span(w.name),
+ .leader = if (w.leader == null) "" else std.mem.span(w.leader),
+ .arg = w.arg,
+ .opens = w.opens,
+ .doc = if (w.doc == null) "" else std.mem.span(w.doc),
+ };
+ return .{
+ .name = std.mem.sliceTo(&h.name, 0),
+ .version = h.version,
+ .abi = h.abi,
+ .hash = hash,
+ .path = path,
+ .exts = if (h.exts == null) "" else std.mem.span(h.exts),
+ .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 },
+ };
+}
+
+/// The kind that opens `path`, by its extension (any case).
+pub fn forPath(r: *const Kind.Registry, path: []const u8) ?u8 {
+ const dot = std.mem.lastIndexOfScalar(u8, path, '.') orelse return null;
+ if (std.mem.indexOfScalarPos(u8, path, dot, '/') != null) return null;
+ for (r.exts[0..r.nexts]) |*e| if (std.ascii.eqlIgnoreCase(e.ext.slice(), path[dot..])) return e.kind;
+ return null;
+}
+
+pub fn word(r: *const Kind.Registry, name: []const u8) ?*const Kind.Word {
+ for (r.words[0..r.nwords]) |*w| if (std.mem.eql(u8, w.name.slice(), name)) return w;
+ return null;
+}
+
+/// The word whose leader path is `keys`, or whether one has a path
+/// `keys` begins (`prefix`).
+pub fn leader(r: *const Kind.Registry, keys: []const u8) union(enum) { none, prefix, word: *const Kind.Word } {
+ var begun = false;
+ for (r.words[0..r.nwords]) |*w| {
+ if (w.leader.len == 0) continue;
+ if (std.mem.eql(u8, w.leader.slice(), keys)) return .{ .word = w };
+ if (std.mem.startsWith(u8, w.leader.slice(), keys)) begun = true;
+ }
+ return if (begun) .prefix else .none;
+}
+
+/// `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 {
+ 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 };
+ @memcpy(e.name[0..@min(15, l.name.len)], l.name[0..@min(15, l.name.len)]);
+ e.path.set(l.path);
+ const why = if (l.why.len > 0) l.why else r.collides(l, except, &e.why);
+ if (why) |w| {
+ e.why.set(w);
+ e.refused = true;
+ r.n += 1;
+ return e.why.slice();
+ }
+ e.why.set(if (e.compiledIn()) "static" else "loaded");
+ r.ops[r.n] = if (tape.replaying()) null else @ptrFromInt(l.ops);
+ for (l.words) |w| {
+ r.words[r.nwords] = .{ .kind = r.n, .arg = w.arg, .opens = w.opens };
+ r.words[r.nwords].name.set(w.name);
+ r.words[r.nwords].leader.set(w.leader);
+ r.words[r.nwords].doc.set(w.doc);
+ r.nwords += 1;
+ }
+ var it = std.mem.tokenizeScalar(u8, l.exts, ' ');
+ while (it.next()) |ext| : (r.nexts += 1) {
+ r.exts[r.nexts] = .{ .kind = r.n };
+ r.exts[r.nexts].ext.set(ext);
+ }
+ r.n += 1;
+ return null;
+}
+
+pub fn collides(r: *const Kind.Registry, l: Loaded, except: []const u8, why: *Kind.Buf(128)) ?[]const u8 {
+ const mine = struct {
+ fn of(rr: *const Kind.Registry, k: u8, ex: []const u8) bool {
+ return ex.len > 0 and std.mem.eql(u8, rr.kinds[k].nameOf(), ex);
+ }
+ }.of;
+ if (l.name.len == 0 or l.name.len > 15) return "refused: its name is empty or longer than 15 bytes";
+ for (l.name) |ch| if (!std.ascii.isAlphanumeric(ch) and ch != '_' and ch != '-') return "refused: its name is not a word";
+ if (r.byName(l.name)) |k| if (!mine(r, k, except)) return why.print("refused: kind {s} is here already", .{l.name});
+ if (r.nwords + l.words.len > r.words.len) return "refused: no room for its words";
+ for (l.words, 0..) |w, i| {
+ if (w.name.len == 0 or w.name.len > 32) return "refused: a word's name is empty or longer than 32 bytes";
+ if (coreWord(w.name)) return why.print("refused: word {s} is the core's", .{w.name});
+ if (r.word(w.name)) |o| if (!mine(r, o.kind, except)) return why.print("refused: word {s} is {s}'s", .{ w.name, r.kinds[o.kind].nameOf() });
+ for (l.words[0..i]) |before| if (std.mem.eql(u8, before.name, w.name)) return why.print("refused: word {s} twice", .{w.name});
+ if (w.leader.len > 8) return why.print("refused: leader SPC {s} is longer than 8 keys", .{w.leader});
+ if (w.leader.len > 0) {
+ if (@import("keys.zig").leaderTaken(w.leader)) return why.print("refused: leader SPC {s} is taken", .{w.leader});
+ for (r.words[0..r.nwords]) |o| if (o.leader.len > 0 and !mine(r, o.kind, except) and (std.mem.startsWith(u8, w.leader, o.leader.slice()) or std.mem.startsWith(u8, o.leader.slice(), w.leader)))
+ return why.print("refused: leader SPC {s} is taken", .{w.leader});
+ }
+ }
+ var it = std.mem.tokenizeScalar(u8, l.exts, ' ');
+ var n: usize = 0;
+ while (it.next()) |ext| : (n += 1) {
+ if (ext.len < 2 or ext[0] != '.' or ext.len > 16) return why.print("refused: extension {s} is no .ext", .{ext});
+ if (r.forPath(ext)) |o| if (!mine(r, o, except)) return why.print("refused: extension {s} is {s}'s", .{ ext, r.kinds[o].nameOf() });
+ }
+ if (r.nexts + n > r.exts.len) return "refused: no room for its extensions";
+ return null;
+}
+
+/// Kind `k` out, its words and extensions with it.
+pub fn drop(r: *Kind.Registry, k: u8) void {
+ var n: u8 = 0;
+ for (r.words[0..r.nwords]) |w| if (w.kind != k) {
+ r.words[n] = w;
+ if (w.kind > k) r.words[n].kind -= 1;
+ n += 1;
+ };
+ r.nwords = n;
+ n = 0;
+ for (r.exts[0..r.nexts]) |e| if (e.kind != k) {
+ r.exts[n] = e;
+ if (e.kind > k) r.exts[n].kind -= 1;
+ n += 1;
+ };
+ r.nexts = n;
+ std.mem.copyForwards(Kind.Entry, r.kinds[k .. r.n - 1], r.kinds[k + 1 .. r.n]);
+ std.mem.copyForwards(?*const Kind.Ops, r.ops[k .. r.n - 1], r.ops[k + 1 .. r.n]);
+ r.n -= 1;
+ r.ops[r.n] = null;
+}
+
+fn coreWord(name: []const u8) bool {
+ const builtins = @import("builtins.zig");
+ 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
+/// <name>` (refused while a pane of its kind is open), `Addon reload
+/// <name>` (its panes kept through it, as their dumps); bare, the kinds
+/// this session has.
+pub fn run(p: *Pardes, id: usize, arg: ?[]const u8) void {
+ const a = std.mem.trim(u8, arg orelse "", " \t");
+ var said: [256]u8 = undefined;
+ if (a.len == 0) {
+ var w: std.Io.Writer = .fixed(&said);
+ w.writeAll("addons:") catch {};
+ for (p.kinds.kinds[0..p.kinds.n]) |*e| if (!e.refused) w.print(" {s} {d}{s}", .{ e.nameOf(), e.version, if (e.compiledIn()) " (compiled in)" else "" }) catch {};
+ return p.setMessage(id, w.buffered());
+ }
+ const takes = "Addon takes load <path>, unload <name> or reload <name>";
+ const sp = std.mem.indexOfAny(u8, a, " \t") orelse return p.reportFailure(id, takes);
+ const op = std.meta.stringToEnum(enum { load, unload, reload }, a[0..sp]) orelse return p.reportFailure(id, takes);
+ const rest = std.mem.trim(u8, a[sp..], " \t");
+ var target: [pardes.effect_path_cap]u8 = undefined;
+ if (op == .load) {
+ const dir = std.mem.trimEnd(u8, if (p.panes[id]) |pane| Pardes.paneDir(pane) else p.sessionDir(), "/");
+ const path = if (std.fs.path.isAbsolute(rest)) rest else std.fmt.bufPrint(&target, "{s}/{s}", .{ dir, rest }) catch return p.reportFailure(id, "Addon: that path is too long");
+ if (path.len > pardes.effect_path_cap) return p.reportFailure(id, "Addon: that path is too long");
+ return p.emit(.{ .addon = .{ .op = .load, .pane = @intCast(id), .arg = .from(path) } });
+ }
+ const k = p.kinds.byName(rest) orelse return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon {t}: no addon brought kind {s}", .{ op, rest }) catch takes);
+ const e = &p.kinds.kinds[k];
+ if (e.compiledIn()) return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon {t}: {s} is compiled in", .{ op, rest }) catch takes);
+ if (op == .reload) return p.emit(.{ .addon = .{ .op = .reload, .pane = @intCast(id), .arg = .from(e.path.slice()) } });
+ const n = openPanes(p, rest);
+ if (n > 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon unload: {d} {s} pane{s} open", .{ n, rest, if (n == 1) " is" else "s are" }) catch takes);
+ const hash = e.hash;
+ const gone = std.fmt.bufPrint(&said, "Addon unload: {s} is gone", .{rest}) catch "Addon unload";
+ p.kinds.drop(k);
+ release(p, hash);
+ p.setMessage(id, gone);
+}
+
+fn release(p: *Pardes, hash: [32]u8) void {
+ p.emit(.{ .addon = .{ .op = .release, .pane = 0, .arg = .from(&hash) } });
+}
+
+pub fn openPanes(p: *const Pardes, name: []const u8) usize {
+ var n: usize = 0;
+ for (p.panes) |slot| if (slot) |pane| if (pane.addon) |s| if (s.live and s.is(name)) {
+ n += 1;
+ };
+ return n;
+}
+
+/// The `addon` event: what a library brought taken in (or listed
+/// refused), or a reload's swap; the panes that waited for it open.
+pub fn landed(p: *Pardes, op: Op, pane: u8, l: Loaded) void {
+ const id = if (pane < p.panes.len and p.panes[pane] != null) pane else p.active;
+ var said: [320]u8 = undefined;
+ const base = std.fs.path.basename(l.path);
+ if (op == .reload) {
+ var why: Why = .{};
+ if (!swap(p, l, &why)) {
+ if (l.why.len == 0) release(p, l.hash);
+ return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon reload: {s}: {s}", .{ base, why.slice() }) catch "Addon reload: refused");
+ }
+ const n = openPanes(p, l.name);
+ return p.setMessage(id, std.fmt.bufPrint(&said, "Addon reload: {s} {d}, {d} pane{s}", .{ l.name, l.version, n, if (n == 1) "" else "s" }) catch "Addon reload");
+ }
+ // A load of a path refused before tries it afresh.
+ var i: u8 = 0;
+ 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 (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");
+ }
+ const n = reopenWaiting(p, l.name);
+ p.setMessage(id, std.fmt.bufPrint(&said, "Addon load: {s} {d}{s}", .{ l.name, l.version, if (n > 0) ", its panes back" else "" }) catch "Addon load");
+}
+
+/// The panes of kind `name` that waited for it (placeholders), opened.
+pub fn reopenWaiting(p: *Pardes, name: []const u8) usize {
+ const k = p.kinds.byName(name) orelse return 0;
+ var n: usize = 0;
+ for (p.panes, 0..) |slot, id| if (slot) |pane| if (pane.addon) |s| if (!s.live and s.is(name)) {
+ s.ops = p.kinds.ops[k];
+ s.gen = p.kinds.kinds[k].gen;
+ s.refused = .{};
+ if (Kind.reopen(p, id, s, null)) n += 1;
+ };
+ return n;
+}
+
+/// `Addon reload`: the kind at `l.path` replaced by what `l` brought, or
+/// why not, nothing changed. Everything is checked first: its head (the
+/// host's), its collisions (its old self looked past), its name the same,
+/// and every open pane of the kind opened anew in a fresh arena from the
+/// old state's record (its `load`, else its place and its words); only
+/// then are the old states closed and the panes swapped, at a new
+/// generation (a job the old one asked for is dropped as it answers).
+fn swap(p: *Pardes, l: Loaded, why: *Why) bool {
+ const k = for (p.kinds.kinds[0..p.kinds.n], 0..) |*e, i| {
+ if (!e.refused and !e.compiledIn() and std.mem.eql(u8, e.path.slice(), l.path)) break @as(u8, @intCast(i));
+ } else return fail(why, "no addon was loaded from it", .{});
+ const name = p.kinds.kinds[k].nameOf();
+ if (l.why.len > 0) return fail(why, "{s}", .{l.why});
+ var scratch: Kind.Buf(128) = .{};
+ 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 gen = p.kinds.kinds[k].gen +% 1;
+ var fresh: [pardes.MAX_PANES]?*Kind.Slot = @splat(null);
+ defer for (fresh) |f| if (f) |s| {
+ const gpa = s.gpa;
+ s.free();
+ gpa.destroy(s);
+ };
+ 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 };
+ 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.
+ if (old_ops) |o| {
+ var place: [32]u8 = undefined;
+ n.where_text.set(Kind.where(s, &place));
+ if (o.dump != null and l.caps.load) n.record = dump.slotRecord(s) catch return fail(why, "out of memory", .{});
+ }
+ if (!Kind.reopen(p, id, n, null)) return fail(why, "refused: a pane of it would not open again: {s}", .{n.refused.slice()});
+ };
+ // Every pane opened: the old states go, the new take their slots.
+ for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |s| if (fresh[id]) |n| {
+ if (old_ops) |o| if (o.close) |f| f(s.st);
+ for (&s.jobs) |*j| if (j.*) |job| {
+ s.gpa.free(job.in);
+ j.* = null;
+ };
+ s.arena.deinit();
+ s.arena = n.arena;
+ n.arena = .init(n.gpa);
+ if (@import("builtin").mode == .debug) std.mem.swap(@TypeOf(s.used), &s.used, &n.used);
+ s.st = n.st;
+ s.ops = new_ops;
+ s.version = l.version;
+ s.gen = gen;
+ s.next_tick = n.next_tick;
+ n.st = null;
+ };
+ const old_hash = p.kinds.kinds[k].hash;
+ p.kinds.drop(k);
+ _ = p.kinds.take(l, "");
+ p.kinds.kinds[p.kinds.n - 1].gen = gen;
+ release(p, old_hash);
+ return true;
+}
+
+fn fail(why: *Why, comptime fmt: []const u8, args: anytype) bool {
+ _ = why.print(fmt, args);
+ return false;
+}
+
+/// /addons: `<kind> <version> <abi> <panes open> <path> <blake3>
+/// static|loaded|refused: why` a line (`-` for no path, no hash).
+pub fn list(p: *const Pardes, out: *std.ArrayList(u8), gpa: std.mem.Allocator) !void {
+ for (p.kinds.kinds[0..p.kinds.n]) |*e| {
+ const name = if (e.nameOf().len > 0) e.nameOf() else std.fs.path.basename(e.path.slice());
+ try out.print(gpa, "{s} {d} {d}.{d} {d} {s} ", .{ name, e.version, e.abi >> 16, e.abi & 0xffff, if (e.refused) 0 else openPanes(p, name), if (e.path.len > 0) e.path.slice() else "-" });
+ if (e.compiledIn()) try out.append(gpa, '-') else try out.print(gpa, "{x}", .{e.hash});
+ try out.print(gpa, " {s}\n", .{e.why.slice()});
+ }
+}
+
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();
@@ -306,17 +636,17 @@ test "a load that never finished is skipped at the next start, and every start a
var dir_buf: [4096]u8 = undefined;
const dir = try std.fmt.bufPrint(&dir_buf, "{s}/addons", .{data});
for (0..2) |_| {
+ nfound = 0;
scanDir(data, dir, crashedPath(data).slice());
- const last = Kind.kinds()[Kind.kinds().len - 1];
- try std.testing.expectEqualStrings(lib, last.path.slice());
- try std.testing.expect(std.mem.startsWith(u8, last.why.slice(), "skipped: it crashed its last load"));
- Kind.forgetRefused(lib);
+ try std.testing.expectEqualStrings(lib, found[0].path);
+ try std.testing.expect(std.mem.startsWith(u8, found[0].why, "skipped: it crashed its last load"));
}
+ nfound = 0;
// Asked for by name, it is tried again (and refused here for what it is).
+ var words: [64]LoadedWord = undefined;
var why: Why = .{};
- try std.testing.expect(!loadMarked(data, lib, &why));
- try std.testing.expect(std.mem.indexOf(u8, Kind.kinds()[Kind.kinds().len - 1].why.slice(), "skipped") == null);
+ const l = read(data, lib, &words, &why);
+ try std.testing.expect(l.why.len > 0 and std.mem.indexOf(u8, l.why, "skipped") == null);
try std.testing.expectError(error.FileNotFound, tmp.dir.access(std.testing.io, "addon-crashed", .{}));
try std.testing.expectError(error.FileNotFound, tmp.dir.access(std.testing.io, "addon-loading", .{}));
- Kind.forgetRefused(lib);
}