summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--addons/fake/fake.c28
-rw-r--r--addons/fake/fake.out11
-rw-r--r--build.zig17
-rw-r--r--src/Addon.zig652
-rw-r--r--src/Core.zig27
-rw-r--r--src/Effect.zig14
-rw-r--r--src/Event.zig9
-rw-r--r--src/Kind.zig1388
-rw-r--r--src/Output.zig6
-rw-r--r--src/addon_test.zig167
-rw-r--r--src/builtins.zig2
-rw-r--r--src/dump.zig86
-rw-r--r--src/echo.zig2
-rw-r--r--src/exec.zig51
-rw-r--r--src/gui/gui.zig14
-rw-r--r--src/host_io.zig104
-rw-r--r--src/image.zig12
-rw-r--r--src/keys.zig6
-rw-r--r--src/kinds/pardes_addon.h117
-rw-r--r--src/look.zig7
-rw-r--r--src/macos.zig4
-rw-r--r--src/mouse.zig2
-rw-r--r--src/ninep/ctl.zig34
-rw-r--r--src/ninep/tree.zig2
-rw-r--r--src/replica.zig154
-rw-r--r--src/screen_state.zig197
-rw-r--r--src/tape.zig4
-rw-r--r--src/tui/tui.zig21
-rw-r--r--src/wire.zig6
-rw-r--r--test/behaviour/addons.zig46
-rw-r--r--test/fs_namespace.zig4
-rw-r--r--test/mode.zig2
-rw-r--r--test/panes.zig10
-rw-r--r--test/replicas.zig3
-rw-r--r--test/session.zig23
35 files changed, 1975 insertions, 1257 deletions
diff --git a/addons/fake/fake.c b/addons/fake/fake.c
index ad0f83ee..818c8ea3 100644
--- a/addons/fake/fake.c
+++ b/addons/fake/fake.c
@@ -30,7 +30,7 @@ typedef struct {
static int init(const pa_api *a) {
api = a;
- return 0;
+ return PA_OK;
}
static void *open(const char *path, const uint8_t *bytes, uint64_t len) {
@@ -45,8 +45,9 @@ static void *open(const char *path, const uint8_t *bytes, uint64_t len) {
static void close(void *st) { (void)st; }
-static uint64_t tick(void *st, uint64_t now) {
+static uint64_t tick(void *st, uint64_t now, uint32_t flags) {
fake *f = st;
+ (void)flags;
f->ticks++;
return f->ticks < 3 ? now + 100000000u : 0; /* ten a second, three times */
}
@@ -63,13 +64,13 @@ static void paint(void *st, const pa_window *w, pa_paint *out) {
fake *f = st;
char line[48] = "count ";
num(line + 6, f ? f->count : 0);
- pa_put(w, 0, 0, line, PA_INDEX(2), PA_DEFAULT);
- if (f && f->last[0]) pa_put(w, 1, 0, f->last, PA_DEFAULT, PA_DEFAULT);
+ pa_put(w, 0, 0, line, PA_INDEX(2), PA_DEFAULT, 0);
+ if (f && f->last[0]) pa_put(w, 1, 0, f->last, PA_DEFAULT, PA_DEFAULT, 0);
const char *p = "fake";
for (out->prefix_len = 0; p[out->prefix_len]; out->prefix_len++) out->prefix[out->prefix_len] = p[out->prefix_len];
if (f && out->cap > 0 && w->rows > 2) {
pa_placement *pl = &out->placements[out->n++];
- *pl = (pa_placement){.rgba = f->pixels, .w = 2, .h = 2, .stride = 8, .src = {0, 0, 2, 2}, .dst = {0, 2, 2, 1}, .key = f->count};
+ *pl = (pa_placement){.rgba = f->pixels, .w = 2, .h = 2, .src = {0, 0, 2, 2}, .dst = {0, 2, 2, 1}, .key = f->count};
}
}
@@ -96,10 +97,7 @@ static uint64_t body_len(void *st) { (void)st; return 6; }
static uint32_t body(void *st, uint64_t off, char *buf, uint32_t n) {
(void)st;
- const char *text = "fake\n\n";
- uint32_t k = 0;
- while (off + k < 6 && k < n) { buf[k] = text[off + k]; k++; }
- return k;
+ return pa_copy("fake\n\n", 6, off, buf, n);
}
static int address(void *st, const char *text, uint32_t n) {
@@ -131,9 +129,9 @@ static int ctl(void *st, const char *line, uint32_t n, char *reply, uint32_t cap
unsigned by = 0;
for (uint32_t i = 9; i < n && line[i] >= '0' && line[i] <= '9'; i++) by = by * 10 + (unsigned)(line[i] - '0');
f->count += by ? by : 1;
- return 0;
+ return PA_OK;
}
- if (n >= 7 && line[4] == 'J') return api->job("abc", 3) ? 0 : 1;
+ if (n >= 7 && line[4] == 'J') return api->job("abc", 3) ? PA_OK : 1;
const char *no = "no such fake word";
uint32_t k = 0;
while (no[k] && k + 1 < cap) { reply[k] = no[k]; k++; }
@@ -174,14 +172,14 @@ static int load(void *st, const uint8_t *buf, uint32_t n, uint32_t version) {
fake *f = st;
if (n != 4 || version > 1) return 1;
f->count = (unsigned)buf[0] | (unsigned)buf[1] << 8 | (unsigned)buf[2] << 16 | (unsigned)buf[3] << 24;
- return 0;
+ return PA_OK;
}
#endif
static const pa_word words[] = {
- {.size = sizeof(pa_word), .name = FAKE_WORD, .leader = "zf", .opens = 1, .doc = "a fake pane, counting"},
- {.size = sizeof(pa_word), .name = "FakeBump", .arg = PA_ARG_OPTIONAL, .doc = "add to the count"},
- {.size = sizeof(pa_word), .name = "FakeJob", .doc = "count a job's answer"},
+ PA_WORD(FAKE_WORD, "zf", PA_ARG_NONE, 1, "a fake pane, counting"),
+ PA_WORD("FakeBump", 0, PA_ARG_OPTIONAL, 0, "add to the count"),
+ PA_WORD("FakeJob", 0, PA_ARG_NONE, 0, "count a job's answer"),
};
const pa_ops pardes_addon = {
diff --git a/addons/fake/fake.out b/addons/fake/fake.out
index 40b958f8..7a09e97a 100644
--- a/addons/fake/fake.out
+++ b/addons/fake/fake.out
@@ -1,5 +1,6 @@
> seed 7
> size 60 20
+window 58x8
tag fake
|count 0
|
@@ -19,7 +20,6 @@ tag fake
|
|
|
-|
placement 2x2 src 0,0 2x2 over 0,2 2x1 key fdcaab9076ad067e
> click 3 1
tag fake
@@ -30,7 +30,6 @@ tag fake
|
|
|
-|
placement 2x2 src 0,0 2x2 over 0,2 2x1 key 71e39225f0cec2a5
> FakeBump 10
tag fake
@@ -41,19 +40,17 @@ tag fake
|
|
|
-|
placement 2x2 src 0,0 2x2 over 0,2 2x1 key 54740e23065232cf
> FakeJob
tag fake
-|count 13
+|count 19
|m3
|
|
|
|
|
-|
-placement 2x2 src 0,0 2x2 over 0,2 2x1 key 54740e23065232cf
+placement 2x2 src 0,0 2x2 over 0,2 2x1 key 540d6c1b4eee5716
> tick 100
tag fake
|count 19
@@ -63,7 +60,6 @@ tag fake
|
|
|
-|
placement 2x2 src 0,0 2x2 over 0,2 2x1 key 540d6c1b4eee5716
> settle
tag fake
@@ -74,5 +70,4 @@ tag fake
|
|
|
-|
placement 2x2 src 0,0 2x2 over 0,2 2x1 key 540d6c1b4eee5716
diff --git a/build.zig b/build.zig
index 30cb80d8..90a7aa9e 100644
--- a/build.zig
+++ b/build.zig
@@ -1533,7 +1533,7 @@ pub fn build(b: *std.Build) void {
// wrong, which the loader must refuse (test/behaviour/addons.zig).
for ([_]struct { []const u8, []const u8 }{
.{ "fake_addon", "-DFAKE_PLAIN" },
- .{ "fake_addon_abi", "-DFAKE_ABI=0x20000" },
+ .{ "fake_addon_abi", "-DFAKE_ABI=0x30000" },
.{ "fake_addon_no_export", "-DFAKE_NO_EXPORT" },
.{ "fake_addon_core_word", "-DFAKE_WORD=\"Save\"" },
.{ "fake_addon_no_dump", "-DFAKE_NO_DUMP" },
@@ -1560,18 +1560,19 @@ pub fn build(b: *std.Build) void {
const behaviour_tests = b.addTest(.{ .root_module = behaviour_module, .filters = test_filters });
const behaviour_test_step = b.step("behaviour-test", "run the behavioural tests, through the 9P tree alone (test/behaviour/)");
_ = units.add(b, behaviour_tests, behaviour_test_step);
- // An addon's own test, the template's: `--addon-test` over the fake
- // and its script, compared with what it printed when written.
- if (fake_addon_lib) |fake_lib| {
+ // An addon's own test: `--addon-test` over the fake (the template)
+ // with its script, compared with what it printed when written.
+ const addon_test_step = b.step("addon-test", "pardes --addon-test over the fake addon, against addons/fake/fake.out");
+ for ([_]struct { *std.Build.Step.Compile, []const u8 }{.{ fake_addon_lib.?, "addons/fake/fake" }}) |t| {
const run_addon_test = b.addRunArtifact(exe);
run_addon_test.addArg("--addon-test");
- run_addon_test.addArtifactArg(fake_lib);
- run_addon_test.addFileArg(b.path("addons/fake/fake.script"));
+ run_addon_test.addArtifactArg(t[0]);
+ run_addon_test.addFileArg(b.path(b.fmt("{s}.script", .{t[1]})));
const compare = b.addSystemCommand(&.{ "diff", "-u" });
- compare.addFileArg(b.path("addons/fake/fake.out"));
+ compare.addFileArg(b.path(b.fmt("{s}.out", .{t[1]})));
compare.addFileArg(run_addon_test.captureStdOut(.{}));
behaviour_test_step.dependOn(&compare.step);
- b.step("addon-test", "pardes --addon-test over the fake addon, against addons/fake/fake.out").dependOn(&compare.step);
+ addon_test_step.dependOn(&compare.step);
}
// The 9P monkey at T1 (test/monkey9p.zig): random requests against
// an in-process session, the documented rules checked after each.
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);
}
diff --git a/src/Core.zig b/src/Core.zig
index 5c68a5af..bbacc536 100644
--- a/src/Core.zig
+++ b/src/Core.zig
@@ -91,6 +91,9 @@ const Pardes = @This();
gpa: std.mem.Allocator,
image_gpa: std.mem.Allocator,
+/// Its kinds of pane (Kind.zig): the compiled-in ones, then what addons
+/// brought; the data both cores hold, the code only the authority.
+kinds: pardes.Kind.Registry = .{},
pdf_gpa: std.mem.Allocator,
tree_sitter_gpa: std.mem.Allocator,
opts: Options,
@@ -460,6 +463,7 @@ pub fn create(gpa: std.mem.Allocator, opts: Options) !*Pardes {
p.* = .{
.gpa = gpa,
.image_gpa = opts.image_allocator orelse gpa,
+ .kinds = .started(opts.addons),
.pdf_gpa = opts.pdf_allocator orelse gpa,
.tree_sitter_gpa = opts.tree_sitter_allocator orelse gpa,
.opts = opts,
@@ -508,7 +512,7 @@ fn initSession(gpa: std.mem.Allocator, opts: Options) !*Pardes {
const opened = initial_doc: {
if (comptime pdf_enabled) if (look.isPdfPath(path))
break :initial_doc pardes.panes.Pdf.openPane(p, 0, path, opts.file_line, .last);
- if (pardes.Kind.forPath(path)) |kind|
+ if (p.kinds.forPath(path)) |kind|
break :initial_doc pardes.Kind.openFile(p, 0, kind, path);
break :initial_doc pardes.panes.File.open(p, 0, path, opts.file_line);
};
@@ -1192,7 +1196,7 @@ pub fn dropPendingWrite(p: *Pardes, id: usize) void {
pub fn postEvent(p: *Pardes, ev: Event) void {
switch (ev) {
.key => |k| std.debug.assert(k.text.len == 0),
- .output, .paste, .lsp_resp, .pipe_resp, .search, .file_changed, .command, .addon => unreachable,
+ .output, .paste, .lsp_resp, .pipe_resp, .search, .file_changed, .command, .addon, .job => unreachable,
else => {},
}
if (p.in_len == p.in_q.len) return;
@@ -1623,7 +1627,8 @@ pub fn update(p: *Pardes, ev: Event) void {
if (focused) if (p.host.vtable.newer_build) |f| if (f(p.host.ctx)) p.setMessage(p.active, newer_build_words);
},
.handed => |h| p.handover.finish(p, h.seq, h.to, h.failure),
- .addon => |a| pardes.Kind.landed(p, a.op, a.pane, a.ok, a.text),
+ .addon => |a| pardes.Addon.landed(p, a.op, a.pane, a.kind.*),
+ .job => |j| pardes.Kind.jobDone(p, j.pane, j.serial, j.token, j.gen, j.bytes),
.frontend => |f| {
p.frontend = f;
},
@@ -2166,6 +2171,8 @@ pub const Options = struct {
recent_import: ?[]const u8 = null,
startup_config_path: ?[]const u8 = null,
image_allocator: ?std.mem.Allocator = null,
+ /// What the host's scan of the addon directories found (Addon.scan).
+ addons: []const pardes.Kind.Loaded = &.{},
pdf_allocator: ?std.mem.Allocator = null,
tree_sitter_allocator: ?std.mem.Allocator = null,
/// Where each frame's Surface text is built. Hosts pass a purpose-built
@@ -2471,7 +2478,7 @@ test "Filter is ordered after Del and toggles only its terminal or +Pager pane"
try testing.expect(!first.tty_filter);
const doc_id = p.freeSlot().?;
- const doc = try pardes.Kind.open(p, doc_id, &image.kind, "/tmp/filter-inert.ppm", "");
+ const doc = try pardes.Kind.open(p, doc_id, pardes.Kind.image, "/tmp/filter-inert.ppm", "");
try testing.expect(!doc.tty_filter);
try testing.expect(p.executeBuiltinLine(doc_id, "Filter"));
try testing.expect(!doc.tty_filter);
@@ -2641,7 +2648,7 @@ test "unknown Exec from a document runs as a command pane in its directory, type
// A terminal free at a prompt in the same directory is not borrowed.
p.setCwd(0, "/tmp/pardes-image-dir");
- const image_doc = try pardes.Kind.open(p, 1, &image.kind, "/tmp/pardes-image-dir/pic.ppm", null);
+ const image_doc = try pardes.Kind.open(p, 1, pardes.Kind.image, "/tmp/pardes-image-dir/pic.ppm", null);
try std.testing.expectEqualStrings("/tmp/pardes-image-dir", Pardes.paneDir(image_doc));
const dst = exec.execute(p, 1, "echo image-fallback") orelse return error.ExecFoundNowhereToRun;
try std.testing.expect(dst != 0 and dst != 1);
@@ -2695,7 +2702,7 @@ test "a command of several lines keeps its tag one row, and its echo does not st
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 });
defer p.deinit();
while (p.nextEffect()) |_| {}
- _ = try pardes.Kind.open(p, 1, &image.kind, "/tmp/pic.ppm", null);
+ _ = try pardes.Kind.open(p, 1, pardes.Kind.image, "/tmp/pic.ppm", null);
const two = "def greet(name):\n return name\n\nprint(greet(\"x\"))\n";
const dst = exec.execute(p, 1, two) orelse return error.NoCommandPane;
while (p.nextEffect()) |_| {}
@@ -3043,7 +3050,7 @@ test "a command pane shows how its command ended, and the next command there run
defer p.deinit();
while (p.nextEffect()) |_| {}
p.setCwd(0, "/tmp/pardes-cmd");
- _ = try pardes.Kind.open(p, 1, &image.kind, "/tmp/pardes-cmd/pic.ppm", null);
+ _ = try pardes.Kind.open(p, 1, pardes.Kind.image, "/tmp/pardes-cmd/pic.ppm", null);
const dst = exec.execute(p, 1, "make -j8") orelse return error.NoCommandPane;
while (p.nextEffect()) |_| {}
@@ -3143,7 +3150,7 @@ test "image dump restores source bytes renderer choices and exact custom tail" {
p.panes[0] = null;
const source = "\x00embedded image bytes\xff";
- const pane = try pardes.Kind.open(p, 0, &image.kind, "/missing/restored-image.ppm", source);
+ const pane = try pardes.Kind.open(p, 0, pardes.Kind.image, "/missing/restored-image.ppm", source);
pane.addon.?.art = .{ .glyph = true, .palette = .terminal, .ascii = false };
if (pane.tag.own) |own| p.gpa.free(own);
pane.tag.own = try p.gpa.dupe(u8, " Keep Del");
@@ -3166,8 +3173,8 @@ test "image dump restores source bytes renderer choices and exact custom tail" {
try dump.dumpState(restored);
const again = try dump.load(gpa, .{ .tty_only = true }, restored.dump_out.?, null);
defer again.deinit();
- const again_slot = again.panes[0].?.addon.?;
- try std.testing.expectEqualSlices(u8, source, kept[0..image.kind.dump.?(again_slot.st, &kept, kept.len)]);
+ // Loaded as a replica loads it: a placeholder holding the record.
+ try std.testing.expectEqualSlices(u8, source, again.panes[0].?.addon.?.record.?);
}
test "a terminal its program gave back takes command lines again" {
diff --git a/src/Effect.zig b/src/Effect.zig
index 3ec87ff1..5fc5f970 100644
--- a/src/Effect.zig
+++ b/src/Effect.zig
@@ -76,7 +76,10 @@ pub const Effect = union(enum) {
/// An addon loaded, let go, or let go and loaded again (Addon.zig):
/// `arg` its path, or for unload its kind's name. Answered by an
/// `addon` event.
- addon: struct { op: @import("Kind.zig").AddonOp, pane: u8, arg: Buf(effect_path_cap) },
+ addon: struct { op: @import("Addon.zig").Op, pane: u8, arg: Buf(effect_path_cap) },
+ /// A kind's job for the host's workers (host_io.Job), answered by a
+ /// `job` event.
+ job: struct { pane: u8, serial: u32, token: u32 },
quit,
pub fn Buf(comptime n: usize) type {
@@ -235,8 +238,11 @@ pub fn perform(p: *Pardes, e: Effect) void {
f(p.host.ctx)
else
p.reportError(d.pane, "detach", error.NotAttached),
- // dlopen is the same on every host, so none has a method of its own.
- .addon => |a| @import("Addon.zig").perform(p, a.op, a.pane, a.arg.slice()),
+ // dlopen is the same on every host, so none has a method of its
+ // own; a replica's core holds no library (what it brought comes in
+ // the event this taped).
+ .addon => |a| if (!tape.replaying()) @import("Addon.zig").perform(p, a.op, a.pane, a.arg.slice()),
+ .job => |j| if (v.job) |f| f(p.host.ctx, j.pane, j.serial, j.token) else if (!tape.replaying()) host_io.KindJob.runHere(p, j.pane, j.serial, j.token),
// the loop's own condition; a host tears down after its own loop
.quit => p.quit = true,
}
@@ -609,7 +615,7 @@ test "Save reaches every tagline with text behind it and no other" {
// An image's bytes on disk already are what they are: nothing of the
// pane's own is unwritten, so the word is absent and inert.
const img_id = p.freeSlot().?;
- const img = try pardes.Kind.open(p, img_id, &panes.Image.kind, "/tmp/pardes-tag.ppm", "");
+ const img = try pardes.Kind.open(p, img_id, pardes.Kind.image, "/tmp/pardes-tag.ppm", "");
try std.testing.expectEqualStrings(" Term Collapse Del", tagline.curTail(img));
try std.testing.expect(p.executeBuiltinLine(img_id, "Save"));
try std.testing.expect(img.prompt != .save);
diff --git a/src/Event.zig b/src/Event.zig
index 88407c9f..bf27ff69 100644
--- a/src/Event.zig
+++ b/src/Event.zig
@@ -142,9 +142,12 @@ pub const Event = union(enum) {
/// (docs/next-daemon.md, "Phase 6: GUI replica notes"): a window, which
/// draws pictures itself and takes the GUI's own words, or a terminal.
frontend: Frontend,
- /// How the host's `addon` effect went: `text` the kind's name, or
- /// why not (Kind.landed). Borrowed for the step.
- addon: struct { op: @import("Kind.zig").AddonOp, pane: u8, ok: bool, text: []const u8 },
+ /// What the host's `addon` effect read off a library, or why not
+ /// (Addon.landed). Borrowed for the step.
+ addon: struct { op: @import("Addon.zig").Op, pane: u8, kind: *const @import("Kind.zig").Loaded },
+ /// A kind's job answered (host_io.Job), at its kind's generation then.
+ /// Borrowed for the step.
+ job: struct { pane: u8, serial: u32, token: u32, gen: u32, bytes: []const u8 },
tick,
};
diff --git a/src/Kind.zig b/src/Kind.zig
index 7f9284c0..e5332831 100644
--- a/src/Kind.zig
+++ b/src/Kind.zig
@@ -1,16 +1,18 @@
//! A kind of pane that is neither text nor a terminal: a picture, a game,
//! anything an addon (a library, Addon.zig) brings. Its records and
//! functions are the C ABI of src/kinds/pardes_addon.h (translated, the
-//! one copy); this file is the core's side of it: the kinds this process
-//! has (compiled in, or an addon's, with the words and extensions they
-//! bring), and a pane's `Slot`, the state the core keeps round the kind's
-//! own (its path, its art bytes, its jobs, the record of one whose addon
-//! is not here).
+//! one copy); this file is the core's side of it: the kinds a core has
+//! (`Registry`, compiled in or brought, with their words and extensions),
+//! and a pane's `Slot`, the state the core keeps round the kind's own.
//!
-//! The core calls a kind at these places alone: Look and a word open a
-//! pane (`open`), the frame paints it (`draw`), its tag asks its prefix,
-//! keys and the mouse go to it, a tick steps it, a job's answer lands,
-//! 9P reads its body and writes its words, a dump keeps it, and its close.
+//! The core is deterministic and a kind is only the authority's: a
+//! replica's core has the same registry (its words, its extensions) but no
+//! code for a kind a library brought, and draws its panes from the
+//! authority's frames (a Shadow, screen_state.zig). So every call into a
+//! kind whose answer the core acts on is made at a place both cores reach,
+//! and its `Outcome` (the answer, what it said, a Look) is taped: a replica
+//! takes it off the tape. Paint and the reads (body, ctl, a dump) change
+//! nothing and are not taped.
const std = @import("std");
const builtin = @import("builtin");
pub const c = @import("pardes_addon");
@@ -18,323 +20,170 @@ const pardes = @import("pardes.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");
+pub const Loaded = Addon.Loaded;
+pub const LoadedWord = Addon.LoadedWord;
+pub const loadedOf = Addon.loadedOf;
const Placement = surface.Placement;
const Pardes = pardes.Pardes;
const Pane = pardes.panes.Pane;
-const Kind = @This();
pub const Ops = c.pa_ops;
pub const abi_major: u32 = c.PA_ABI_MAJOR;
pub const abi: u32 = c.PA_ABI;
-// The header's own sizes (src/kinds/pardes_addon.h's _Static_asserts), seen
-// from here: the records the core reads are the ones addons are built to.
+// The header's own sizes (its _Static_asserts), seen from here: the
+// records the core reads are the ones addons are built to.
comptime {
if (@sizeOf(usize) == 8) for (.{
.{ c.pa_cell, 20 }, .{ c.pa_window, 24 }, .{ c.pa_rect, 16 }, .{ c.pa_cells, 8 },
- .{ c.pa_placement, 72 }, .{ c.pa_paint, 120 }, .{ c.pa_key, 8 }, .{ c.pa_mouse, 10 },
- .{ c.pa_api, 64 }, .{ c.pa_out, 16 }, .{ c.pa_word, 40 }, .{ c.pa_head, 56 },
+ .{ c.pa_placement, 80 }, .{ c.pa_paint, 120 }, .{ c.pa_key, 8 }, .{ c.pa_mouse, 10 },
+ .{ c.pa_api, 64 }, .{ c.pa_out, 16 }, .{ c.pa_word, 32 }, .{ c.pa_head, 56 },
.{ c.pa_ops, 192 },
}) |r| if (@sizeOf(r[0]) != r[1]) @compileError(std.fmt.comptimePrint("{s} is {d} bytes, the header says {d}", .{ @typeName(r[0]), @sizeOf(r[0]), r[1] }));
}
-// ---- the kinds this process has ----
+// ---- the kinds a core has ----
-/// A kind as /addons lists it: an addon's loaded, one compiled in, or an
-/// addon refused and why.
-pub const Listed = struct {
+pub const max_kinds = 16;
+/// The kinds compiled in come first in every core: the image is kind 0.
+pub const image: u8 = 0;
+
+/// Which of its functions a kind has: what both cores decide by.
+pub const Caps = packed struct(u8) {
+ tick: bool = false,
+ key: bool = false,
+ mouse: bool = false,
+ ctl: bool = false,
+ jobs: bool = false,
+ load: bool = false,
+ pad: u2 = 0,
+};
+
+pub const Entry = struct {
name: [16]u8 = @splat(0),
version: u32 = 0,
abi: u32 = 0,
- /// Null: refused.
- ops: ?*const Ops = null,
- /// Its addon's handle (Addon.zig), none for one compiled in.
- lib: ?*anyopaque = null,
+ hash: [32]u8 = @splat(0),
+ /// One more at each reload: a job's answer of another is dropped.
+ gen: u32 = 0,
+ caps: Caps = .{},
path: Buf(256) = .{},
- /// The library file's BLAKE3, as it was loaded.
- hash: ?[32]u8 = null,
+ /// A library refused, listed with `why`.
+ refused: bool = false,
why: Buf(128) = .{},
- pub fn nameOf(l: *const Listed) []const u8 {
- return std.mem.sliceTo(&l.name, 0);
+ pub fn nameOf(e: *const Entry) []const u8 {
+ return std.mem.sliceTo(&e.name, 0);
}
-};
-
-pub const max_kinds = 16;
-var listed: [max_kinds]Listed = undefined;
-var nlisted: usize = 0;
-
-/// The run-time tail of the builtin registry (builtins.zig keeps the
-/// comptime part): each kind's words, found after the comptime map misses.
-pub const Word = struct { ops: *const Ops, w: *const c.pa_word };
-var words: [64]Word = undefined;
-var nwords: usize = 0;
-var exts: [32]struct { ext: []const u8, ops: *const Ops } = undefined;
-var nexts: usize = 0;
-
-/// The kinds compiled in, registered before any lookup.
-var statics_done = false;
-fn statics() void {
- if (statics_done) return;
- statics_done = true;
- _ = register(&@import("image.zig").kind, "", null);
-}
-
-pub fn kinds() []const Listed {
- statics();
- return listed[0..nlisted];
-}
-
-/// The kind named `name` (`image`), if this process has it.
-pub fn byName(name: []const u8) ?*const Ops {
- for (kinds()) |*l| if (l.ops != null and l.ops != skipping and std.mem.eql(u8, l.nameOf(), name)) return l.ops;
- return null;
-}
-
-/// The kind a reload replaces, which its successor's checks look past.
-var skipping: ?*const Ops = null;
-
-/// The kind that opens `path`, by its extension (any case).
-pub fn forPath(path: []const u8) ?*const Ops {
- statics();
- const dot = std.mem.lastIndexOfScalar(u8, path, '.') orelse return null;
- if (std.mem.indexOfScalarPos(u8, path, dot, '/') != null) return null;
- for (exts[0..nexts]) |e| if (e.ops != skipping and std.ascii.eqlIgnoreCase(e.ext, path[dot..])) return e.ops;
- return null;
-}
-
-/// A kind's word `name`, the tail's linear scan.
-pub fn word(name: []const u8) ?Word {
- statics();
- for (words[0..nwords]) |w| if (w.ops != skipping and std.mem.eql(u8, std.mem.span(w.w.name), name)) return w;
- return null;
-}
-
-pub fn wordsOf() []const Word {
- statics();
- return words[0..nwords];
-}
-
-/// The kind of the word whose leader path is `keys`, or whether one has
-/// a path `keys` begins (`prefix`).
-pub fn leader(keys: []const u8) union(enum) { none, prefix, word: Word } {
- statics();
- var begun = false;
- for (words[0..nwords]) |w| {
- if (w.w.leader == null or w.ops == skipping) continue;
- const path = std.mem.span(w.w.leader);
- if (std.mem.eql(u8, path, keys)) return .{ .word = w };
- if (std.mem.startsWith(u8, path, keys)) begun = true;
+ pub fn compiledIn(e: *const Entry) bool {
+ return std.mem.allEqual(u8, &e.hash, 0);
}
- return if (begun) .prefix else .none;
-}
-
-fn headWords(ops: *const Ops) []const c.pa_word {
- return if (ops.head.nwords == 0 or ops.head.words == null) &.{} else ops.head.words[0..ops.head.nwords];
-}
-
-fn extsOf(ops: *const Ops) []const u8 {
- return if (ops.head.exts == null) "" else std.mem.span(ops.head.exts);
-}
-
-pub fn headName(ops: *const Ops) []const u8 {
- return std.mem.sliceTo(&ops.head.name, 0);
-}
+};
-/// `ops` taken in, its words and extensions with it, or refused in
-/// words: a head this pardes cannot read, another ABI, no paint, a name,
-/// a word, a leader path or an extension already someone's, or its init
-/// failing. Listed either way (/addons), the refusal's words returned.
-/// `taken(word)` says whether the core has a word of that name, and
-/// `leader_taken(path)` a leader path that path begins or is begun by.
-pub fn register(ops: *const Ops, path: []const u8, lib: ?*anyopaque) ?[]const u8 {
- return registerHashed(ops, path, lib, null);
-}
+pub const Word = struct { kind: u8, name: Buf(32) = .{}, leader: Buf(8) = .{}, arg: u8 = 0, opens: u8 = 0, doc: Buf(96) = .{} };
+pub const Ext = struct { kind: u8, ext: Buf(16) = .{} };
-pub fn registerHashed(ops: *const Ops, path: []const u8, lib: ?*anyopaque, hash: ?[32]u8) ?[]const u8 {
- // The kinds compiled in first: the checks below would list them under
- // this one's entry.
- statics();
- if (nlisted == max_kinds) return "refused: no room for another kind";
- const l = &listed[nlisted];
- l.* = .{ .lib = lib, .hash = hash };
- l.path.set(path);
- const why = check(ops, l) orelse {
- l.name = ops.head.name;
- l.name[15] = 0;
- l.version = ops.head.version;
- l.abi = ops.head.abi;
- if (ops.init) |init| if (init(&api) != 0) {
- l.why.set("refused: its init failed");
- nlisted += 1;
- return l.why.slice();
- };
- l.ops = ops;
- addTail(ops);
- l.why.set(if (lib == null) "static" else "loaded");
- nlisted += 1;
- return null;
+/// A core's kinds: the compiled-in ones every core starts with, then what
+/// libraries brought (Addon.zig), as data both cores hold alike; and each
+/// one's code here, which a replica has none of for a library's.
+pub const Registry = struct {
+ pub const replica = .{
+ .local = .{"ops"},
+ .len = .{ .{ "kinds", "n" }, .{ "words", "nwords" }, .{ "exts", "nexts" } },
};
- if (ops.head.size >= @sizeOf(c.pa_head)) {
- l.name = ops.head.name;
- l.name[15] = 0;
- l.version = ops.head.version;
- l.abi = ops.head.abi;
- }
- l.why.set(why);
- nlisted += 1;
- return l.why.slice();
-}
+ kinds: [max_kinds]Entry = undefined,
+ n: u8 = 0,
+ words: [64]Word = undefined,
+ nwords: u8 = 0,
+ exts: [32]Ext = undefined,
+ nexts: u8 = 0,
+ ops: [max_kinds]?*const Ops = @splat(null),
-fn addTail(ops: *const Ops) void {
- for (headWords(ops)) |*w| {
- words[nwords] = .{ .ops = ops, .w = w };
- nwords += 1;
- }
- var it = std.mem.tokenizeScalar(u8, extsOf(ops), ' ');
- while (it.next()) |ext| {
- exts[nexts] = .{ .ext = ext, .ops = ops };
- nexts += 1;
+ /// The kinds compiled in.
+ pub fn init() Registry {
+ var r: Registry = .{};
+ var words: [64]LoadedWord = undefined;
+ _ = r.take(loadedOf(&@import("image.zig").kind, "", @splat(0), &words), "");
+ return r;
}
-}
-
-fn dropTail(ops: *const Ops) void {
- var n: usize = 0;
- for (words[0..nwords]) |w| if (w.ops != ops) {
- words[n] = w;
- n += 1;
- };
- nwords = n;
- n = 0;
- for (exts[0..nexts]) |e| if (e.ops != ops) {
- exts[n] = e;
- n += 1;
- };
- nexts = n;
-}
-fn check(ops: *const Ops, l: *Listed) ?[]const u8 {
- const h = &ops.head;
- if (h.size < @sizeOf(c.pa_head)) return "refused: its head is too short for this pardes";
- if (h.abi >> 16 != abi_major) return l.why.print("refused: wants abi {d}, this pardes speaks {d}", .{ h.abi >> 16, abi_major });
- if (ops.paint == null) return "refused: it has no paint";
- const name = std.mem.sliceTo(&h.name, 0);
- if (name.len == 0 or name.len > 15) return "refused: its name is empty or longer than 15 bytes";
- for (name) |ch| if (!std.ascii.isAlphanumeric(ch) and ch != '_' and ch != '-') return "refused: its name is not a word";
- if (byName(name) != null) return l.why.print("refused: kind {s} is here already", .{name});
- if (h.nwords > 0 and ops.ctl == null) return "refused: it has words and no ctl";
- if ((ops.job_run == null) != (ops.job_done == null)) return "refused: a job needs job_run and job_done";
- if (nwords + h.nwords > words.len) return "refused: no room for its words";
- for (headWords(ops), 0..) |*w, i| {
- if (w.size < @sizeOf(c.pa_word) or w.name == null) return "refused: a word it brings is malformed";
- const wn = std.mem.span(w.name);
- if (wn.len == 0 or wn.len > 32) return "refused: a word's name is empty or longer than 32 bytes";
- if (coreWord(wn)) return l.why.print("refused: word {s} is the core's", .{wn});
- if (word(wn)) |other| return l.why.print("refused: word {s} is {s}'s", .{ wn, headName(other.ops) });
- for (headWords(ops)[0..i]) |*before| if (std.mem.eql(u8, std.mem.span(before.name), wn)) return l.why.print("refused: word {s} twice", .{wn});
- if (w.leader != null) {
- const keys = std.mem.span(w.leader);
- if (keys.len == 0 or coreLeader(keys) or leader(keys) != .none) return l.why.print("refused: leader SPC {s} is taken", .{keys});
- for (words[0..nwords]) |o| if (o.ops != skipping and o.w.leader != null and std.mem.startsWith(u8, keys, std.mem.span(o.w.leader))) return l.why.print("refused: leader SPC {s} is taken", .{keys});
- }
- }
- var it = std.mem.tokenizeScalar(u8, extsOf(ops), ' ');
- var n: usize = 0;
- while (it.next()) |ext| : (n += 1) {
- if (ext.len < 2 or ext[0] != '.') return l.why.print("refused: extension {s} has no dot", .{ext});
- if (forPath(ext)) |other| return l.why.print("refused: extension {s} is {s}'s", .{ ext, headName(other) });
+ /// The kinds compiled in and what the scan at start found.
+ pub fn started(found: []const Loaded) Registry {
+ var r: Registry = .init();
+ for (found) |l| _ = r.take(l, "");
+ return r;
}
- if (nexts + n > exts.len) return "refused: no room for its extensions";
- return 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;
-}
-
-fn coreLeader(keys: []const u8) bool {
- return @import("keys.zig").leaderTaken(keys);
-}
-/// `name` out of the list, its words and extensions with it: its
-/// library's handle, to close, or null when no such kind is loaded.
-/// The caller sees no pane of it is open.
-pub fn unregister(name: []const u8) ?Listed {
- statics();
- const at = for (listed[0..nlisted], 0..) |*l, i| {
- if (l.ops != null and std.mem.eql(u8, l.nameOf(), name)) break i;
- } else return null;
- const gone = listed[at];
- dropTail(gone.ops.?);
- std.mem.copyForwards(Listed, listed[at .. nlisted - 1], listed[at + 1 .. nlisted]);
- nlisted -= 1;
- return gone;
-}
+ pub fn byName(r: *const Registry, name: []const u8) ?u8 {
+ for (r.kinds[0..r.n], 0..) |*e, i| if (!e.refused and std.mem.eql(u8, e.nameOf(), name)) return @intCast(i);
+ return null;
+ }
-/// An addon at `path` that brought no kind, listed with why.
-pub fn note(path: []const u8, why: []const u8) void {
- statics();
- if (nlisted == max_kinds) return;
- listed[nlisted] = .{};
- listed[nlisted].path.set(path);
- listed[nlisted].why.set(why);
- nlisted += 1;
-}
+ // What libraries bring, the loader's (Addon.zig).
+ pub const forPath = Addon.forPath;
+ pub const word = Addon.word;
+ pub const leader = Addon.leader;
+ pub const take = Addon.take;
+ pub const collides = Addon.collides;
+ pub const drop = Addon.drop;
-/// The refused ones forgotten, as a load of the same path tries again.
-pub fn forgetRefused(path: []const u8) void {
- var n: usize = 0;
- for (listed[0..nlisted]) |l| if (l.ops != null or !std.mem.eql(u8, l.path.slice(), path)) {
- listed[n] = l;
- n += 1;
- };
- nlisted = n;
-}
-
-/// /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 (kinds()) |*l| {
- const name = if (l.nameOf().len > 0) l.nameOf() else std.fs.path.basename(l.path.slice());
- try out.print(gpa, "{s} {d} {d}.{d} {d} {s} ", .{ name, l.version, l.abi >> 16, l.abi & 0xffff, if (l.ops != null) openPanes(p, name) else 0, if (l.path.len > 0) l.path.slice() else "-" });
- if (l.hash) |h| try out.print(gpa, "{x}", .{h}) else try out.append(gpa, '-');
- try out.print(gpa, " {s}\n", .{l.why.slice()});
- }
-}
+};
// ---- a pane of a kind ----
/// What the core keeps of a pane of a kind, round the kind's own state.
-/// The walker sees none of it (it is the kind's): a dump writes its
-/// record by hand (`dumpWrite`), a digest takes its name and path, and a
-/// rollback copy leaves it be, as a terminal's emulator.
+/// A rollback copy leaves it be, as a terminal's emulator; a dump writes
+/// its record by hand (dump.zig).
pub const Slot = struct {
- /// Null: a placeholder, its kind not here (a dump's, loaded without
- /// it), which keeps its record for the day it comes.
- ops: ?*const Ops,
+ /// Its kind's code here; null in a placeholder (`live` false: its kind
+ /// not in the registry, its record kept for the day it comes) or a
+ /// Shadow (a replica's pane of a library's kind).
+ ops: ?*const Ops = null,
+ live: bool = false,
st: ?*anyopaque = null,
- /// What the slot's memory comes from; the kind's allocations are its
- /// arena's, freed at the close.
+ /// The kind's allocations are its arena's, freed at the close.
gpa: std.mem.Allocator,
arena: std.heap.ArenaAllocator,
name: [16]u8 = @splat(0),
version: u32 = 0,
+ /// Its kind's generation when it was opened (Entry.gen).
+ gen: u32 = 0,
path: []u8 = &.{},
/// Petscii, Palette and Ascii: how a frontend shows its placements.
art: Placement.Art = .{},
- /// When `tick` asked to be called next; 0, never.
+ /// When `tick` is next due; 0, never; 1, at the next step.
next_tick: u64 = 0,
- jobs: [4]?*Job = @splat(null),
+ /// PA_FOCUSED as `tick` was last told.
+ focused: bool = false,
+ /// Its jobs' inputs, until the host takes them (Effect.job).
+ jobs: [4]?Job = @splat(null),
tokens: u32 = 0,
/// A placeholder's: the kind's record and its `where`, kept whole.
record: ?[]u8 = null,
where_text: Buf(32) = .{},
- /// An error the kind said while opening: the open refused.
+ /// What the kind said refusing its open (or a load).
refused: Buf(96) = .{},
/// Its words' last values, a `Word arg` line each, which a kind with
/// no `dump` is given again when its pane comes back.
values: Buf(256) = .{},
+ /// Its last frame's prefix, and whether it placed a picture.
+ prefix: Buf(96) = .{},
+ placed: bool = false,
+ /// Its last frame changed its tag: a step is owed to lay the tag out
+ /// again (the tag is laid out before the body paints).
+ retag: bool = false,
+ /// A replica's frame of it, as the authority painted it.
+ shadow: ?*@import("screen_state.zig").Shadow = null,
+ /// Debug: the kind's allocations, which paint must leave as they are.
+ used: if (checked) std.ArrayList([]u8) else void = if (checked) .empty else {},
+
+ pub const Job = struct { token: u32, in: []u8 };
+
+ pub const dumpWrite = @import("dump.zig").slotWrite;
+ pub const dumpRead = @import("dump.zig").slotRead;
+ pub const dumpFree = @import("dump.zig").slotFree;
pub fn nameOf(s: *const Slot) []const u8 {
return std.mem.sliceTo(&s.name, 0);
@@ -344,92 +193,17 @@ pub const Slot = struct {
return std.mem.eql(u8, s.nameOf(), name);
}
- /// The core's words of a pane: a kind's with keys is raw, as a raw
- /// terminal is.
- pub fn takesKeys(s: *const Slot) bool {
- return if (s.ops) |o| o.key != null else false;
- }
-
- pub fn takesMouse(s: *const Slot) bool {
- return if (s.ops) |o| o.mouse != null else false;
- }
-
pub fn digest(s: *const Slot, h: *walk.Hasher) void {
h.update(s.nameOf());
h.update(s.path);
h.update(std.mem.asBytes(&s.art));
- h.update(&.{@intFromBool(s.ops != null)});
+ h.update(std.mem.asBytes(&.{ s.next_tick, s.gen, @intFromBool(s.live), @intFromBool(s.focused) }));
}
- /// What a dump keeps of it: the kind, its path, its place, the art
- /// bytes, and the kind's own record when it has one.
- const Record = struct {
- name: []const u8 = "",
- version: u32 = 0,
- path: []const u8 = "",
- where: []const u8 = "",
- art: Placement.Art = .{},
- record: ?[]const u8 = null,
- values: []const u8 = "",
- };
-
- pub fn dumpWrite(s: *const Slot, gpa: std.mem.Allocator, w: *std.Io.Writer) !void {
- var place: [32]u8 = undefined;
- var r: Record = .{ .name = s.nameOf(), .version = s.version, .path = s.path, .art = s.art, .record = s.record, .where = s.where_text.slice(), .values = s.values.slice() };
- var owned: ?[]u8 = null;
- defer if (owned) |o| gpa.free(o);
- if (s.ops) |ops| {
- const prev = enter(null, 0, @constCast(s), "dump");
- defer leave(prev);
- if (ops.where) |f| r.where = place[0..@min(place.len, f(s.st, &place, place.len))];
- if (ops.dump) |f| {
- var cap: usize = 64 << 10;
- while (true) {
- const buf = try gpa.alloc(u8, cap);
- const n = f(s.st, buf.ptr, @intCast(cap));
- if (n <= cap) {
- owned = buf;
- r.record = buf[0..n];
- break;
- }
- gpa.free(buf);
- cap = n;
- }
- }
- }
- const bytes = try walk.save(Record, gpa, &r);
- defer gpa.free(bytes);
- try w.writeAll(bytes);
- }
-
- pub fn dumpFree(s: *Slot, gpa: std.mem.Allocator) void {
- _ = gpa;
- s.free();
- }
-
- /// The pane back from its record: through its kind's `open` and
- /// `load` (or `address` with its place) when this process has it,
- /// else a placeholder holding the record.
- pub fn dumpRead(out: *Slot, gpa: std.mem.Allocator, bytes: []const u8) !void {
- var arena: std.heap.ArenaAllocator = .init(gpa);
- defer arena.deinit();
- const r = try walk.load(Record, arena.allocator(), bytes, null);
- out.* = .{ .ops = null, .gpa = gpa, .arena = .init(gpa), .version = r.version, .art = r.art };
- 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);
- if (byName(out.nameOf())) |ops| reopen(null, 0, out, ops);
- }
-
- fn free(s: *Slot) void {
- for (&s.jobs) |*j| if (j.*) |job| {
- job.wait();
- job.destroy();
- j.* = null;
- };
+ pub fn free(s: *Slot) void {
+ for (&s.jobs) |*j| if (j.*) |job| s.gpa.free(job.in);
+ if (checked) s.used.deinit(s.gpa);
+ if (s.shadow) |sh| sh.deinit(s.gpa);
s.arena.deinit();
s.gpa.free(s.path);
if (s.record) |r| s.gpa.free(r);
@@ -437,168 +211,21 @@ pub const Slot = struct {
}
};
-/// A placeholder made whole by its kind: `open` from its path, then
-/// `load` with its record, or `address` with its place. A refused load
-/// keeps it a placeholder, its record kept.
-fn reopen(p: ?*Pardes, id: usize, s: *Slot, ops: *const Ops) void {
- const prev = enter(p, id, s, "open");
- defer leave(prev);
- const path_z = s.arena.allocator().dupeSentinel(u8, s.path, 0) catch return;
- current.?.opening = true;
- const st = if (ops.open) |f| f(if (s.path.len > 0) path_z.ptr else null, null, 0) else null;
- current.?.opening = false;
- if (s.refused.len > 0) {
- if (ops.close) |f| f(st);
- return;
- }
- if (s.record) |rec| if (ops.load) |f| if (f(st, rec.ptr, @intCast(rec.len), s.version) != 0) {
- if (ops.close) |cl| cl(st);
- s.refused.set("its load refused the record");
- return;
- };
- if (s.record == null and s.where_text.len > 0) if (ops.address) |f| {
- _ = f(st, s.where_text.slice().ptr, @intCast(s.where_text.len));
- };
- // ...and its words' last values, a kind with no record of its own.
- if (s.record == null) if (ops.ctl) |f| {
- var lines = std.mem.tokenizeScalar(u8, s.values.slice(), '\n');
- var reply: [160]u8 = undefined;
- while (lines.next()) |line| _ = f(st, line.ptr, @intCast(line.len), &reply, reply.len);
- };
- s.ops = ops;
- s.st = st;
- s.version = ops.head.version;
- if (ops.tick != null) s.next_tick = 1;
- if (s.record) |r| s.gpa.free(r);
- s.record = null;
-}
-
-/// The panes of kind `name` that waited for it (placeholders), opened.
-pub fn reopenWaiting(p: *Pardes, name: []const u8) usize {
- const ops = byName(name) orelse return 0;
- var n: usize = 0;
- for (p.panes, 0..) |slot, id| if (slot) |pane| if (pane.addon) |s| if (s.ops == null and s.is(name)) {
- s.refused = .{};
- reopen(p, id, s, ops);
- if (s.ops != null) n += 1;
- };
- return n;
-}
-
-/// `Addon reload`: the kind `name` replaced by `ops` (a library loaded
-/// beside the old one), or why not, nothing changed. Everything is
-/// checked first: its head and collisions (the old kind's own looked
-/// past), its name the same, and every open pane of the kind opened anew
-/// in a fresh arena from the old state's `dump` (its `load`, else its
-/// place and words); only then are the old states closed and the panes
-/// swapped. A job the old library started is dropped, never handed to
-/// the new one. The old library's handle comes back in `old_lib`.
-pub fn replace(p: *Pardes, name: []const u8, ops: *const Ops, lib: *anyopaque, hash: [32]u8, old_lib: *?*anyopaque, why: *Buf(160)) bool {
- const l = listedNamed(name) orelse return fail(why, "no addon brought kind {s}", .{name});
- const old = l.ops.?;
- var trial: Listed = .{};
- skipping = old;
- const refused = check(ops, &trial);
- skipping = null;
- if (refused) |r| return fail(why, "{s}", .{r});
- if (!std.mem.eql(u8, headName(ops), name)) return fail(why, "refused: its kind is now {s}, not {s}", .{ headName(ops), name });
- if (ops.init) |init| if (init(&api) != 0) return fail(why, "refused: its init failed", .{});
- var fresh: [pardes.MAX_PANES]?*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.ops == old) {
- const n = s.gpa.create(Slot) catch return fail(why, "out of memory", .{});
- n.* = .{ .ops = null, .gpa = s.gpa, .arena = .init(s.gpa), .name = s.name, .version = s.version, .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.
- {
- const prev = enter(p, id, s, "dump");
- defer leave(prev);
- var place: [32]u8 = undefined;
- if (old.where) |f| n.where_text.set(place[0..@min(place.len, f(s.st, &place, place.len))]);
- if (old.dump != null and ops.load != null) n.record = dumpOf(s) catch return fail(why, "out of memory", .{});
- }
- reopen(p, id, n, ops);
- if (n.ops == null) {
- for (fresh) |f| if (f) |o| closeState(p, o);
- return fail(why, "refused: a pane of it would not open again: {s}", .{if (n.refused.len > 0) n.refused.slice() else "its load refused the record"});
- }
- };
- // 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| {
- for (&s.jobs) |*j| if (j.*) |job| {
- job.wait();
- job.destroy();
- j.* = null;
- };
- closeState(p, s);
- s.arena.deinit();
- s.arena = n.arena;
- n.arena = .init(n.gpa);
- s.st = n.st;
- s.ops = ops;
- s.version = n.version;
- s.next_tick = n.next_tick;
- n.st = null;
- n.ops = null;
- };
- dropTail(old);
- addTail(ops);
- old_lib.* = l.lib;
- l.ops = ops;
- l.lib = lib;
- l.hash = hash;
- l.version = ops.head.version;
- l.abi = ops.head.abi;
- return true;
-}
-
-fn closeState(p: *Pardes, s: *Slot) void {
- const ops = s.ops orelse return;
- const f = ops.close orelse return;
- const prev = enter(p, 0, s, "close");
- defer leave(prev);
- f(s.st);
-}
-
-fn fail(why: *Buf(160), comptime fmt: []const u8, args: anytype) bool {
- _ = why.print(fmt, args);
- return false;
-}
-
-/// The kind's record of a slot's state, in `s.gpa`.
-fn dumpOf(s: *Slot) ![]u8 {
- const f = s.ops.?.dump.?;
- var cap: usize = 64 << 10;
- while (true) {
- const buf = try s.gpa.alloc(u8, cap);
- const n = f(s.st, buf.ptr, @intCast(cap));
- if (n <= cap) return s.gpa.realloc(buf, n) catch buf[0..n];
- s.gpa.free(buf);
- cap = n;
- }
-}
+const checked = builtin.mode == .debug;
-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.ops != null and s.is(name)) {
- n += 1;
- };
- return n;
+fn caps(p: *const Pardes, s: *const Slot) Caps {
+ const k = p.kinds.byName(s.nameOf()) orelse return .{};
+ return if (s.live) p.kinds.kinds[k].caps else .{};
}
-/// A new pane of kind `ops` in slot `id`, showing `path` (null for one a
+/// A new slot of kind `k` for pane `id`, showing `path` (null for one a
/// word opened) from `bytes`: made whatever the kind says, with what it
/// refused in `slot.refused` for the caller to say.
-pub fn open(p: *Pardes, id: usize, ops: *const Ops, path: ?[]const u8, bytes: ?[]const u8) !*Pane {
+pub fn open(p: *Pardes, id: usize, k: u8, path: ?[]const u8, bytes: ?[]const u8) !*Pane {
const gpa = p.image_gpa;
+ const e = &p.kinds.kinds[k];
const s = try gpa.create(Slot);
- s.* = .{ .ops = ops, .gpa = gpa, .arena = .init(gpa), .version = ops.head.version, .name = ops.head.name };
- s.name[15] = 0;
+ s.* = .{ .ops = p.kinds.ops[k], .live = true, .gpa = gpa, .arena = .init(gpa), .version = e.version, .gen = e.gen, .name = e.name };
errdefer {
s.free();
gpa.destroy(s);
@@ -606,34 +233,72 @@ pub fn open(p: *Pardes, id: usize, ops: *const Ops, path: ?[]const u8, bytes: ?[
s.path = try gpa.dupe(u8, path orelse "");
const pane = try p.newDocPane(id);
pane.addon = s;
- if (ops.tick != null) s.next_tick = 1;
- const prev = enter(p, id, s, "open");
- defer leave(prev);
- const path_z = try s.arena.allocator().dupeSentinel(u8, s.path, 0);
- current.?.opening = true;
- if (ops.open) |f| s.st = f(if (path != null) path_z.ptr else null, if (bytes) |b| b.ptr else null, if (bytes) |b| b.len else 0);
+ _ = reopen(p, id, s, bytes);
return pane;
}
/// `open` of the file at `path`, read here (a file that cannot be read
/// is no bytes: its kind may refuse it). The slot stays ours across the
/// read: another request may make a pane meanwhile.
-pub fn openFile(p: *Pardes, id: usize, ops: *const Ops, path: []const u8) !*Pane {
+pub fn openFile(p: *Pardes, id: usize, k: u8, path: []const u8) !*Pane {
p.reserved_slots[id] = true;
const bytes: ?[]u8 = @import("fs.zig").read(p, path) catch null;
p.reserved_slots[id] = false;
defer if (bytes) |b| p.gpa.free(b);
- return open(p, id, ops, path, bytes orelse "");
+ return open(p, id, k, path, bytes orelse "");
+}
+
+/// 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 {
+ var cl: Call = .{ .p = p, .id = id, .slot = s };
+ const prev = cl.begin("open");
+ if (s.ops) |ops| {
+ const path_z = s.arena.allocator().dupeSentinel(u8, s.path, 0) catch "";
+ cl.opening = true;
+ if (ops.open) |f| s.st = f(if (s.path.len > 0) path_z.ptr else null, if (bytes) |b| b.ptr else null, if (bytes) |b| b.len else 0);
+ cl.opening = false;
+ if (cl.refused.len == 0) if (s.record) |rec| if (ops.load) |f| if (f(s.st, rec.ptr, @intCast(rec.len), s.version) != c.PA_OK) {
+ cl.refused.set("its load refused the record");
+ };
+ if (cl.refused.len == 0 and s.record == null) {
+ if (s.where_text.len > 0) if (ops.address) |f| {
+ _ = f(s.st, s.where_text.slice().ptr, @intCast(s.where_text.len));
+ };
+ var lines = std.mem.tokenizeScalar(u8, s.values.slice(), '\n');
+ var reply: [160]u8 = undefined;
+ if (ops.ctl) |f| while (lines.next()) |line| {
+ _ = f(s.st, line.ptr, @intCast(line.len), &reply, reply.len);
+ };
+ }
+ if (cl.refused.len > 0) if (ops.close) |f| f(s.st);
+ }
+ const o = cl.end(prev);
+ s.refused.set(o.text);
+ if (o.text.len > 0) return false;
+ if (s.record) |r| s.gpa.free(r);
+ s.record = null;
+ s.live = true;
+ if (p) |core| poke(core, s);
+ return true;
+}
+
+/// Tick at the next step, once (16.2's rule 2).
+fn poke(p: *const Pardes, s: *Slot) void {
+ if (caps(p, s).tick) s.next_tick = 1;
}
/// Its kind's close, then everything it had.
pub fn close(p: *Pardes, pane: *Pane) void {
const s = pane.addon orelse return;
if (s.ops) |ops| if (ops.close) |f| {
- const prev = enter(p, 0, s, "close");
- defer leave(prev);
+ var cl: Call = .{ .p = null, .id = 0, .slot = s, .quiet = true };
+ const prev = cl.begin("close");
f(s.st);
+ _ = cl.end(prev);
};
+ _ = p;
const gpa = s.gpa;
s.free();
gpa.destroy(s);
@@ -641,68 +306,90 @@ pub fn close(p: *Pardes, pane: *Pane) void {
}
/// The pane's body painted over the `cols` x `rows` cells at (x, y):
-/// its cells into `s`, its placements after the frame's others. A
-/// placeholder says what it waits for.
+/// its cells into `s`, its placements after the frame's others. A Shadow
+/// draws the authority's frame of it; a placeholder says what it waits for.
pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pane, id: usize, x: u16, y: u16, cols: u16, rows: u16) void {
const slot = pane.addon orelse return;
if (cols == 0 or rows == 0) return;
const th = p.theme();
const fg: surface.Color = if (th.fg) |f| .{ .rgb = f } else .default;
const bg: surface.Color = if (th.bg) |b| .{ .rgb = b } else .default;
- const ops = slot.ops orelse {
+ var frame: Frame = .{ .cols = cols, .rows = rows };
+ if (slot.ops == null and slot.live) {
+ const sh = slot.shadow orelse return;
+ frame = .{ .cells = sh.cells, .cols = sh.cols, .rows = sh.rows, .places = sh.places.items, .n = @intCast(sh.places.items.len) };
+ } else if (slot.ops == null) {
var line: [160]u8 = undefined;
const why = if (slot.refused.len > 0) slot.refused.slice() else "Addon load <path>";
const text = std.fmt.bufPrint(&line, "addon {s} {d} is not loaded: {s}", .{ slot.nameOf(), slot.version, why }) catch &line;
_ = s.print(x, y, cols, text, .{ .fg = fg, .bg = bg });
return;
- };
- const cells = arena.alloc(c.pa_cell, @as(usize, cols) * rows) catch return;
- @memset(std.mem.sliceAsBytes(cells), 0);
- var places: [16]c.pa_placement = undefined;
- var out: c.pa_paint = std.mem.zeroInit(c.pa_paint, .{ .placements = &places, .cap = places.len });
- const win = window(p, slot, pane, cells.ptr, cols, rows);
- {
- const prev = enter(p, id, slot, "paint");
- defer leave(prev);
- current.?.painting = true;
- ops.paint.?(slot.st, &win, &out);
- }
- for (0..rows) |r| for (0..cols) |col| {
- const cell = &cells[r * cols + col];
+ } else frame.cells = (paint(p, slot, id, arena, cols, rows, &frame) orelse return);
+ for (0..@min(rows, frame.rows)) |r| for (0..@min(cols, frame.cols)) |col| {
+ const cell = &frame.cells[r * frame.cols + col];
if (cell.flags & c.PA_PAINTED == 0) continue;
const len = @min(cell.len, 7);
s.set(x + @as(u16, @intCast(col)), y + @as(u16, @intCast(r)), if (len == 0) " " else cell.text[0..len], styleOf(cell, fg, bg));
};
- for (places[0..@min(out.n, places.len)]) |pl| {
+ for (frame.places[0..frame.n]) |pl| {
if (pl.rgba == null or pl.w == 0 or pl.h == 0) continue;
const dx = @min(pl.dst.x, cols);
const dy = @min(pl.dst.y, rows);
_ = p.appendPlacement(.{
.pane = @intCast(id),
- .raster = .{ .ptr = pl.rgba, .w = pl.w, .h = pl.h, .stride = pl.stride },
+ .raster = .{ .ptr = pl.rgba, .w = pl.w, .h = pl.h, .stride = pl.w * 4 },
.src = .{ .x = pl.src.x, .y = pl.src.y, .w = pl.src.w, .h = pl.src.h },
.dst = .{ .x = x + dx, .y = y + dy, .w = @min(pl.dst.w, cols - dx), .h = @min(pl.dst.h, rows - dy) },
.at = if (pl.at.w == 0) null else .{ .x = pl.at.x, .y = pl.at.y, .w = pl.at.w, .h = pl.at.h },
.key = Placement.keyOf(pane.serial, @truncate(pl.key >> 32), @truncate(pl.key)),
+ .dirty = if (pl.from == 0) null else .{ .from = Placement.keyOf(pane.serial, @truncate(pl.from >> 32), @truncate(pl.from)), .y = pl.dirty_y, .h = pl.dirty_h },
.art = slot.art,
});
}
}
-fn window(p: *const Pardes, slot: *const Slot, pane: *const Pane, cells: [*]c.pa_cell, cols: u16, rows: u16) c.pa_window {
+/// A frame of a kind's pane: its cells, `cols` a row, and its placements.
+pub const Frame = struct {
+ cells: []c.pa_cell = &.{},
+ cols: u16,
+ rows: u16,
+ places: []c.pa_placement = &.{},
+ n: u32 = 0,
+ buf: [64]c.pa_placement = undefined,
+};
+
+/// The one paint of a frame: its cells (in `arena`) and placements into
+/// `frame`, its prefix and whether it placed a picture into the slot.
+/// Debug checks it changed none of the kind's memory.
+pub fn paint(p: *const Pardes, slot: *Slot, id: usize, arena: std.mem.Allocator, cols: u16, rows: u16, frame: *Frame) ?[]c.pa_cell {
+ const ops = slot.ops orelse return null;
+ const cells = arena.alloc(c.pa_cell, @as(usize, cols) * rows) catch return null;
+ @memset(std.mem.sliceAsBytes(cells), 0);
+ var out: c.pa_paint = std.mem.zeroInit(c.pa_paint, .{ .placements = &frame.buf, .cap = frame.buf.len });
const px = if (comptime pardes.pdf_enabled) p.cell_pixels else struct { w: u16 = 8, h: u16 = 16 }{};
- return std.mem.zeroInit(c.pa_window, .{
- .cells = cells,
- .stride = cols,
- .rows = rows,
- .cols = cols,
- .cell_w = px.w,
- .cell_h = px.h,
- .focused = @intFromBool(p.panes[p.active] == pane),
- .glyphs = @intFromBool(slot.art.glyph or !p.frontend.native_images),
- .palette = @intFromEnum(slot.art.palette),
- .ascii = @intFromBool(slot.art.ascii),
- });
+ const win = std.mem.zeroInit(c.pa_window, .{ .cells = cells.ptr, .stride = cols, .rows = rows, .cols = cols, .cell_w = px.w, .cell_h = px.h, .focused = @intFromBool(focusedAnywhere(p, id)) });
+ var cl: Call = .{ .p = null, .id = 0, .slot = slot, .quiet = true };
+ const prev = cl.begin("paint");
+ const before = if (checked) usedHash(slot) else {};
+ ops.paint.?(slot.st, &win, &out);
+ if (checked and usedHash(slot) != before) std.debug.panic("addon {s}: paint changed its state (paint must be pure)", .{slot.nameOf()});
+ _ = cl.end(prev);
+ frame.places = &frame.buf;
+ frame.n = @min(out.n, frame.buf.len);
+ const own = out.prefix[0..@min(out.prefix_len, out.prefix.len)];
+ const placed = for (frame.buf[0..frame.n]) |pl| {
+ if (pl.rgba != null and pl.w > 0 and pl.h > 0) break true;
+ } else false;
+ if (placed != slot.placed or !std.mem.eql(u8, own, slot.prefix.slice())) slot.retag = true;
+ slot.prefix.set(own);
+ slot.placed = placed;
+ return cells;
+}
+
+fn usedHash(slot: *const Slot) u64 {
+ var h = std.hash.Wyhash.init(0);
+ for (slot.used.items) |m| h.update(m);
+ return h.final();
}
fn styleOf(cell: *const c.pa_cell, fg: surface.Color, bg: surface.Color) surface.CellStyle {
@@ -726,41 +413,62 @@ fn colorOf(v: u32, default: surface.Color) surface.Color {
};
}
-/// The start of its tag: what its kind's paint says over no cells, else
-/// its path (a word's pane: the kind's name).
+/// The start of its tag: its kind's prefix from its last frame (else the
+/// kind's name), the core's art words when that frame placed a picture,
+/// and its path. The authority's: a step reads it off the tape on a replica.
pub fn prefix(p: *const Pardes, arena: std.mem.Allocator, pane: *const Pane) ![]const u8 {
const slot = pane.addon.?;
- const fallback = if (slot.path.len > 0) slot.path else slot.nameOf();
- const ops = slot.ops orelse return fallback;
- var out: c.pa_paint = std.mem.zeroes(c.pa_paint);
- const win = window(p, slot, pane, undefined, 0, 0);
- {
- const prev = enter(null, 0, slot, "paint");
- defer leave(prev);
- current.?.painting = true;
- ops.paint.?(slot.st, &win, &out);
- }
- if (out.prefix_len == 0) return fallback;
- return arena.dupe(u8, out.prefix[0..@min(out.prefix_len, out.prefix.len)]);
+ var buf: [400]u8 = undefined;
+ const made = tape.value(.addon, prefixOf, .{ p, slot, &buf });
+ return arena.dupe(u8, made);
+}
+
+fn prefixOf(p: *const Pardes, slot: *const Slot, buf: *[400]u8) []const u8 {
+ if (!slot.live) return if (slot.path.len > 0) slot.path else slot.nameOf();
+ const own = if (slot.shadow) |sh| sh.prefix.slice() else slot.prefix.slice();
+ const placed = if (slot.shadow) |sh| sh.placed else slot.placed;
+ var w: std.Io.Writer = .fixed(buf);
+ w.writeAll(if (own.len > 0) own else slot.nameOf()) catch {};
+ if (placed) w.print(" petscii:{s} palette:{t} ascii:{s}", .{
+ if (slot.art.glyph or !p.frontend.native_images) "on" else "off",
+ slot.art.palette,
+ if (slot.art.ascii) "on" else "off",
+ }) catch {};
+ if (slot.path.len > 0) w.print(" {s}", .{slot.path}) catch {};
+ return w.buffered();
}
/// A key at a pane whose kind takes keys: whether it did. A replica's
-/// speculation leaves it to the authority (the kind's state is not in
-/// the rollback copy).
+/// speculation leaves it to the authority.
pub fn key(p: *Pardes, id: usize, k: pardes.Key) bool {
const slot = (p.panes[id] orelse return false).addon orelse return false;
- const f = (slot.ops orelse return false).key orelse return false;
+ if (!caps(p, slot).key) return false;
if (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), .pad = 0 };
- const prev = enter(p, id, slot, "key");
- defer leave(prev);
- return f(slot.st, &pk) != 0;
+ var cl: Call = .{ .p = p, .id = id, .slot = slot };
+ const prev = cl.begin("key");
+ if (slot.ops) |o| cl.ret = o.key.?(slot.st, &pk);
+ return taken(p, slot, cl.end(prev));
+}
+
+fn taken(p: *Pardes, slot: *Slot, o: Outcome) bool {
+ if (o.ret == 0) return false;
+ poke(p, slot);
+ return true;
+}
+
+pub fn takesKeys(p: *const Pardes, s: *const Slot) bool {
+ return caps(p, s).key;
+}
+
+pub fn takesMouse(p: *const Pardes, s: *const Slot) bool {
+ return caps(p, s).mouse;
}
/// The pointer at (col, row) of its body.
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;
- const f = (slot.ops orelse return false).mouse orelse return false;
+ if (!caps(p, slot).mouse) return false;
if (authorityOnly("the mouse in a kind's pane")) return true;
const pm: c.pa_mouse = .{
.button = switch (m.button) {
@@ -784,55 +492,77 @@ pub fn mouse(p: *Pardes, id: usize, m: pardes.Mouse, col: u16, row: u16) bool {
.col = col,
.row = row,
};
- const prev = enter(p, id, slot, "mouse");
- defer leave(prev);
- return f(slot.st, &pm) != 0;
+ var cl: Call = .{ .p = p, .id = id, .slot = slot };
+ const prev = cl.begin("mouse");
+ if (slot.ops) |o| cl.ret = o.mouse.?(slot.st, &pm);
+ return taken(p, slot, cl.end(prev));
}
-/// A step of the session's clock: each kind whose time has come ticks,
-/// and each job's answer lands.
+/// The end of a step: each pane of a kind whose focus changed, or whose
+/// time has come, ticks (16.2).
pub fn step(p: *Pardes) void {
if (tape.speculating()) return;
const now = p.clock.stepped_ns;
for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |slot| {
- const ops = slot.ops orelse continue;
- for (&slot.jobs) |*j| if (j.*) |job| if (job.done.load(.acquire)) {
- j.* = null;
- if (job.thread) |t| t.join();
- const prev = enter(p, id, slot, "job_done");
- ops.job_done.?(slot.st, job.token, job.out.ptr, @intCast(job.out.len));
- leave(prev);
- job.destroy();
- p.needs_frame = true;
- };
- if (slot.next_tick != 0 and now >= slot.next_tick) if (ops.tick) |f| {
- const prev = enter(p, id, slot, "tick");
- slot.next_tick = f(slot.st, now);
- leave(prev);
- p.needs_frame = true;
- };
+ // Its tag laid out again by this step's sync, and drawn.
+ if (slot.retag) p.needs_frame = true;
+ slot.retag = false;
+ if (!caps(p, slot).tick) continue;
+ const focused = focusedAnywhere(p, id);
+ if (focused != slot.focused) {
+ slot.focused = focused;
+ slot.next_tick = 1;
+ }
+ if (slot.next_tick == 0 or now < slot.next_tick) continue;
+ var cl: Call = .{ .p = p, .id = id, .slot = slot };
+ const prev = cl.begin("tick");
+ if (slot.ops) |o| cl.ret = @bitCast(o.tick.?(slot.st, now, if (focused) c.PA_FOCUSED else 0));
+ slot.next_tick = @bitCast(cl.end(prev).ret);
+ p.needs_frame = true;
};
}
-/// When the kinds next want a step: `0` a frame from now (a job under
-/// way), a time, or null.
+/// Whether pane `id` has the keyboard in some view (its window focused).
+fn focusedAnywhere(p: *const Pardes, id: usize) bool {
+ if (p.active == id and p.window_focused) return true;
+ for (p.parked, 0..) |v, k| if (v) |pv| if (k != p.view and views.isOpen(p, @intCast(k)) and pv.view.active == id and pv.view.window_focused) return true;
+ return false;
+}
+
+/// When the kinds next want a step: the soonest deadline (now, for a
+/// tag to lay out again), or null.
pub fn wake(p: *const Pardes) ?u64 {
var soonest: ?u64 = null;
for (p.panes) |pslot| if (pslot) |pane| if (pane.addon) |slot| {
- for (slot.jobs) |j| if (j != null) return 0;
- if (slot.next_tick != 0) soonest = @min(soonest orelse slot.next_tick, slot.next_tick);
+ const due = if (slot.retag) @max(1, p.clock.stepped_ns) else slot.next_tick;
+ if (due != 0) soonest = @min(soonest orelse due, due);
};
return soonest;
}
+/// A job's answer (Event.job): to its kind, unless its pane or its
+/// kind's generation is another's since it was asked.
+pub fn jobDone(p: *Pardes, pane_id: u8, serial: u32, token: u32, gen: u32, bytes: []const u8) void {
+ const pane = p.panes[pane_id] orelse return;
+ const slot = pane.addon orelse return;
+ if (pane.serial != serial or slot.gen != gen or !caps(p, slot).jobs) return;
+ var cl: Call = .{ .p = p, .id = pane_id, .slot = slot };
+ const prev = cl.begin("job_done");
+ if (slot.ops) |o| o.job_done.?(slot.st, token, bytes.ptr, @intCast(bytes.len));
+ _ = cl.end(prev);
+ poke(p, slot);
+ p.needs_frame = true;
+}
+
/// The pane's body as text (its 9P body), from the kind's `body`.
pub fn body(pane: *const Pane, gpa: std.mem.Allocator) ![]u8 {
const slot = pane.addon orelse return gpa.alloc(u8, 0);
const ops = slot.ops orelse return gpa.alloc(u8, 0);
const len_f = ops.body_len orelse return gpa.alloc(u8, 0);
const read = ops.body orelse return gpa.alloc(u8, 0);
- const prev = enter(null, 0, slot, "body");
- defer leave(prev);
+ var cl: Call = .{ .p = null, .id = 0, .slot = slot, .quiet = true };
+ const prev = cl.begin("body");
+ defer _ = cl.end(prev);
const n: usize = @intCast(@min(len_f(slot.st), 64 << 20));
const buf = try gpa.alloc(u8, n);
var off: usize = 0;
@@ -844,45 +574,52 @@ pub fn body(pane: *const Pane, gpa: std.mem.Allocator) ![]u8 {
return buf[0..off];
}
+/// The kind's `dump` into `buf`, a quiet call.
+pub fn dumpCall(slot: *Slot, f: anytype, buf: []u8) u32 {
+ var cl: Call = .{ .p = null, .id = 0, .slot = slot, .quiet = true };
+ const prev = cl.begin("dump");
+ defer _ = cl.end(prev);
+ return f(slot.st, buf.ptr, @intCast(buf.len));
+}
+
/// Where in it the pane is, as a text its `address` takes back.
-pub fn where(pane: *const Pane, buf: *[32]u8) []const u8 {
- const slot = pane.addon orelse return "";
+pub fn where(slot: *Slot, buf: *[32]u8) []const u8 {
const ops = slot.ops orelse return slot.where_text.slice();
const f = ops.where orelse return "";
- const prev = enter(null, 0, slot, "where");
- defer leave(prev);
+ var cl: Call = .{ .p = null, .id = 0, .slot = slot, .quiet = true };
+ const prev = cl.begin("where");
+ defer _ = cl.end(prev);
return buf[0..@min(buf.len, f(slot.st, buf, buf.len))];
}
-/// Go to `text` in it; false when the kind has no such place.
-pub fn address(p: *Pardes, id: usize, text: []const u8) bool {
- const slot = (p.panes[id] orelse return false).addon orelse return false;
- const f = (slot.ops orelse return false).address orelse return false;
- if (authorityOnly("an address in a kind's pane")) return true;
- const prev = enter(p, id, slot, "address");
- defer leave(prev);
- return f(slot.st, text.ptr, @intCast(text.len)) != 0;
-}
-
/// A kind's word as written to its pane: null taken, else why not.
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";
- const f = (slot.ops orelse return "its addon is not loaded").ctl orelse return "its kind takes no words";
+ if (!slot.live) return "its addon is not loaded";
+ if (!caps(p, slot).ctl) return "its kind takes no words";
if (authorityOnly("a kind's word")) return null;
- const prev = enter(p, id, slot, "ctl");
- defer leave(prev);
+ var cl: Call = .{ .p = p, .id = id, .slot = slot };
+ const prev = cl.begin("ctl");
@memset(reply, 0);
- if (f(slot.st, line.ptr, @intCast(line.len), reply, reply.len) == 0) return null;
- const said = std.mem.sliceTo(reply, 0);
- return if (said.len > 0) said else "refused";
+ if (slot.ops) |o| if (o.ctl.?(slot.st, line.ptr, @intCast(line.len), reply, reply.len) != c.PA_OK) {
+ const said = std.mem.sliceTo(reply, 0);
+ cl.refused.set(if (said.len > 0) said else "refused");
+ };
+ const o = cl.end(prev);
+ poke(p, slot);
+ if (o.text.len == 0) return null;
+ const n = @min(o.text.len, reply.len);
+ @memcpy(reply[0..n], o.text[0..n]);
+ return reply[0..n];
}
/// What its ctl file reads after the core's fields.
pub fn ctlRead(pane: *const Pane, buf: []u8) []const u8 {
const slot = pane.addon orelse return "";
const f = (slot.ops orelse return "").ctl_read orelse return "";
- const prev = enter(null, 0, slot, "ctl_read");
- defer leave(prev);
+ var cl: Call = .{ .p = null, .id = 0, .slot = slot, .quiet = true };
+ const prev = cl.begin("ctl_read");
+ defer _ = cl.end(prev);
return buf[0..@min(buf.len, f(slot.st, buf.ptr, @intCast(buf.len)))];
}
@@ -892,303 +629,178 @@ fn authorityOnly(comptime why: []const u8) bool {
return true;
}
-// ---- the Addon word, and the words addons bring ----
-
-pub const AddonOp = enum { load, unload, reload };
-
-fn listedNamed(name: []const u8) ?*Listed {
- statics();
- for (listed[0..nlisted]) |*l| if (l.ops != null and std.mem.eql(u8, l.nameOf(), name)) return l;
- return null;
-}
-
-/// `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. The library is the host's to open: an effect, its
-/// answer an `addon` event (`landed`).
-pub fn addonWord(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 (kinds()) |*l| if (l.ops != null) w.print(" {s} {d}{s}", .{ l.nameOf(), l.version, if (l.lib == null) " (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(AddonOp, 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;
- const what: []const u8 = switch (op) {
- .load => if (std.fs.path.isAbsolute(rest)) rest else std.fmt.bufPrint(&target, "{s}/{s}", .{ std.mem.trimEnd(u8, if (p.panes[id]) |pane| Pardes.paneDir(pane) else p.sessionDir(), "/"), rest }) catch rest,
- .unload, .reload => what: {
- const l = listedNamed(rest) orelse return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon {t}: no addon brought kind {s}", .{ op, rest }) catch takes);
- if (l.lib == null) return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon {t}: {s} is compiled in", .{ op, rest }) catch takes);
- const n = openPanes(p, rest);
- if (op == .unload and 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);
- break :what rest;
- },
- };
- if (what.len > pardes.effect_path_cap) return p.reportFailure(id, "Addon: that path is too long");
- p.emit(.{ .addon = .{ .op = op, .pane = @intCast(id), .arg = .from(what) } });
-}
-
-/// How the host's load, unload or reload went: `text` the kind's name,
-/// or why not. The panes that waited for that kind open.
-pub fn landed(p: *Pardes, op: AddonOp, pane: u8, ok: bool, text: []const u8) void {
- const id = if (pane < p.panes.len and p.panes[pane] != null) pane else p.active;
- var said: [256]u8 = undefined;
- if (!ok) return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon {t}: {s}", .{ op, text }) catch "Addon: refused");
- if (op == .unload) return p.setMessage(id, std.fmt.bufPrint(&said, "Addon unload: {s} is gone", .{text}) catch "Addon unload");
- const n = reopenWaiting(p, text);
- const version = if (byName(text)) |o| o.head.version else 0;
- p.setMessage(id, std.fmt.bufPrint(&said, "Addon {t}: {s} {d}{s}", .{ op, text, version, if (n > 0) ", its panes back" else "" }) catch "Addon");
-}
+// ---- a call into a kind ----
-/// A line whose first word is an addon's: run, and true; false for one
-/// that is no addon's word.
-pub fn runLine(p: *Pardes, id: usize, line: []const u8) bool {
- const end = std.mem.indexOfAny(u8, line, " \t") orelse line.len;
- const w = word(line[0..end]) orelse return false;
- runWord(p, id, w, line, std.mem.trim(u8, line[end..], " \t"));
- return true;
-}
-
-/// Word `w` from pane `id`: one that opens its kind opens a pane of it
-/// (`arg` its path), any other goes to the pane's kind, which must be
-/// the word's.
-pub fn runWord(p: *Pardes, id: usize, w: Word, line: []const u8, arg: []const u8) void {
- const name = std.mem.span(w.w.name);
- const kind = headName(w.ops);
- var said: [256]u8 = undefined;
- if (w.w.arg == c.PA_ARG_NONE and arg.len > 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} takes no argument", .{name}) catch name);
- if (w.w.arg == c.PA_ARG_REQUIRED and arg.len == 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} needs an argument", .{name}) catch name);
- if (w.w.opens != 0) {
- const free = p.freeSlot() orelse return p.reportError(id, name, error.NoPaneSlots);
- var path_buf: [4096]u8 = undefined;
- const path = if (arg.len == 0 or std.fs.path.isAbsolute(arg)) arg else std.fmt.bufPrint(&path_buf, "{s}/{s}", .{ std.mem.trimEnd(u8, if (p.panes[id]) |pane| Pardes.paneDir(pane) else p.sessionDir(), "/"), arg }) catch arg;
- const made = (if (path.len > 0) openFile(p, free, w.ops, path) else open(p, free, w.ops, null, null)) catch |err| return p.reportError(id, name, err);
- if (made.addon.?.refused.len > 0) {
- made.vweight = 0;
- p.unplaced.set(free);
- return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: {s}", .{ name, made.addon.?.refused.slice() }) catch name);
- }
- @import("exec.zig").placeNew(p, id, id, free, .doc);
- return;
- }
- const here = if (p.panes[id]) |pane| if (pane.addon) |s| s.ops == w.ops else false else false;
- if (!here) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} is a {s} pane's word: run it in one", .{ name, kind }) catch name);
- var reply: [160]u8 = undefined;
- if (ctl(p, id, line, &reply)) |why| return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: {s}", .{ name, why }) catch name);
- if (arg.len > 0) keepValue(p.panes[id].?.addon.?, name, arg);
-}
-
-/// `Word arg` kept as the word's last value, one line a word.
-fn keepValue(s: *Slot, name: []const u8, arg: []const u8) void {
- var next: Buf(256) = .{};
- var w: std.Io.Writer = .fixed(&next.bytes);
- var lines = std.mem.tokenizeScalar(u8, s.values.slice(), '\n');
- while (lines.next()) |line| {
- const head = line[0 .. std.mem.indexOfScalar(u8, line, ' ') orelse line.len];
- if (!std.mem.eql(u8, head, name)) w.print("{s}\n", .{line}) catch return;
- }
- w.print("{s} {s}\n", .{ name, arg }) catch return;
- next.len = @intCast(w.buffered().len);
- s.values = next;
-}
-
-// ---- what the core does for a kind (pa_api) ----
+/// What a call into a kind did that the core takes on: its answer, a
+/// refusal or reply, what it said, a Look, a text pane. The authority's
+/// is taped; a replica's core takes it off the tape, kind or none.
+pub const Outcome = struct {
+ ret: i64 = 0,
+ text: []const u8 = "",
+ said: []const u8 = "",
+ err: bool = false,
+ look: []const u8 = "",
+ pane_name: []const u8 = "",
+ pane_text: []const u8 = "",
+};
-/// The pane a call into a kind is for: what `api` acts on.
-const Current = struct {
+/// The call under way: what `api` acts on, and what it gathers.
+const Call = struct {
p: ?*Pardes,
id: usize,
slot: *Slot,
- call: []const u8 = "",
- painting: bool = false,
- /// In `open`: an error said refuses the pane.
+ /// Paint and the reads: what the kind asks of `api` is dropped, and
+ /// nothing is taped.
+ quiet: bool = false,
opening: bool = false,
+ name: []const u8 = "",
+ ret: i64 = 0,
+ refused: Buf(160) = .{},
+ said: Buf(160) = .{},
+ err: bool = false,
look: Buf(256) = .{},
+ pane_name: Buf(64) = .{},
+ pane_text: []const u8 = "",
+
+ fn begin(cl: *Call, comptime name: []const u8) ?*Call {
+ cl.name = name;
+ const prev = current;
+ current = cl;
+ return prev;
+ }
+
+ /// The call's outcome, as the authority's took it, done.
+ fn end(cl: *Call, prev: ?*Call) Outcome {
+ current = prev;
+ if (cl.quiet) return .{};
+ const o = tape.value(.addon, same, .{Outcome{ .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 }});
+ const p = cl.p orelse return o;
+ if (o.said.len > 0) if (o.err) p.reportFailure(cl.id, o.said) else p.setMessage(cl.id, o.said);
+ if (o.pane_name.len > 0) textPane(p, cl.id, cl.slot, o.pane_name, o.pane_text);
+ if (o.look.len > 0) @import("look.zig").lookAt(p, cl.id, o.look);
+ return o;
+ }
};
-var current: ?Current = null;
+var current: ?*Call = null;
-fn enter(p: ?*Pardes, id: usize, slot: *Slot, comptime call: []const u8) ?Current {
- const prev = current;
- current = .{ .p = p, .id = id, .slot = slot, .call = call };
- return prev;
+fn same(o: Outcome) Outcome {
+ return o;
}
/// The call into an addon under way, for a crash record: `in addon
-/// fake.so, paint`. Null outside one, or in a kind compiled in.
+/// fake, paint`. Null outside one.
pub fn calling(buf: []u8) ?[]const u8 {
- const cur = current orelse return null;
- const ops = cur.slot.ops orelse return null;
- for (listed[0..nlisted]) |*l| if (l.ops == ops) {
- if (l.lib == null) return null;
- return std.fmt.bufPrint(buf, "in addon {s}, {s}", .{ std.fs.path.basename(l.path.slice()), cur.call }) catch null;
- };
- return null;
-}
-
-fn leave(prev: ?Current) void {
- const done = current.?;
- current = prev;
- // A Look the kind asked for, once its call is over: it may change
- // the panes the call was among.
- if (done.look.len > 0) if (done.p) |p| @import("look.zig").lookAt(p, done.id, done.look.slice());
+ const cl = current orelse return null;
+ return std.fmt.bufPrint(buf, "in addon {s}, {s}", .{ cl.slot.nameOf(), cl.name }) catch null;
}
pub const api: c.pa_api = .{
.size = @sizeOf(c.pa_api),
- .alloc = apiAlloc,
- .free = apiFree,
- .job = apiJob,
- .look = apiLook,
- .say = apiSay,
- .text = apiText,
- .seed = apiSeed,
+ .alloc = struct {
+ fn f(n: usize) callconv(.c) ?*anyopaque {
+ const cl = current orelse return null;
+ const mem = cl.slot.arena.allocator().alignedAlloc(u8, .@"16", @max(n, 1)) catch return null;
+ @memset(mem, 0);
+ if (checked) cl.slot.used.append(cl.slot.gpa, mem) catch {};
+ return mem.ptr;
+ }
+ }.f,
+ .free = struct {
+ fn f(ptr: ?*anyopaque, n: usize) callconv(.c) void {
+ const cl = current orelse return;
+ const at: [*]align(16) u8 = @ptrCast(@alignCast(ptr orelse return));
+ if (checked) for (cl.slot.used.items, 0..) |m, i| if (m.ptr == at) {
+ _ = cl.slot.used.swapRemove(i);
+ break;
+ };
+ cl.slot.arena.allocator().free(at[0..@max(n, 1)]);
+ }
+ }.f,
+ .job = struct {
+ /// Its input kept for the host, which runs it (Effect.job).
+ fn f(in: ?*const anyopaque, n: u32) callconv(.c) u32 {
+ const cl = current orelse return 0;
+ const p = cl.p orelse return 0;
+ if (cl.quiet) return 0;
+ const s = cl.slot;
+ const free = for (&s.jobs) |*j| {
+ if (j.* == null) break j;
+ } else return 0;
+ const copy = s.gpa.dupe(u8, if (in) |b| @as([*]const u8, @ptrCast(b))[0..n] else "") catch return 0;
+ s.tokens +%= 1;
+ if (s.tokens == 0) s.tokens = 1;
+ free.* = .{ .token = s.tokens, .in = copy };
+ const pane = p.panes[cl.id] orelse return 0;
+ p.emit(.{ .job = .{ .pane = @intCast(cl.id), .serial = pane.serial, .token = s.tokens } });
+ return s.tokens;
+ }
+ }.f,
+ .look = struct {
+ fn f(text: [*c]const u8, n: u32) callconv(.c) void {
+ const cl = current orelse return;
+ if (!cl.quiet and text != null) cl.look.set(text[0..n]);
+ }
+ }.f,
+ .say = struct {
+ fn f(text: [*c]const u8, n: u32, err: c_int) callconv(.c) void {
+ const cl = current orelse return;
+ if (cl.quiet or text == null) return;
+ // Refusing its open: the opener says why.
+ if (err != 0 and cl.opening) return cl.refused.set(text[0..n]);
+ cl.said.set(text[0..n]);
+ cl.err = err != 0;
+ }
+ }.f,
+ .text = struct {
+ /// The pane is made as the call ends; its serial is the next.
+ fn f(name: [*c]const u8, name_len: u32, bytes: [*c]const u8, n: u32) callconv(.c) u32 {
+ const cl = current orelse return 0;
+ const p = cl.p orelse return 0;
+ if (cl.quiet or name == null or cl.pane_name.len > 0) return 0;
+ cl.pane_name.set(name[0..name_len]);
+ cl.pane_text = p.scratch.allocator().dupe(u8, if (bytes == null) "" else bytes[0..n]) catch "";
+ return p.next_serial + 1;
+ }
+ }.f,
+ .seed = struct {
+ /// The same pane seeds the same on every machine.
+ fn f() callconv(.c) u64 {
+ const cl = current orelse return seed_base;
+ const serial: u32 = if (cl.p) |p| (if (p.panes[cl.id]) |pane| pane.serial else 0) else 0;
+ return std.hash.Wyhash.hash(seed_base, std.mem.asBytes(&serial));
+ }
+ }.f,
};
/// What every pane's seed is made from: 0, or `--addon-test`'s `seed N`.
pub var seed_base: u64 = 0;
-fn apiSeed() callconv(.c) u64 {
- const cur = current orelse return seed_base;
- const serial: u32 = if (cur.p) |p| (if (p.panes[cur.id]) |pane| pane.serial else 0) else 0;
- return std.hash.Wyhash.hash(seed_base, std.mem.asBytes(&serial));
-}
-
-fn apiAlloc(n: usize) callconv(.c) ?*anyopaque {
- const cur = current orelse return null;
- const mem = cur.slot.arena.allocator().alignedAlloc(u8, .@"16", @max(n, 1)) catch return null;
- @memset(mem, 0);
- return mem.ptr;
-}
-
-fn apiFree(ptr: ?*anyopaque, n: usize) callconv(.c) void {
- const cur = current orelse return;
- const at: [*]align(16) u8 = @ptrCast(@alignCast(ptr orelse return));
- cur.slot.arena.allocator().free(at[0..@max(n, 1)]);
-}
-
-fn apiLook(text: [*c]const u8, n: u32) callconv(.c) void {
- if (current == null or current.?.painting or text == null) return;
- current.?.look.set(text[0..n]);
-}
-
-fn apiSay(text: [*c]const u8, n: u32, err: c_int) callconv(.c) void {
- const cur = current orelse return;
- if (cur.painting or text == null) return;
- const said = text[0..n];
- // Refusing its open: the opener says so, as it says why it could not.
- if (err != 0 and cur.opening) return cur.slot.refused.set(said);
- const p = cur.p orelse return;
- if (err != 0) p.reportFailure(cur.id, said) else p.setMessage(cur.id, said);
-}
-
-fn apiText(name: [*c]const u8, name_len: u32, bytes: [*c]const u8, n: u32) callconv(.c) u32 {
- const cur = current orelse return 0;
- if (cur.painting or name == null) return 0;
- const p = cur.p orelse return 0;
- const dir = std.fs.path.dirname(cur.slot.path) orelse p.sessionDir();
- const path = std.fmt.allocPrint(p.gpa, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), name[0..name_len] }) catch return 0;
- const content = p.gpa.dupe(u8, if (bytes == null) "" else bytes[0..n]) catch {
- p.gpa.free(path);
- return 0;
- };
- const free = p.freeSlot() orelse {
- p.gpa.free(path);
- p.gpa.free(content);
- return 0;
- };
+fn textPane(p: *Pardes, id: usize, slot: *const Slot, name: []const u8, text: []const u8) void {
+ const dir = std.fs.path.dirname(slot.path) orelse p.sessionDir();
+ const free = p.freeSlot() orelse return;
+ const path = std.fmt.allocPrint(p.gpa, "{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), name }) catch return;
+ const content = p.gpa.dupe(u8, text) catch return p.gpa.free(path);
const history = pardes.panes.File.History.create(p.gpa) catch {
p.gpa.free(path);
- p.gpa.free(content);
- return 0;
+ return p.gpa.free(content);
};
const pane = p.newDocPane(free) catch {
p.gpa.destroy(history);
p.gpa.free(path);
- p.gpa.free(content);
- return 0;
+ return p.gpa.free(content);
};
pane.file = .{ .path = path, .content = content, .history = history };
pane.body.cur_pinned = true;
- @import("exec.zig").placeNew(p, cur.id, cur.id, free, .doc);
- return pane.serial;
-}
-
-/// A kind's job: its input copied, run on a thread of its own (inline in
-/// a test), its answer kept for `step` to hand back on the loop.
-const Job = struct {
- token: u32,
- gpa: std.mem.Allocator,
- run: *const fn ([*c]const u8, u32, [*c]c.pa_out) callconv(.c) u32,
- in: []u8,
- out: []u8 = &.{},
- thread: ?std.Thread = null,
- done: std.atomic.Value(bool) = .init(false),
-
- fn work(j: *Job) void {
- var out: c.pa_out = .{ .alloc = outAlloc, .ctx = j };
- const n = j.run(j.in.ptr, @intCast(j.in.len), &out);
- j.out = j.out[0..@min(n, j.out.len)];
- j.done.store(true, .release);
- }
-
- fn outAlloc(out: [*c]c.pa_out, n: usize) callconv(.c) ?*anyopaque {
- const j: *Job = @ptrCast(@alignCast(out.*.ctx));
- if (j.out.len > 0) return null;
- j.out = j.gpa.alloc(u8, n) catch return null;
- return j.out.ptr;
- }
-
- fn wait(j: *Job) void {
- if (j.thread) |t| t.join();
- j.thread = null;
- }
-
- fn destroy(j: *Job) void {
- j.gpa.free(j.in);
- j.gpa.free(j.out);
- j.gpa.destroy(j);
- }
-};
-
-/// A test's jobs (and --addon-test's) run at once, their answers landing
-/// at the step's end.
-pub var inline_jobs = builtin.is_test;
-
-fn apiJob(in: ?*const anyopaque, n: u32) callconv(.c) u32 {
- const cur = current orelse return 0;
- if (cur.painting) return 0;
- const slot = cur.slot;
- const run = (slot.ops orelse return 0).job_run orelse return 0;
- const free_at = for (&slot.jobs) |*j| {
- if (j.* == null) break j;
- } else return 0;
- const job = slot.gpa.create(Job) catch return 0;
- slot.tokens +%= 1;
- if (slot.tokens == 0) slot.tokens = 1;
- job.* = .{ .token = slot.tokens, .gpa = slot.gpa, .run = run, .in = slot.gpa.dupe(u8, if (in) |b| @as([*]const u8, @ptrCast(b))[0..n] else "") catch {
- slot.gpa.destroy(job);
- return 0;
- } };
- if (!inline_jobs) {
- if (std.Thread.spawn(.{}, Job.work, .{job})) |t| job.thread = t else |_| job.work();
- } else job.work();
- free_at.* = job;
- return job.token;
+ @import("exec.zig").placeNew(p, id, id, free, .doc);
}
// ---- small things ----
pub fn Buf(comptime n: usize) type {
return struct {
+ pub const replica = .{ .len = .{.{ "bytes", "len" }} };
bytes: [n]u8 = undefined,
- len: std.math.IntFittingRange(0, n) = 0,
+ len: u16 = 0,
const Self = @This();
pub fn set(b: *Self, s: []const u8) void {
@@ -1207,22 +819,20 @@ pub fn Buf(comptime n: usize) type {
};
}
-test "a kind's head is checked before it is taken" {
+test "a kind's head is checked before a core takes it, and a core's kinds are its own" {
const Fake = struct {
fn paint(_: ?*anyopaque, _: [*c]const c.pa_window, _: [*c]c.pa_paint) callconv(.c) void {}
};
var ops = std.mem.zeroInit(Ops, .{ .head = std.mem.zeroInit(c.pa_head, .{ .size = @sizeOf(c.pa_head), .abi = abi, .version = 3 }), .paint = Fake.paint });
@memcpy(ops.head.name[0..5], "zzzab");
- var wrong = ops;
- wrong.head.abi = (abi_major + 1) << 16;
- @memcpy(wrong.head.name[0..5], "zzzac");
- try std.testing.expectEqualStrings("refused: wants abi 2, this pardes speaks 1", register(&wrong, "/x/wrong.so", null).?);
- try std.testing.expect(register(&ops, "", null) == null);
- try std.testing.expect(byName("zzzab") == &ops);
- try std.testing.expectEqualStrings("refused: kind zzzab is here already", register(&ops, "", null).?);
- _ = unregister("zzzab");
- try std.testing.expect(byName("zzzab") == null);
- forgetRefused("/x/wrong.so");
- forgetRefused("");
- for (kinds()) |*l| try std.testing.expect(!std.mem.startsWith(u8, l.nameOf(), "zzza"));
+ var a: Registry = .init();
+ var b: Registry = .init();
+ var words: [64]LoadedWord = undefined;
+ try std.testing.expect(a.take(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 image is here already", b.take(loadedOf(&@import("image.zig").kind, "", @splat(1), &words), "").?);
+ a.drop(a.byName("zzzab").?);
+ try std.testing.expect(a.byName("zzzab") == null);
}
diff --git a/src/Output.zig b/src/Output.zig
index 37865fd8..ecd0666b 100644
--- a/src/Output.zig
+++ b/src/Output.zig
@@ -899,8 +899,8 @@ fn helpContent(p: *const Pardes, gpa: std.mem.Allocator, prefix: []const u8) ![]
try w.print("{s}\n", .{row.line});
}
// An addon's words, after the core's, each saying whose it is.
- for (pardes.Kind.wordsOf()) |kw| {
- const path = if (kw.w.leader == null) "" else std.mem.span(kw.w.leader);
+ for (p.kinds.words[0..p.kinds.nwords]) |*kw| {
+ const path = kw.leader.slice();
if (!std.mem.startsWith(u8, path, prefix) or (prefix.len > 0 and path.len == 0)) continue;
var keys: [64]u8 = undefined;
var n: usize = 0;
@@ -913,7 +913,7 @@ fn helpContent(p: *const Pardes, gpa: std.mem.Allocator, prefix: []const u8) ![]
keys[n + 1] = ch;
n += 2;
};
- try w.print("{s: <12}{s} {s} (addon {s})\n", .{ keys[0..n], std.mem.span(kw.w.name), if (kw.w.doc == null) "" else std.mem.span(kw.w.doc), pardes.Kind.headName(kw.ops) });
+ try w.print("{s: <12}{s} {s} (addon {s})\n", .{ keys[0..n], kw.name.slice(), kw.doc.slice(), p.kinds.kinds[kw.kind].nameOf() });
}
if (show_calls_help) try w.writeAll(calls_help);
if (prefix.len == 0) try w.writeAll(language_footer);
diff --git a/src/addon_test.zig b/src/addon_test.zig
index 496b7504..3cbb5a64 100644
--- a/src/addon_test.zig
+++ b/src/addon_test.zig
@@ -1,25 +1,30 @@
//! `pardes --addon-test <addon.so> <script>`: an addon's author's test.
//! The addon is loaded into a session of its own (80x24, no terminal, no
-//! disk, its jobs run at once, its clock virtual and moved by `tick` and
-//! `settle` alone), a pane of it opened, and the
-//! script's lines run in turn, each printed with the pane's cells and
-//! placements after it; a script compares the output with what it wants
-//! as a snapshot script does (`diff`).
+//! disk, its jobs run at once, its clock virtual, starting at 1 s and moved
+//! by `tick` and `settle` alone), a pane of it opened, and the script's
+//! lines run in turn, each printed with the pane's tag (the kind's own
+//! prefix), cells and placements after it; a script compares the output
+//! with what it wants as a snapshot script does (`diff`).
//!
//! open [path] a pane of the kind (the first step opens one bare)
//! key hjkl<up> keys, a character or <up> <down> <left> <right>
//! <enter> <tab> <bs> <esc> <space> each
//! tick 100 the clock 100 ms on, the kind's ticks run
-//! settle ticks until the kind asks for none (at most 1000)
-//! size 60 20 the session's cells, the pane's share of them
+//! settle ticks until the kind asks for none (at most 1000):
+//! a kind that asks for ticks while idle fails it
+//! size 60 20 the session's terminal, the pane's share of it
+//! (`window CxR` says what the kind paints)
//! seed 7 what every pane's seed is made from (before open)
//! click 3 1 a left click at the body's column 3, row 1
+//! focus, blur the window takes the keyboard, or loses it
+//! dump the pane's record (its length) loaded into a
+//! fresh pane, which the script goes on with
+//! body, ctl the pane's body, or its ctl file's read
//! Word arg one of the kind's words, at its pane
//! # anything a comment, not printed
const std = @import("std");
const pardes = @import("pardes.zig");
const Kind = @import("Kind.zig");
-const Addon = @import("Addon.zig");
const Pardes = pardes.Pardes;
pub fn main(io: std.Io, gpa: std.mem.Allocator, lib: []const u8, script_path: ?[]const u8) u8 {
@@ -35,16 +40,8 @@ pub fn main(io: std.Io, gpa: std.mem.Allocator, lib: []const u8, script_path: ?[
}
fn run(io: std.Io, gpa: std.mem.Allocator, lib: []const u8, script_path: ?[]const u8, out: *std.Io.Writer) !void {
- Kind.inline_jobs = true;
- var why: Kind.Buf(160) = .{};
- if (!Addon.load(lib, &why)) {
- try out.print("addon-test: {s}\n", .{why.slice()});
- return error.KindRefused;
- }
- const ops = Kind.byName(why.slice()).?;
const script = try std.Io.Dir.cwd().readFileAlloc(io, script_path orelse return error.NoScript, gpa, .limited(1 << 20));
defer gpa.free(script);
-
// The regions and the C libraries' allocators a frontend sets up.
const allocs = pardes.memory.init(gpa);
defer pardes.memory.deinit();
@@ -54,6 +51,14 @@ fn run(io: std.Io, gpa: std.mem.Allocator, lib: []const u8, script_path: ?[]cons
defer pardes.syntax.stop();
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24, .state = .memory, .image_allocator = allocs.image, .pdf_allocator = allocs.pdf, .tree_sitter_allocator = allocs.tree_sitter, .frame_allocator = allocs.frame });
defer p.deinit();
+ // Loaded as `Addon load` loads it, the host's half here.
+ pardes.Addon.perform(p, .load, 0, lib);
+ const last = &p.kinds.kinds[p.kinds.n - 1];
+ if (last.refused) {
+ try out.print("addon-test: {s}\n", .{last.why.slice()});
+ return error.KindRefused;
+ }
+ const k = p.kinds.n - 1;
var now: u64 = std.time.ns_per_s;
p.advance(now);
var id: ?usize = null;
@@ -65,58 +70,97 @@ fn run(io: std.Io, gpa: std.mem.Allocator, lib: []const u8, script_path: ?[]cons
const verb_end = std.mem.indexOfScalar(u8, line, ' ') orelse line.len;
const verb = line[0..verb_end];
const arg = std.mem.trim(u8, line[verb_end..], " ");
- if (std.mem.eql(u8, verb, "seed")) {
+ const is = struct {
+ fn is(v: []const u8, w: []const u8) bool {
+ return std.mem.eql(u8, v, w);
+ }
+ }.is;
+ if (is(verb, "seed")) {
Kind.seed_base = std.fmt.parseInt(u64, arg, 10) catch 0;
- } else if (std.mem.eql(u8, verb, "open")) {
- id = try open(p, ops, if (arg.len > 0) arg else null);
- } else {
- if (id == null) id = try open(p, ops, null);
- const at = id.?;
- if (std.mem.eql(u8, verb, "key")) {
- try keys(p, arg);
- } else if (std.mem.eql(u8, verb, "tick")) {
- now += (std.fmt.parseInt(u64, arg, 10) catch 0) * std.time.ns_per_ms;
+ continue;
+ }
+ const opened = id == null or is(verb, "open");
+ if (opened) id = try open(p, k, if (is(verb, "open") and arg.len > 0) arg else null);
+ const at = id.?;
+ if (is(verb, "key")) {
+ try keys(p, arg);
+ } else if (is(verb, "tick")) {
+ now += (std.fmt.parseInt(u64, arg, 10) catch 0) * std.time.ns_per_ms;
+ p.advance(now);
+ } else if (is(verb, "settle")) {
+ var n: usize = 0;
+ while (Kind.wake(p) != null and n < 1000) : (n += 1) {
+ now = @max(now + 1, p.nextWake() orelse break);
p.advance(now);
- } else if (std.mem.eql(u8, verb, "settle")) {
- var n: usize = 0;
- while (pardes.Kind.wake(p) != null and n < 1000) : (n += 1) {
- now = @max(now + 1, p.nextWake() orelse break);
- p.advance(now);
- }
- if (n == 1000) try out.writeAll("settle: still asking for frames after 1000 ticks\n");
- } else if (std.mem.eql(u8, verb, "size")) {
- var it = std.mem.tokenizeScalar(u8, arg, ' ');
- const cols = std.fmt.parseInt(u16, it.next() orelse "80", 10) catch 80;
- const rows = std.fmt.parseInt(u16, it.next() orelse "24", 10) catch 24;
- p.update(.{ .resize = .{ .cols = cols, .rows = rows } });
- } else if (std.mem.eql(u8, verb, "click")) {
- var it = std.mem.tokenizeScalar(u8, arg, ' ');
- const col = std.fmt.parseInt(u16, it.next() orelse "0", 10) catch 0;
- const row = std.fmt.parseInt(u16, it.next() orelse "0", 10) catch 0;
- const body = bodyRect(p, at) orelse continue;
- for ([_]pardes.Mouse.Kind{ .press, .release }) |k|
- p.update(.{ .mouse = .{ .button = .left, .kind = k, .col = body.x + col, .row = body.y + row } });
- } else {
- p.active = at;
- if (!p.executeBuiltinLine(at, line)) try out.print("not a word here: {s}\n", .{verb});
}
+ if (n == 1000) try out.writeAll("settle: still asking for frames after 1000 ticks\n");
+ } else if (is(verb, "size")) {
+ var it = std.mem.tokenizeScalar(u8, arg, ' ');
+ const cols = std.fmt.parseInt(u16, it.next() orelse "80", 10) catch 80;
+ const rows = std.fmt.parseInt(u16, it.next() orelse "24", 10) catch 24;
+ p.update(.{ .resize = .{ .cols = cols, .rows = rows } });
+ } else if (is(verb, "click")) {
+ var it = std.mem.tokenizeScalar(u8, arg, ' ');
+ const col = std.fmt.parseInt(u16, it.next() orelse "0", 10) catch 0;
+ const row = std.fmt.parseInt(u16, it.next() orelse "0", 10) catch 0;
+ const body = window(p, at) orelse continue;
+ for ([_]pardes.Mouse.Kind{ .press, .release }) |mk|
+ p.update(.{ .mouse = .{ .button = .left, .kind = mk, .col = body.x + col, .row = body.y + row } });
+ } else if (is(verb, "focus") or is(verb, "blur")) {
+ p.update(.{ .focus = is(verb, "focus") });
+ } else if (is(verb, "dump")) {
+ id = try reloaded(p, gpa, at, out);
+ } else if (is(verb, "body") or is(verb, "ctl")) {
+ drain(p);
+ var fields: [4096]u8 = undefined;
+ const text = if (is(verb, "body")) try Kind.body(p.panes[at].?, p.scratch.allocator()) else Kind.ctlRead(p.panes[at].?, &fields);
+ var rows = std.mem.splitScalar(u8, std.mem.trimEnd(u8, text, "\n"), '\n');
+ while (rows.next()) |row| try out.print("={s}\n", .{row});
+ continue;
+ } else if (!is(verb, "open")) {
+ p.active = at;
+ if (!p.executeBuiltinLine(at, line)) try out.print("not a word here: {s}\n", .{verb});
}
drain(p);
- if (id) |at| try show(p, gpa, at, out);
+ try show(p, gpa, id.?, out, opened or is(verb, "size"));
}
}
-fn open(p: *Pardes, ops: *const Kind.Ops, path: ?[]const u8) !usize {
+fn open(p: *Pardes, k: u8, path: ?[]const u8) !usize {
const free = p.freeSlot() orelse return error.NoPaneSlots;
- _ = if (path) |at| try Kind.openFile(p, free, ops, at) else try Kind.open(p, free, ops, null, null);
+ _ = if (path) |at| try Kind.openFile(p, free, k, at) else try Kind.open(p, free, k, null, null);
pardes.exec.placeNew(p, p.active, null, free, .doc);
p.active = free;
drain(p);
return free;
}
+/// The pane's record loaded into a fresh pane in its place, as a Restore
+/// would: what the script goes on with.
+fn reloaded(p: *Pardes, gpa: std.mem.Allocator, at: usize, out: *std.Io.Writer) !usize {
+ const slot = p.panes[at].?.addon.?;
+ const record = try pardes.dump.slotRecord(slot);
+ defer if (record) |r| slot.gpa.free(r);
+ try out.print("dump {d} bytes\n", .{if (record) |r| r.len else 0});
+ var bytes: std.Io.Writer.Allocating = .init(gpa);
+ defer bytes.deinit();
+ try pardes.dump.slotWrite(slot, gpa, &bytes.writer);
+ const fresh = try slot.gpa.create(Kind.Slot);
+ try pardes.dump.slotRead(fresh, slot.gpa, bytes.written());
+ try p.removePane(at, null);
+ const free = p.freeSlot() orelse return error.NoPaneSlots;
+ const pane = try p.newDocPane(free);
+ pane.addon = fresh;
+ pardes.dump.reviveSlot(p, free, fresh);
+ pardes.exec.placeNew(p, p.active, null, free, .doc);
+ p.active = free;
+ drain(p);
+ return free;
+}
+
+/// Its effects done as a host does them: its jobs here, at once.
fn drain(p: *Pardes) void {
- while (p.nextEffect()) |_| {}
+ while (p.nextEffect()) |e| if (e == .job) p.perform(e);
}
fn keys(p: *Pardes, text: []const u8) !void {
@@ -140,25 +184,32 @@ fn keys(p: *Pardes, text: []const u8) !void {
break :blk c;
};
p.update(.{ .key = .{ .cp = cp } });
+ drain(p);
}
}
-fn bodyRect(p: *Pardes, id: usize) ?pardes.Rect {
+/// The cells the kind paints: its body less the notices over it.
+fn window(p: *Pardes, id: usize) ?pardes.Rect {
var arena: std.heap.ArenaAllocator = .init(p.gpa);
defer arena.deinit();
const s = p.render(arena.allocator()) catch return null;
- const r = s.region(.body, id) orelse return null;
- return r.rect;
+ var r = (s.region(.body, id) orelse return null).rect;
+ const shown = p.panes[id].?.notices.span();
+ r.y += shown;
+ r.h -|= shown;
+ return r;
}
/// The pane's body, a line a row (an unpainted cell a space), then each
-/// placement over it.
-fn show(p: *Pardes, gpa: std.mem.Allocator, id: usize, out: *std.Io.Writer) !void {
+/// placement over it; `window CxR` first when asked.
+fn show(p: *Pardes, gpa: std.mem.Allocator, id: usize, out: *std.Io.Writer, size: bool) !void {
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
const s = try p.render(arena.allocator());
const pane = p.panes[id] orelse return out.writeAll("(the pane is gone)\n");
- try out.print("tag {s}\n", .{try pardes.tagline.tagPrefix(p, pane)});
+ const slot = pane.addon.?;
+ if (size) if (window(p, id)) |w| try out.print("window {d}x{d}\n", .{ w.w, w.h });
+ try out.print("tag {s}\n", .{if (slot.prefix.len > 0) slot.prefix.slice() else slot.nameOf()});
const r = (s.region(.body, id) orelse return).rect;
for (r.y..r.y + r.h) |y| {
var row: std.ArrayList(u8) = .empty;
diff --git a/src/builtins.zig b/src/builtins.zig
index 659e7cd5..a22872df 100644
--- a/src/builtins.zig
+++ b/src/builtins.zig
@@ -995,7 +995,7 @@ pub const Addon = struct {
pub const scope: Scope = .session;
pub const takes_arg = true;
pub fn run(c: Ctx) void {
- pardes.Kind.addonWord(c.p, c.id, c.arg);
+ pardes.Addon.run(c.p, c.id, c.arg);
}
};
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, &notes, &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);
+}
diff --git a/src/echo.zig b/src/echo.zig
index 89d02154..8c196816 100644
--- a/src/echo.zig
+++ b/src/echo.zig
@@ -614,7 +614,7 @@ const Rig = struct {
for (msgs.items) |m| gpa.free(m);
msgs.deinit(gpa);
}
- try g.screens.flush(g.a.core, g.peer, &msgs);
+ try g.screens.flush(g.a.core, g.peer, g.r.view, &msgs);
for (msgs.items) |m| if (g.r.screen(m)) |reply| {
_ = try g.screens.take(g.a.core, g.peer, reply);
};
diff --git a/src/exec.zig b/src/exec.zig
index b3cbbdd9..327d4683 100644
--- a/src/exec.zig
+++ b/src/exec.zig
@@ -918,7 +918,7 @@ pub fn executeBuiltinLine(p: *Pardes, id: usize, txt: []const u8) bool {
const head = std.meta.stringToEnum(Builtin, cmd[0 .. sp orelse break :blk null]) orelse break :blk null;
break :blk if (builtins.registry.takesArg(head)) head else null;
};
- const b = bi orelse return pardes.Kind.runLine(p, id, cmd);
+ const b = bi orelse return runLine(p, id, cmd);
p.exec_depth += 1;
defer p.exec_depth -= 1;
const typed = if (sp) |s| std.mem.trim(u8, cmd[s + 1 ..], " \t") else "";
@@ -1235,3 +1235,52 @@ test "a switch clicked with a selection held flips unless the selection says on
// A word that takes any argument still gets the selection.
try std.testing.expectEqualStrings("Msg some text", withArg(p, "Msg", "some text"));
}
+
+// ---- the words addons bring (Kind.Registry's) ----
+
+/// A line whose first word is an addon's: run, and true; false for one
+/// that is no addon's word.
+fn runLine(p: *Pardes, id: usize, line: []const u8) bool {
+ const end = std.mem.indexOfAny(u8, line, " \t") orelse line.len;
+ const w = p.kinds.word(line[0..end]) orelse return false;
+ runWord(p, id, w, line, std.mem.trim(u8, line[end..], " \t"));
+ return true;
+}
+
+/// Word `w` from pane `id`: one that opens its kind opens a pane of it
+/// (`arg` its path), any other goes to the pane's kind, which must be
+/// the word's. Its argument is checked first.
+pub fn runWord(p: *Pardes, id: usize, w: *const pardes.Kind.Word, line: []const u8, arg: []const u8) void {
+ const name = w.name.slice();
+ var said: [256]u8 = undefined;
+ if (w.arg == pardes.Kind.c.PA_ARG_NONE and arg.len > 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} takes no argument", .{name}) catch name);
+ if (w.arg == pardes.Kind.c.PA_ARG_REQUIRED and arg.len == 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} needs an argument", .{name}) catch name);
+ if (w.opens != 0) {
+ const free = p.freeSlot() orelse return p.reportError(id, name, error.NoPaneSlots);
+ var path_buf: [4096]u8 = undefined;
+ const path = if (arg.len == 0 or std.fs.path.isAbsolute(arg)) arg else std.fmt.bufPrint(&path_buf, "{s}/{s}", .{ std.mem.trimEnd(u8, if (p.panes[id]) |pane| Pardes.paneDir(pane) else p.sessionDir(), "/"), arg }) catch arg;
+ const made = (if (path.len > 0) pardes.Kind.openFile(p, free, w.kind, path) else pardes.Kind.open(p, free, w.kind, null, null)) catch |err| return p.reportError(id, name, err);
+ if (made.addon.?.refused.len > 0) {
+ made.vweight = 0;
+ p.unplaced.set(free);
+ return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: {s}", .{ name, made.addon.?.refused.slice() }) catch name);
+ }
+ return placeNew(p, id, id, free, .doc);
+ }
+ const kind = p.kinds.kinds[w.kind].nameOf();
+ const here = if (p.panes[id]) |pane| if (pane.addon) |s| s.live and s.is(kind) else false else false;
+ if (!here) return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} is a {s} pane's word: run it in one", .{ name, kind }) catch name);
+ var reply: [160]u8 = undefined;
+ if (pardes.Kind.ctl(p, id, line, &reply)) |why| return p.reportFailure(id, std.fmt.bufPrint(&said, "{s}: {s}", .{ name, why }) catch name);
+ if (arg.len == 0) return;
+ // Its last value, one line a word, which a kind with no record is
+ // given again when its pane comes back.
+ const s = p.panes[id].?.addon.?;
+ var next: pardes.Kind.Buf(256) = .{};
+ var out: std.Io.Writer = .fixed(&next.bytes);
+ var lines = std.mem.tokenizeScalar(u8, s.values.slice(), '\n');
+ while (lines.next()) |l| if (!std.mem.eql(u8, l[0 .. std.mem.indexOfScalar(u8, l, ' ') orelse l.len], name)) out.print("{s}\n", .{l}) catch return;
+ out.print("{s} {s}\n", .{ name, arg }) catch return;
+ next.len = @intCast(out.buffered().len);
+ s.values = next;
+}
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 07624c20..eb8636be 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -1744,6 +1744,7 @@ fn localSession(
host_io.startWorkers(io, allocs);
defer host_io.stopWorkers();
var opts = opts_in;
+ opts.addons = pardes.Addon.scan();
opts.image_allocator = allocs.image;
opts.pdf_allocator = allocs.pdf;
opts.tree_sitter_allocator = allocs.tree_sitter;
@@ -1827,6 +1828,7 @@ fn localSession(
.test_clock = if (test_mode) host_io.testClock() else null,
.test_hz = if (std.c.getenv("PARDES_TEST_HZ")) |hz| @max(1, std.fmt.parseInt(u64, std.mem.span(hz), 10) catch 60) else 60,
};
+ defer shell.job_tasks.cancelAll(io);
defer shell.shutdownPtys();
// A virtual clock never blinks: goldens and feel series hold still.
g.no_blink = shell.test_clock != null;
@@ -3172,6 +3174,7 @@ const Shell = struct {
/// What drainQueue takes each wakeup, kept rather than rebuilt.
batch: Queue.Batch = .{},
pipe_tasks: *PipeTasks,
+ job_tasks: PipeTasks = .{},
inotify_fd: c_int,
watches: *file_watch.Table,
threads_ok: bool = false,
@@ -3545,6 +3548,7 @@ const Shell = struct {
.pipe = pipe,
.search = search,
.hand_over = handOver,
+ .job = kindJob,
};
/// The command panes' exits, told once their output is in (host_io).
@@ -3626,6 +3630,10 @@ const Shell = struct {
s.core.update(.{ .handed = .{ .seq = h.seq, .to = if (h.ok) h.words else "", .failure = if (h.ok) null else h.words } });
s.gpa.free(h.words);
},
+ .kind_job => |j| {
+ s.job_tasks.finish(s.io, j.id);
+ j.land(s.core);
+ },
.files_changed => check_files = true,
.fs_ready => {},
.window => if (s.gui != null) s.openAnotherWindow(null) else s.openTheWindow(),
@@ -4358,6 +4366,12 @@ fn pipe(ctx: ?*anyopaque, id: u32) void {
s.core.reportError(s.core.active, "pipe", error.WorkersUnavailable);
}
+fn kindJob(ctx: ?*anyopaque, pane: u8, serial: u32, token: u32) void {
+ const s = shellOf(ctx);
+ if (s.threads_ok) return host_io.KindJob.start(s.io, s.core, &s.job_tasks, pane, serial, token, s.queue, Queue.pushJob);
+ host_io.KindJob.runHere(s.core, pane, serial, token);
+}
+
fn search(ctx: ?*anyopaque, gen: u32) void {
const s = shellOf(ctx);
if (s.threads_ok) return pardes.Stream.start(s.io, s.gpa, s.core, gen, s.queue, Queue.pushSearch);
diff --git a/src/host_io.zig b/src/host_io.zig
index c00eb453..1808d266 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -66,9 +66,98 @@ pub const Host = struct {
/// Carry the dump of handover `seq` to another window on a worker
/// (Handover.start), which answers through the inbox's `handed`.
hand_over: ?*const fn (ctx: ?*anyopaque, seq: u32, bytes: []const u8) void = null,
+ /// Run job `token` of the pane in slot `pane` (whose serial is
+ /// `serial`) on a worker (`KindJob`), answering with the `job` event;
+ /// absent, it runs on the loop.
+ job: ?*const fn (ctx: ?*anyopaque, pane: u8, serial: u32, token: u32) void = null,
};
};
+/// A kind's job (Effect.job), taken off its pane: its input, its kind's
+/// `job_run` and generation; run on a worker (or here), its answer the
+/// core's `job` event. Its library is held until it lands (Addon.holdJob).
+pub const KindJob = struct {
+ gpa: std.mem.Allocator,
+ pane: u8,
+ serial: u32,
+ token: u32,
+ gen: u32,
+ ops: *const pardes.Kind.Ops,
+ in: []u8,
+ out: []u8 = &.{},
+ id: u32 = 0,
+
+ pub fn take(p: *pardes.Pardes, pane: u8, serial: u32, token: u32) ?*KindJob {
+ const pn = p.panes[pane] orelse return null;
+ const slot = pn.addon orelse return null;
+ const ops = slot.ops orelse return null;
+ if (pn.serial != serial or ops.job_run == null) return null;
+ const at = for (&slot.jobs) |*j| {
+ if (j.*) |job| if (job.token == token) break j;
+ } else return null;
+ const j = slot.gpa.create(KindJob) catch return null;
+ j.* = .{ .gpa = slot.gpa, .pane = pane, .serial = serial, .token = token, .gen = slot.gen, .ops = ops, .in = at.*.?.in };
+ at.* = null;
+ pardes.Addon.holdJob(ops, true);
+ return j;
+ }
+
+ pub fn run(j: *KindJob) void {
+ const Out = struct {
+ fn alloc(out: [*c]pardes.Kind.c.pa_out, n: usize) callconv(.c) ?*anyopaque {
+ const job: *KindJob = @ptrCast(@alignCast(out.*.ctx));
+ if (job.out.len > 0) return null;
+ job.out = job.gpa.alloc(u8, n) catch return null;
+ return job.out.ptr;
+ }
+ };
+ var out: pardes.Kind.c.pa_out = .{ .alloc = Out.alloc, .ctx = j };
+ const n = j.ops.job_run.?(j.in.ptr, @intCast(j.in.len), &out);
+ j.out = j.out[0..@min(n, j.out.len)];
+ }
+
+ /// Its answer to the core, and it gone.
+ pub fn land(j: *KindJob, p: *pardes.Pardes) void {
+ p.update(.{ .job = .{ .pane = j.pane, .serial = j.serial, .token = j.token, .gen = j.gen, .bytes = j.out } });
+ j.drop();
+ }
+
+ pub fn drop(j: *KindJob) void {
+ pardes.Addon.holdJob(j.ops, false);
+ j.gpa.free(j.in);
+ j.gpa.free(j.out);
+ j.gpa.destroy(j);
+ }
+
+ /// On a worker of `io`, posting itself to `sink` done: a host's `job`.
+ pub fn start(io: std.Io, p: *pardes.Pardes, tasks: *selection_pipe.Tasks, pane: u8, serial: u32, token: u32, ctx: ?*anyopaque, sink: *const fn (?*anyopaque, *KindJob) void) void {
+ const j = take(p, pane, serial, token) orelse return;
+ next_id +%= 1;
+ j.id = next_id | 0x8000_0000;
+ const work = struct {
+ fn work(job: *KindJob, c: ?*anyopaque, s: *const fn (?*anyopaque, *KindJob) void) void {
+ job.run();
+ s(c, job);
+ }
+ }.work;
+ if (tasks.full()) return here(j, p);
+ const future = io.concurrent(work, .{ j, ctx, sink }) catch return here(j, p);
+ _ = tasks.add(.{ .id = j.id, .future = future });
+ }
+
+ var next_id: u32 = 0;
+
+ fn here(j: *KindJob, p: *pardes.Pardes) void {
+ j.run();
+ j.land(p);
+ }
+
+ /// No workers (a test, the fallback): it runs on the loop, at once.
+ pub fn runHere(p: *pardes.Pardes, pane: u8, serial: u32, token: u32) void {
+ here(take(p, pane, serial, token) orelse return, p);
+ }
+};
+
/// PARDES_TEST_CLOCK makes a shell's `Host.now` answer a virtual clock
/// instead of `std.Io.Clock.awake`, moved only when a timed wait runs out --
/// straight to the core's next wake -- so the snapshot harness sees the same
@@ -82,7 +171,6 @@ pub fn testClock() ?u64 {
/// loop (pdf_view.workers). `stopWorkers` is its defer.
pub fn startWorkers(io: std.Io, allocs: pardes.memory.Allocators) void {
pardes.image.start(io, allocs.image);
- pardes.Addon.scan();
if (comptime pardes.pdf_enabled) {
pardes.pdf.start(allocs.pdf);
pardes.panes.Pdf.workers = io;
@@ -253,6 +341,8 @@ pub const Inbox = struct {
/// How a handover went (Handover.zig): `words` is where it went, or
/// why it did not; gpa-owned.
handed: struct { seq: u32, ok: bool, words: []u8 },
+ /// A kind's job done on a worker (KindJob), never dropped.
+ kind_job: *KindJob,
files_changed,
fs_ready,
/// The daemon asks for the window again (`pardes-gui --attach`).
@@ -272,6 +362,7 @@ pub const Inbox = struct {
},
.search => |b| b.deinit(gpa),
.handed => |h| gpa.free(h.words),
+ .kind_job => |j| j.drop(),
.adopted => |a| if (a.fd >= 0) {
_ = libc.close(a.fd);
},
@@ -284,7 +375,7 @@ pub const Inbox = struct {
pub const output_capacity = capacity - pardes.MAX_PANES;
pub const Batch = struct {
- items: [capacity + selection_pipe.Tasks.capacity + 2]Msg = undefined,
+ items: [capacity + 2 * selection_pipe.Tasks.capacity + 2]Msg = undefined,
len: usize = 0,
pub fn slice(batch: *Batch) []Msg {
@@ -312,7 +403,7 @@ pub const Inbox = struct {
readers: [pardes.MAX_PANES]?u32 = @splat(null),
waiting: usize = 0,
lsp_id: ?u32 = null,
- completions: [selection_pipe.Tasks.capacity + 1]Msg = undefined,
+ completions: [2 * selection_pipe.Tasks.capacity + 1]Msg = undefined,
completion_len: usize = 0,
pub fn lock(q: *Inbox) void {
@@ -402,7 +493,7 @@ pub const Inbox = struct {
}
switch (m) {
.files_changed => q.files_changed = true,
- .lsp, .pipe => {
+ .lsp, .pipe, .kind_job => {
if (m == .lsp) {
if (q.lsp_id != m.lsp.id) {
q.unlock();
@@ -554,6 +645,11 @@ pub const Inbox = struct {
}
/// A `|` job's answer (selection_pipe.start's sink).
+ pub fn pushJob(ctx: ?*anyopaque, j: *KindJob) void {
+ const q: *Inbox = @ptrCast(@alignCast(ctx.?));
+ q.push(.{ .kind_job = j });
+ }
+
pub fn pushPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void {
const q: *Inbox = @ptrCast(@alignCast(ctx.?));
q.push(.{ .pipe = response });
diff --git a/src/image.zig b/src/image.zig
index 80261d91..b5efc5fd 100644
--- a/src/image.zig
+++ b/src/image.zig
@@ -505,20 +505,14 @@ fn paint(ctx: ?*anyopaque, win: [*c]const Kind.c.pa_window, out: [*c]Kind.c.pa_p
const st: *const State = @ptrCast(@alignCast(ctx orelse return));
const w = win.*;
const o = &out.*;
- const prefix = std.fmt.bufPrint(&o.prefix, "{s} petscii:{s} palette:{s} ascii:{s} {s}", .{
- config.tag_image,
- if (w.glyphs != 0) "on" else "off",
- if (w.palette == 0) "commodore" else "terminal",
- if (w.ascii != 0) "on" else "off",
- st.path,
- }) catch &o.prefix;
- o.prefix_len = @intCast(prefix.len);
+ // `img`, which the core follows with its art words and the path.
+ @memcpy(o.prefix[0..config.tag_image.len], config.tag_image);
+ o.prefix_len = config.tag_image.len;
if (st.rgba.len == 0 or o.cap == 0) return;
o.placements[0] = std.mem.zeroInit(Kind.c.pa_placement, .{
.rgba = st.rgba.ptr,
.w = st.w,
.h = st.h,
- .stride = st.w * 4,
.src = .{ .x = 0, .y = 0, .w = st.w, .h = st.h },
.dst = .{ .x = 0, .y = 0, .w = w.cols, .h = w.rows },
});
diff --git a/src/keys.zig b/src/keys.zig
index 003f39b5..d9925838 100644
--- a/src/keys.zig
+++ b/src/keys.zig
@@ -229,7 +229,7 @@ pub fn handleKey(p: *Pardes, key: Key) void {
}
// A kind's pane that takes keys takes them all, as a raw terminal
// does, but Esc, which leaves (and Shift-Esc, above).
- if (header == null and pane.focus == .body and pane.prompt == .none) if (pane.addon) |slot| if (slot.takesKeys()) {
+ if (header == null and pane.focus == .body and pane.prompt == .none) if (pane.addon) |slot| if (pardes.Kind.takesKeys(p, slot)) {
p.prefix = .none;
if (key.cp == Key.escape and !key.ctrl and !key.alt) return exec.runBuiltin(p, .Last, p.active, "", null);
if (pardes.Kind.key(p, p.active, key)) return;
@@ -473,8 +473,8 @@ fn leaderKey(p: *Pardes, key: Key) void {
}
}
// An addon's word, after the core's (a path the core has is no addon's).
- switch (pardes.Kind.leader(pfx)) {
- .word => |w| pardes.Kind.runWord(p, p.active, w, std.mem.span(w.w.name), ""),
+ switch (p.kinds.leader(pfx)) {
+ .word => |w| exec.runWord(p, p.active, w, w.name.slice(), ""),
.prefix => p.prefix = .leader,
.none => {},
}
diff --git a/src/kinds/pardes_addon.h b/src/kinds/pardes_addon.h
index c3fbc097..c6aa448b 100644
--- a/src/kinds/pardes_addon.h
+++ b/src/kinds/pardes_addon.h
@@ -13,18 +13,26 @@
* echo 'Addon load ./game.so' > $PARDES_MOUNT/ctl
* pardes --addon-test ./game.so game.script a test, as a script
*
- * addons/fake/fake.c implements every function; it is the template.
+ * Conventions: init, ctl and load return PA_OK (0), or nonzero to refuse;
+ * key, mouse and address return nonzero when the event or text was theirs.
+ * The pa_api pointer handed to init is kept by the addon; it is about the
+ * pane of the current call.
+ *
+ * addons/fake/fake.c implements every function; it is the template, and
+ * addons/snake/snake.c a game in it.
*/
#ifndef PARDES_ADDON_H
#define PARDES_ADDON_H
#include <stddef.h>
#include <stdint.h>
+#include <string.h> /* memcpy, for pa_copy and PA_POD */
/* Major in the high half: an addon of another major is refused. */
-#define PA_ABI_MAJOR 1u
+#define PA_ABI_MAJOR 2u
#define PA_ABI_MINOR 0u
#define PA_ABI ((PA_ABI_MAJOR << 16) | PA_ABI_MINOR)
+#define PA_OK 0
/* ---- the picture ---- */
@@ -52,42 +60,48 @@ typedef struct pa_cell {
} pa_cell;
/* The pane's body, which `paint` writes: `rows` rows of `cols` cells,
- * `stride` cells apart, every one zero (unpainted) when the call begins. */
+ * `stride` cells apart, every one zero (unpainted) when the call begins.
+ * How a frontend shows the placements (glyph art, its palette) is the
+ * core's words' (Petscii, Palette, Ascii), never the addon's. */
typedef struct pa_window {
pa_cell *cells;
uint32_t stride;
uint16_t rows, cols;
/* A cell's size in pixels, as the frontend draws it. */
uint16_t cell_w, cell_h;
- /* The pane has the keyboard. */
- uint8_t focused;
- /* How the frontend shows this pane's placements, which the core words
- * Petscii, Palette and Ascii set: as glyph art (Petscii on, or a frontend
- * with no pixels), in the commodore (0) or the terminal's (1) palette,
- * with letters among the glyphs. An addon may say so in its prefix. */
- uint8_t glyphs, palette, ascii;
+ /* The pane has the keyboard in some view: for the picture ("paused"). */
+ uint8_t focused, pad[3];
} pa_window;
typedef struct pa_rect { uint32_t x, y, w, h; } pa_rect; /* pixels */
typedef struct pa_cells { uint16_t x, y, w, h; } pa_cells; /* cells of the window */
/* A picture over the cells: the `src` part of an RGBA raster (straight
- * alpha, `stride` bytes a row), over the `dst` cells; at `at` pixels in
- * them, or fitted there (no larger than it is) when `at.w` is 0. The
- * raster is the addon's and must stay as it is until the pane's next paint
- * or close. `key` is the pixels' identity in the pane: one key, one
- * picture, so a frontend keeps what it uploaded under it. */
+ * alpha, packed rows of w*4 bytes), over the `dst` cells, at `at` pixels in
+ * them, or fitted there (no larger than it is) when at.w is 0. The raster
+ * is the addon's and must stay as it is until the pane's next paint or
+ * close. `key` names the pixels in this pane: one key, one picture, so a
+ * frontend keeps what it uploaded under it. `from` is 0, or the key of a
+ * picture a frontend may still hold of which rows dirty_y .. dirty_y +
+ * dirty_h of this one alone differ (a frontend that holds it may patch
+ * those rows; any other uploads the whole). */
typedef struct pa_placement {
const uint8_t *rgba;
- uint32_t w, h, stride;
+ uint32_t w, h;
pa_rect src;
pa_cells dst;
pa_rect at;
uint64_t key;
+ uint64_t from;
+ uint32_t dirty_y, dirty_h;
} pa_placement;
/* What `paint` hands back beside the cells: placements (room for `cap`,
- * lowest first) and the start of the pane's tag (empty: the pane's path). */
+ * at least 64, lowest first) and the start of the pane's tag, which the
+ * core follows with one space, its own words for a picture shown
+ * (`petscii:on palette:commodore ascii:off`, when this frame placed one),
+ * and the pane's path; empty, the kind's name. paint is called once a
+ * frame, body and prefix together, never over no cells. */
typedef struct pa_paint {
pa_placement *placements;
uint32_t cap, n;
@@ -165,17 +179,22 @@ typedef struct pa_out {
#define PA_ARG_OPTIONAL 1
#define PA_ARG_REQUIRED 2
-/* A word the addon brings. Written to a pane of its kind, it goes to `ctl`;
- * one that `opens` it, run anywhere else, opens a pane of the kind
- * (`open` with its argument as the path, or none). */
+/* A word the addon brings. An `opens` word opens a pane of the kind
+ * wherever it runs (`open` with its argument as the path, or none); any
+ * other goes to `ctl` of the pane of its kind it is written to, refused
+ * elsewhere. The core checks `arg` before the call; ctl is required when a
+ * word has opens 0. */
typedef struct pa_word {
- uint32_t size;
const char *name;
/* The keys after the leader (Space) that run it, "ng" for SPC n g, or NULL. */
const char *leader;
- uint8_t arg, opens, pad[2];
+ uint8_t arg, opens, pad[6];
const char *doc;
} pa_word;
+#define PA_WORD(nm, ld, a, op, dc) { .name = (nm), .leader = (ld), .arg = (a), .opens = (op), .doc = (dc) }
+
+/* tick's flags: the pane has the keyboard in some view. */
+#define PA_FOCUSED 0x1u
typedef struct pa_head {
uint32_t size, abi;
@@ -190,7 +209,7 @@ typedef struct pa_head {
typedef struct pa_ops {
pa_head head;
- /* Once, at load: nonzero refuses the addon. */
+ /* Once, at load: PA_OK, or nonzero to refuse the addon. */
int (*init)(const pa_api *api);
/* A new pane (path and bytes NULL for one a word opened): its state,
* which every call below is handed. NULL with nothing said is a pane
@@ -198,12 +217,17 @@ typedef struct pa_ops {
void *(*open)(const char *path, const uint8_t *bytes, uint64_t len);
/* The pane closes; its arena is freed after. */
void (*close)(void *st);
- /* Time has come to `now_ns`: step, and say when next (0: never). Absent,
- * the pane repaints when the core repaints. */
- uint64_t (*tick)(void *st, uint64_t now_ns);
+ /* Time has come to now_ns (monotonic, any origin; --addon-test's starts
+ * at 1 s): step, and return the absolute time of the next step, 0 for
+ * none. Called when that time comes and once after open, a taken key or
+ * mouse, ctl, job_done, load, and a change of PA_FOCUSED in `flags`: a
+ * kind that returned 0 is woken by the next thing that happens to it.
+ * Absent, the pane repaints when the core repaints. */
+ uint64_t (*tick)(void *st, uint64_t now_ns, uint32_t flags);
/* Required. Pure: the same state paints the same, and changes nothing. */
void (*paint)(void *st, const pa_window *win, pa_paint *out);
- /* Every key but Esc, which leaves; nonzero if taken. Absent: no keys. */
+ /* Every key but Esc, which leaves; nonzero if taken. A key not taken goes
+ * to the editor's own navigation. Absent: no keys. */
int (*key)(void *st, const pa_key *k);
/* Nonzero if taken. Absent: a click focuses the pane. */
int (*mouse)(void *st, const pa_mouse *m);
@@ -214,18 +238,23 @@ typedef struct pa_ops {
* where the pane is now, as such a text (jumps, Recent, a Restore). */
int (*address)(void *st, const char *text, uint32_t n);
uint32_t (*where)(void *st, char *buf, uint32_t n);
- /* One of the addon's words, as written (`Fit width`): 0 taken, else
- * refused with `reply`. Required with words. */
+ /* One of the addon's words, as written (`Fit width`): PA_OK, else
+ * refused with `reply`. Required when a word has opens 0. */
int (*ctl)(void *st, const char *line, uint32_t n, char *reply, uint32_t cap);
- /* What the pane's ctl file reads after the core's fields. */
+ /* What the pane's ctl file reads after the core's fields: `name value`
+ * lines. */
uint32_t (*ctl_read)(void *st, char *buf, uint32_t n);
/* On a worker thread, pure over `in`: the answer's length. */
uint32_t (*job_run)(const uint8_t *in, uint32_t n, pa_out *out);
/* Back on pardes's thread; `out` is gone after the call. */
void (*job_done)(void *st, uint32_t token, const uint8_t *out, uint32_t n);
- /* The pane's record for a dump (its length; at most `n`), and back. With
- * none, a Restore reopens the pane from its path and its `where`. */
+ /* The pane's record for a dump: at most `n` bytes, n at least 1 MiB; 0 is
+ * no record (load is not called and the pane reopens from its path, its
+ * `where` and its words' last values), and so is having no dump. */
uint32_t (*dump)(void *st, uint8_t *buf, uint32_t n);
+ /* On the state open(path, bytes, len) returned, with the writer's
+ * head.version: PA_OK, or nonzero to refuse (the pane waits as a
+ * placeholder, its record kept). */
int (*load)(void *st, const uint8_t *buf, uint32_t n, uint32_t version);
} pa_ops;
@@ -235,11 +264,11 @@ typedef struct pa_ops {
_Static_assert(sizeof(pa_cell) == 20, "pa_cell");
_Static_assert(sizeof(pa_window) == 24, "pa_window");
_Static_assert(sizeof(pa_rect) == 16 && sizeof(pa_cells) == 8, "pa_rect");
-_Static_assert(sizeof(pa_placement) == 72, "pa_placement");
+_Static_assert(sizeof(pa_placement) == 80, "pa_placement");
_Static_assert(sizeof(pa_paint) == 120, "pa_paint");
_Static_assert(sizeof(pa_key) == 8 && sizeof(pa_mouse) == 10, "pa_key");
_Static_assert(sizeof(pa_api) == 64 && sizeof(pa_out) == 16, "pa_api");
-_Static_assert(sizeof(pa_word) == 40 && sizeof(pa_head) == 56, "pa_head");
+_Static_assert(sizeof(pa_word) == 32 && sizeof(pa_head) == 56, "pa_head");
_Static_assert(sizeof(pa_ops) == 192, "pa_ops");
#endif
@@ -251,7 +280,7 @@ static inline pa_cell *pa_at(const pa_window *w, unsigned row, unsigned col) {
/* `text` (UTF-8) from (row, col) on, a character a cell, cut at the edge;
* the cells written. */
-static inline unsigned pa_put(const pa_window *w, unsigned row, unsigned col, const char *text, uint32_t fg, uint32_t bg) {
+static inline unsigned pa_put(const pa_window *w, unsigned row, unsigned col, const char *text, uint32_t fg, uint32_t bg, uint16_t attrs) {
unsigned n = 0;
while (*text) {
pa_cell *c = pa_at(w, row, col + n);
@@ -262,7 +291,7 @@ static inline unsigned pa_put(const pa_window *w, unsigned row, unsigned col, co
c->len = (uint8_t)len;
c->fg = fg;
c->bg = bg;
- c->attrs = 0;
+ c->attrs = attrs;
c->flags = PA_PAINTED;
n++;
}
@@ -272,6 +301,22 @@ static inline unsigned pa_put(const pa_window *w, unsigned row, unsigned col, co
return n;
}
+/* `n` bytes of src[off..len] into buf: the body of a kind whose text is small. */
+static inline uint32_t pa_copy(const void *src, uint64_t len, uint64_t off, void *buf, uint32_t n) {
+ if (off >= len) return 0;
+ if (n > len - off) n = (uint32_t)(len - off);
+ memcpy(buf, (const uint8_t *)src + off, n);
+ return n;
+}
+
+/* dump and load as a byte copy of a plain struct `type`, refused across
+ * versions: PA_POD(snake, VERSION) then .dump = snake_dump, .load = snake_load */
+#define PA_POD(type, ver) \
+ static uint32_t type##_dump(void *st, uint8_t *buf, uint32_t n) { \
+ return n < sizeof(type) ? 0 : (memcpy(buf, st, sizeof(type)), (uint32_t)sizeof(type)); } \
+ static int type##_load(void *st, const uint8_t *buf, uint32_t n, uint32_t version) { \
+ return version != (ver) || n != sizeof(type) ? 1 : (memcpy(st, buf, sizeof(type)), PA_OK); }
+
/* An addon's head, its size and ABI filled:
* const pa_ops pardes_addon = { .head = PA_ADDON("game", 1, "", words, 1), .paint = paint }; */
#define PA_ADDON(nm, ver, ex, ws, nws) \
diff --git a/src/look.zig b/src/look.zig
index d60ff3a9..7af69c55 100644
--- a/src/look.zig
+++ b/src/look.zig
@@ -187,7 +187,7 @@ pub const Target = union(enum) {
dir: []const u8, // resolved absolute path, in caller's buf
file: FileTarget,
/// A file a kind opens (Kind.forPath): a picture.
- kind: struct { path: []const u8, ops: *const Kind.Ops },
+ kind: struct { path: []const u8, kind: u8 },
url: []const u8,
pane: struct { id: usize, at: Spot },
};
@@ -371,7 +371,8 @@ pub fn resolve(p: ?*pardes.Pardes, word_raw: []const u8, cwd: []const u8, realbu
.at = pl.at,
.kind = .pdf,
} };
- if (Kind.forPath(found.path)) |ops| return .{ .kind = .{ .path = found.path, .ops = ops } };
+ var compiled_in: Kind.Registry = if (p == null) .init() else .{};
+ if ((if (p) |pp| &pp.kinds else &compiled_in).forPath(found.path)) |k| return .{ .kind = .{ .path = found.path, .kind = k } };
return .{ .file = .{ .path = found.path, .at = pl.at } };
}
@@ -1880,7 +1881,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
.kind => |target| {
if (focusPaneByPath(p, target.path, .{})) return;
const free = p.freeSlot() orelse return p.reportError(id, "look", error.NoPaneSlots);
- const made = Kind.openFile(p, free, target.ops, target.path) catch |err|
+ const made = Kind.openFile(p, free, target.kind, target.path) catch |err|
return lookOpenFailed(p, id, target.path, err);
// One its kind refuses (an image corrupt, truncated, not what
// its name says) is said, a failure, rather than shown blank.
diff --git a/src/macos.zig b/src/macos.zig
index 96ef4f1e..a152edfc 100644
--- a/src/macos.zig
+++ b/src/macos.zig
@@ -1099,7 +1099,9 @@ fn drainInbox(st: *State) bool {
// The SDL shell's: this one's file watches and 9P are its own.
// ponytail: no hand_over host entry on macOS yet, so none comes.
// No daemon on macOS (daemon.supported): neither ever comes.
- .files_changed, .fs_ready, .handed, .window, .adopted => {},
+ // ponytail: no `job` host entry here either: a kind's jobs run
+ // on the loop (host_io.KindJob.runHere), so none comes.
+ .files_changed, .fs_ready, .handed, .window, .adopted, .kind_job => {},
}
}
for (0..pardes.MAX_PANES) |pane| {
diff --git a/src/mouse.zig b/src/mouse.zig
index 0798b51f..f57bb533 100644
--- a/src/mouse.zig
+++ b/src/mouse.zig
@@ -388,7 +388,7 @@ fn kindMouse(p: *Pardes, m: Mouse) bool {
for (p.panes, 0..) |slot, id| {
const pane = slot orelse continue;
const k = pane.addon orelse continue;
- if (!k.takesMouse() or pane.collapsed) continue;
+ if (!pardes.Kind.takesMouse(p, k) or pane.collapsed) continue;
const r = p.rects[id];
const top = p.bodyTop(pane, r) + pane.notices.span();
const x0 = r.x + config.GUTTER;
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig
index a65b3562..78fe15c1 100644
--- a/src/ninep/ctl.zig
+++ b/src/ninep/ctl.zig
@@ -527,7 +527,7 @@ fn checkBuiltin(p: *Pardes, req: Req, line: []const u8, scope: builtins.Scope) ?
const here = if (std.meta.stringToEnum(Builtin, word)) |b| builtins.registry.here(p, b) else false;
const b = (if (here) std.meta.stringToEnum(Builtin, word) else null) orelse {
// An addon's word: its kind's to take or refuse as it runs.
- if (pardes.Kind.word(word) != null) return null;
+ if (p.kinds.word(word) != null) return null;
// A setting this build's frontend cannot show (Lift, GripWidth on a
// terminal, the window's own words in a terminal's view) is known,
// and says so rather than sound misspelt.
@@ -898,12 +898,12 @@ fn writeCommands(p: *const Pardes, w: *std.Io.Writer) !void {
try w.writeByte('\n');
}
// The addons' words, after the core's: `(addon <name>)` says whose.
- for (pardes.Kind.wordsOf()) |kw| try w.print("{s}{s} {s} -- {s} (addon {s})\n", .{
- std.mem.span(kw.w.name),
- if (kw.w.arg != pardes.Kind.c.PA_ARG_NONE) " arg" else "",
- if (kw.w.opens != 0) "both" else "pane",
- if (kw.w.doc == null) "" else std.mem.span(kw.w.doc),
- pardes.Kind.headName(kw.ops),
+ for (p.kinds.words[0..p.kinds.nwords]) |*kw| try w.print("{s}{s} {s} -- {s} (addon {s})\n", .{
+ kw.name.slice(),
+ if (kw.arg != pardes.Kind.c.PA_ARG_NONE) " arg" else "",
+ if (kw.opens != 0) "both" else "pane",
+ kw.doc.slice(),
+ p.kinds.kinds[kw.kind].nameOf(),
});
}
@@ -1536,7 +1536,7 @@ test "an address looked at in an image pane names the image, never an empty subj
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
const id = p.freeSlot().?;
- const doc = try pardes.Kind.open(p, id, &pardes.image.kind, "/tmp/pardes-pic.ppm", null);
+ const doc = try pardes.Kind.open(p, id, pardes.Kind.image, "/tmp/pardes-pic.ppm", null);
_ = wr(p, Node.of(doc.serial, .look), ":/x/\n");
try testing.expect(th.logHas(p, "look: /tmp/pardes-pic.ppm is no text to address"));
}
@@ -1545,7 +1545,7 @@ test "Save and Save path on an image pane are refused with why, never a silent s
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
const id = p.freeSlot().?;
- const doc = try pardes.Kind.open(p, id, &pardes.image.kind, "/tmp/pardes-pic.ppm", null);
+ const doc = try pardes.Kind.open(p, id, pardes.Kind.image, "/tmp/pardes-pic.ppm", null);
for ([_][]const u8{ "Save\n", "Save /tmp/pardes-copy.ppm\n" }) |line| {
const r = wr(p, Node.of(doc.serial, .ctl), line);
try testing.expectEqual(Status.err, r.reply.status);
@@ -1575,7 +1575,13 @@ test "a builtin for one kind of pane is refused on another, and still acts on it
try testing.expectEqual(Status.err, wr(p, Node.of(serial, .ctl), line).reply.status);
};
const id = p.freeSlot().?;
- const doc = try pardes.Kind.open(p, id, &pardes.image.kind, "/tmp/pardes-pic.ppm", null);
+ // A picture shown: the core's art words follow the kind's prefix.
+ pardes.image.start(testing.io, testing.allocator);
+ defer pardes.image.stop();
+ const doc = try pardes.Kind.open(p, id, pardes.Kind.image, "/tmp/pardes-pic.ppm", "P6\n1 1\n255\n\x01\x02\x03");
+ while (p.nextEffect()) |e| p.perform(e);
+ pardes.exec.placeNew(p, 0, 0, id, .doc);
+ _ = try p.render(p.scratch.allocator());
const r = wr(p, Node.of(doc.serial, .ctl), "Mode\n");
try testing.expectEqualStrings("Mode: only a text or terminal pane has modes", r.reply.ename);
const glyphs = doc.addon.?.art.glyph;
@@ -2149,7 +2155,7 @@ test "a language server question that finds nothing, cannot be answered, or is a
try testing.expectEqual(@as(u16, 0), p.fs.late_failure_len);
// An image's body takes no write.
const slot = p.freeSlot().?;
- const img = try pardes.Kind.open(p, slot, &pardes.image.kind, "/tmp/pardes-no-such.png", "not a png");
+ const img = try pardes.Kind.open(p, slot, pardes.Kind.image, "/tmp/pardes-no-such.png", "not a png");
try testing.expectEqual(E.INVAL, wr(p, Node.of(img.serial, .body), "text\n").errno());
}
@@ -2791,7 +2797,7 @@ test "a Save whose directory is missing because one above refuses says permissio
test "a body write to an image pane says its body takes none, not that terminals have no text" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
- const doc = try pardes.Kind.open(p, p.freeSlot().?, &pardes.image.kind, "/tmp/pardes-pic.ppm", null);
+ const doc = try pardes.Kind.open(p, p.freeSlot().?, pardes.Kind.image, "/tmp/pardes-pic.ppm", null);
try testing.expectEqualStrings(pane_files.e_no_body, wr(p, Node.of(doc.serial, .body), "x").reply.ename);
}
@@ -2821,7 +2827,7 @@ test "a Rename that reaches other files previews them, says so, and is the pane
test "Edit on an image pane is refused as a data write there is, EINVAL, before it runs" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
- const doc = try pardes.Kind.open(p, p.freeSlot().?, &pardes.image.kind, "/tmp/pardes-pic.ppm", null);
+ const doc = try pardes.Kind.open(p, p.freeSlot().?, pardes.Kind.image, "/tmp/pardes-pic.ppm", null);
const edit = wr(p, Node.of(doc.serial, .ctl), "Edit ,d\n");
const data = wr(p, Node.of(doc.serial, .data), "x");
try testing.expectEqual(E.INVAL, edit.errno());
@@ -2832,7 +2838,7 @@ test "Edit on an image pane is refused as a data write there is, EINVAL, before
test "Petscii, Palette and Ascii take the state their tag word names, set rather than flipped" {
const p = try withFile(testing.allocator, "x\n");
defer p.deinit();
- const doc = try pardes.Kind.open(p, p.freeSlot().?, &pardes.image.kind, "/tmp/pardes-pic.ppm", null);
+ const doc = try pardes.Kind.open(p, p.freeSlot().?, pardes.Kind.image, "/tmp/pardes-pic.ppm", null);
const ctl_node = Node.of(doc.serial, .ctl);
for ([_][]const u8{ "Petscii on\n", "Petscii on\n" }) |line| {
try testing.expectEqual(Status.ok, wr(p, ctl_node, line).reply.status);
diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig
index 9c3de708..4f155b4f 100644
--- a/src/ninep/tree.zig
+++ b/src/ninep/tree.zig
@@ -1113,7 +1113,7 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply {
},
.addons => addons: {
var text: std.ArrayList(u8) = .empty;
- pardes.Kind.list(p, &text, p.scratch.allocator()) catch break :addons Reply.fail(req.tag, E.NOMEM);
+ pardes.Addon.list(p, &text, p.scratch.allocator()) catch break :addons Reply.fail(req.tag, E.NOMEM);
break :addons window(p, req, text.items);
},
.dumps => dumps: {
diff --git a/src/replica.zig b/src/replica.zig
index d30d0e8a..6de125bd 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -298,7 +298,9 @@ pub const Authority = struct {
// A message of either side is u8 pane, u32 the pane's serial (a slot's
// next pane is another), u8 kind, and the kind's bytes:
// to a replica: kind 1, a delta; kind 2, rows (encodeRows): asked for,
-// or a step read them;
+// or a step read them; kind 4, a pane of a kind's frame
+// (a Shadow, screen_state.encodeShadow), at the size the
+// peer's view gives the pane;
// to the authority: kind 1, an acknowledgement (u32 seq); kind 2, a
// request for rows (u64 lo, u64 hi, absolute); kind 3, a
// delta refused: start this terminal afresh.
@@ -312,6 +314,7 @@ pub const Authority = struct {
const kind_delta = 1;
const kind_rows = 2;
+const kind_shadow = 4;
const kind_ack = 1;
const kind_reset = 3;
@@ -343,7 +346,7 @@ pub const Screens = struct {
encode_ns: u64 = 0,
const Tracked = struct { serial: u32, state: *terminal.State, track: screen_state.Track };
- const PeerOf = struct { serial: u32 = 0, peer: screen_state.Peer = .{} };
+ const PeerOf = struct { serial: u32 = 0, peer: screen_state.Peer = .{}, shadow: screen_state.ShadowSent = .{} };
pub fn init(gpa: Allocator) Screens {
return .{ .gpa = gpa, .enc = .init(gpa) };
@@ -353,7 +356,10 @@ pub const Screens = struct {
pub fn deinit(s: *Screens, core: *Pardes) void {
for (&s.tracks, 0..) |*slot, id| if (slot.*) |*tr| tr.track.deinit(s.gpa, if (s.live(core, id, tr)) &tr.state.vt else null);
for (s.peers.items) |p| {
- for (p) |*pp| pp.peer.deinit(s.gpa);
+ for (p) |*pp| {
+ pp.peer.deinit(s.gpa);
+ pp.shadow.deinit(s.gpa);
+ }
s.gpa.destroy(p);
}
s.peers.deinit(s.gpa);
@@ -380,16 +386,20 @@ pub const Screens = struct {
/// Peer `peer` gone: what it was sent forgotten, its number free for
/// the next `addPeer` (a socket's clients come and go).
pub fn dropPeer(s: *Screens, peer: u16) void {
- for (s.peers.items[peer]) |*pp| pp.peer.deinit(s.gpa);
+ for (s.peers.items[peer]) |*pp| {
+ pp.peer.deinit(s.gpa);
+ pp.shadow.deinit(s.gpa);
+ }
s.peers.items[peer].* = @splat(.{});
s.free_peers.append(s.gpa, peer) catch {};
}
/// What `peer` is owed of every terminal, each a message onto `out`
/// (owned): nothing for one it has as it is, or with `bound` in flight.
- pub fn flush(s: *Screens, core: *Pardes, peer: u16, out: *std.ArrayListUnmanaged([]u8)) !void {
+ pub fn flush(s: *Screens, core: *Pardes, peer: u16, view: u8, out: *std.ArrayListUnmanaged([]u8)) !void {
const t0 = nowNs();
defer s.encode_ns += nowNs() - t0;
+ try s.shadows(core, peer, view, out);
for (core.panes, 0..) |slot, id| {
const pane = slot orelse continue;
const state = pane.terminal orelse continue;
@@ -409,6 +419,41 @@ pub const Screens = struct {
}
}
+ /// What `peer` is owed of each pane of a kind this core runs: its
+ /// frame as the peer's view shows it, when it is not what it was sent.
+ fn shadows(s: *Screens, core: *Pardes, peer: u16, view: u8, out: *std.ArrayListUnmanaged([]u8)) !void {
+ const any = for (core.panes) |slot| {
+ if (slot) |pane| if (pane.addon) |k| if (k.ops != null) break true;
+ } else false;
+ if (!any) return;
+ const Ctx = struct { s: *Screens, peer: u16, out: *std.ArrayListUnmanaged([]u8) };
+ try pardes.views.within(core, view, Ctx{ .s = s, .peer = peer, .out = out }, struct {
+ fn each(x: Ctx, p: *Pardes) anyerror!void {
+ var arena: std.heap.ArenaAllocator = .init(x.s.gpa);
+ defer arena.deinit();
+ for (p.panes, 0..) |slot, id| if (slot) |pane| if (pane.addon) |k| if (k.ops != null and !pane.collapsed) {
+ const pp = &x.s.peers.items[x.peer][id];
+ if (pp.shadow.serial != pane.serial) {
+ pp.shadow.deinit(x.s.gpa);
+ pp.shadow.serial = pane.serial;
+ }
+ const r = p.rects[id];
+ const cols = r.w -| pardes.config.GUTTER;
+ const rows = (r.h -| pane.tag_rows) -| pane.notices.span();
+ if (cols == 0 or rows == 0) continue;
+ var frame: pardes.Kind.Frame = .{ .cols = cols, .rows = rows };
+ frame.cells = pardes.Kind.paint(p, k, id, arena.allocator(), cols, rows, &frame) orelse continue;
+ x.s.out.clearRetainingCapacity();
+ try writeScreenHead(&x.s.out, x.s.gpa, @intCast(id), pane.serial, kind_shadow);
+ var w: std.Io.Writer.Allocating = .fromArrayList(x.s.gpa, &x.s.out);
+ defer x.s.out = w.toArrayList();
+ if (!try screen_state.encodeShadow(x.s.gpa, &pp.shadow, &frame, k.prefix.slice(), k.placed, &w.writer)) continue;
+ try x.out.append(x.s.gpa, try x.s.gpa.dupe(u8, w.written()));
+ };
+ }
+ }.each);
+ }
+
/// Rows a peer holds as holes of every terminal a step read (its rows,
/// history and all: text mode over a shell), each a rows message onto
/// `out` (owned): sent after the step and before its unit, so the
@@ -675,6 +720,8 @@ pub const Replica = struct {
pardes.dump.terminal_gpa = terminal.emulator_gpa;
defer pardes.dump.terminal_gpa = null;
const c = try pardes.dump.load(epoch, o, bytes, null);
+ // It runs no kind's code: its panes of a kind are Shadows.
+ c.kinds.ops = @splat(null);
// What loading queued (a watch, a theme asked for) is the
// authority's errand, done there already: the session was dumped
// between steps, with nothing queued.
@@ -1061,6 +1108,17 @@ pub const Replica = struct {
const c = r.t.core orelse return null;
const pane = c.panes[m.pane] orelse return null;
if (pane.serial != m.serial) return null; // a pane gone: its slot's next is another
+ // A pane of a kind: the authority's frame of it, drawn as it is.
+ if (m.kind == kind_shadow) if (pane.addon) |k| {
+ const sh = k.shadow orelse sh: {
+ const made = k.gpa.create(screen_state.Shadow) catch return null;
+ made.* = .{};
+ k.shadow = made;
+ break :sh made;
+ };
+ sh.apply(k.gpa, m.payload) catch r.fail("a kind's frame did not apply");
+ return null;
+ };
const state = pane.terminal orelse return null;
const slot = &r.screens[m.pane];
if (slot.* == null or slot.*.?.serial != m.serial) {
@@ -1857,7 +1915,7 @@ pub const Peers = struct {
ps.msgs.clearRetainingCapacity();
}
(switch (what) {
- .flush => ps.screens.flush(core, peer.sid, &ps.msgs),
+ .flush => ps.screens.flush(core, peer.sid, peer.view, &ps.msgs),
.fill => ps.screens.fill(core, peer.sid, &ps.msgs),
}) catch return;
for (ps.msgs.items) |m| {
@@ -3264,7 +3322,7 @@ fn deliver(a: *Authority, screens: *Screens, peer: u16, r: *Replica, comptime wh
msgs.deinit(gpa);
}
switch (what) {
- .flush => try screens.flush(a.core, peer, &msgs),
+ .flush => try screens.flush(a.core, peer, r.view, &msgs),
.fill => {
try screens.fill(a.core, peer, &msgs);
screens.filled(a.core);
@@ -4018,3 +4076,85 @@ test "a step that reads a terminal's rows as the core keeps them sends a new cli
try sendAs(r1, a, &screens, &.{ r1, r2 }, &ps, .{ .key = .{ .cp = 'O', .text = "O" } });
for ([_]*Replica{ r1, r2 }) |r| try agrees(a, r);
}
+
+/// A kind as a library brings one, for the Shadow's test: a counter its
+/// keys move, painted as `count N`.
+const Counter = struct {
+ const pa = pardes.Kind.c;
+ fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque {
+ return pardes.Kind.api.alloc.?(4);
+ }
+ fn paint(st: ?*anyopaque, win: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {
+ const n: *u32 = @ptrCast(@alignCast(st.?));
+ var line: [16]u8 = undefined;
+ const text = std.fmt.bufPrint(&line, "count {d}", .{n.*}) catch unreachable;
+ for (text, 0..) |ch, i| win.*.cells[i] = std.mem.zeroInit(pa.pa_cell, .{ .text = .{ ch, 0, 0, 0, 0, 0, 0, 0 }, .len = 1, .flags = pa.PA_PAINTED, .fg = pa.PA_DEFAULT, .bg = pa.PA_DEFAULT });
+ }
+ fn press(st: ?*anyopaque, _: [*c]const pa.pa_key) callconv(.c) c_int {
+ const n: *u32 = @ptrCast(@alignCast(st.?));
+ n.* += 1;
+ return 1;
+ }
+ const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Counter", .opens = 1, .doc = "a counter" })};
+ const ops = std.mem.zeroInit(pardes.Kind.Ops, .{
+ .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "counter".* ++ @as([9]u8, @splat(0)), .version = 1, .exts = "", .words = &words, .nwords = 1 }),
+ .open = open,
+ .paint = paint,
+ .key = press,
+ });
+};
+
+fn shows(p: *Pardes, text: []const u8) !bool {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const s = try p.render(arena.allocator());
+ var row: [512]u8 = undefined;
+ for (0..s.rows) |y| {
+ var n: usize = 0;
+ for (0..s.cols) |x| {
+ const g = s.at(@intCast(x), @intCast(y)).grapheme();
+ if (n + g.len > row.len) break;
+ @memcpy(row[n..][0..g.len], g);
+ n += g.len;
+ }
+ if (std.mem.indexOf(u8, row[0..n], text) != null) return true;
+ }
+ return false;
+}
+
+test "a replica with no code for a kind draws its pane from the authority's frames, and its keys go to the authority" {
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
+ defer a.deinit();
+ var screens: Screens = .init(gpa);
+ defer screens.deinit(a.core);
+ const peer = try screens.addPeer();
+ const r = try Replica.init(gpa, 1, try a.unit(0, 0), false);
+ defer r.deinit();
+ // What the host read off the library, as the `addon` event: the
+ // replica's core takes the kind's words, never its code.
+ var words: [64]pardes.Kind.LoadedWord = undefined;
+ const counter = pardes.Kind.loadedOf(&Counter.ops, "/x/counter.so", @splat(7), &words);
+ r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &counter } }));
+ r.confirm(try a.local(.{ .command = "Counter" }));
+ try std.testing.expect(r.core().kinds.ops[r.core().kinds.n - 1] == null);
+ try deliver(a, &screens, peer, r, .flush);
+ try std.testing.expect(try shows(r.core(), "count 0"));
+ // A key into it, typed at the replica: the authority's kind takes it.
+ var sent: std.ArrayListUnmanaged([]u8) = .empty;
+ defer {
+ for (sent.items) |m| gpa.free(m);
+ sent.deinit(gpa);
+ }
+ try key(r, 'x', &sent);
+ for (sent.items) |m| {
+ r.confirm(try a.take(m));
+ r.settle();
+ }
+ try deliver(a, &screens, peer, r, .flush);
+ try std.testing.expect(try shows(a.core, "count 1"));
+ try std.testing.expect(try shows(r.core(), "count 1"));
+ if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why});
+ try std.testing.expect(r.diverged == null);
+ try std.testing.expectEqual(digestOf(a), r.digest());
+}
diff --git a/src/screen_state.zig b/src/screen_state.zig
index 53288c7e..07d4153b 100644
--- a/src/screen_state.zig
+++ b/src/screen_state.zig
@@ -1292,3 +1292,200 @@ test "a replica's terminal pane draws as its authority's does" {
};
}
}
+
+// ---- a pane of a kind, as a Shadow (architecture ยง16.5) ----
+//
+// A replica's core has no code for a library's kind: such a pane is drawn
+// from the authority's frame of it, painted at the size the peer's view
+// gives it. A frame crosses as its size, the prefix the kind gave it and
+// whether it placed a picture, each row by its hash (with its cells when
+// the peer's row there is another), then each placement, with its raster's
+// bytes when the peer was not sent that key with the frame before. The
+// stream is ordered and loses nothing, so what the authority last sent a
+// peer is what the peer holds.
+
+const Kind = @import("Kind.zig");
+const Cell = Kind.c.pa_cell;
+const Place = Kind.c.pa_placement;
+
+/// A replica's frame of a pane of a kind: what Kind.draw draws for it.
+pub const Shadow = struct {
+ cols: u16 = 0,
+ rows: u16 = 0,
+ cells: []Cell = &.{},
+ hashes: []Hash = &.{},
+ places: std.ArrayList(Place) = .empty,
+ /// Each placement's raster, owned, beside it.
+ rasters: std.ArrayList([]u8) = .empty,
+ prefix: Kind.Buf(96) = .{},
+ placed: bool = false,
+
+ pub fn deinit(sh: *Shadow, gpa: std.mem.Allocator) void {
+ gpa.free(sh.cells);
+ gpa.free(sh.hashes);
+ for (sh.rasters.items) |r| gpa.free(r);
+ sh.rasters.deinit(gpa);
+ sh.places.deinit(gpa);
+ gpa.destroy(sh);
+ }
+
+ /// A frame message applied (`encodeShadow`'s).
+ pub fn apply(sh: *Shadow, gpa: std.mem.Allocator, bytes: []const u8) !void {
+ var r: std.Io.Reader = .fixed(bytes);
+ const cols = try r.takeInt(u16, .little);
+ const rows = try r.takeInt(u16, .little);
+ if (cols != sh.cols or rows != sh.rows) {
+ gpa.free(sh.cells);
+ gpa.free(sh.hashes);
+ sh.cells = try gpa.alloc(Cell, @as(usize, cols) * rows);
+ sh.hashes = try gpa.alloc(Hash, rows);
+ @memset(std.mem.sliceAsBytes(sh.cells), 0);
+ @memset(sh.hashes, 0);
+ sh.cols = cols;
+ sh.rows = rows;
+ }
+ sh.placed = try r.takeByte() != 0;
+ sh.prefix.set(try r.take(try r.takeByte()));
+ for (0..rows) |y| {
+ sh.hashes[y] = try r.takeInt(u64, .little);
+ if (try r.takeByte() == 0) continue;
+ for (sh.cells[y * cols ..][0..cols]) |*c| {
+ @memcpy(&c.text, try r.take(8));
+ c.fg = try r.takeInt(u32, .little);
+ c.bg = try r.takeInt(u32, .little);
+ c.attrs = try r.takeInt(u16, .little);
+ c.len = try r.takeByte();
+ c.flags = try r.takeByte();
+ }
+ }
+ var places: std.ArrayList(Place) = .empty;
+ errdefer places.deinit(gpa);
+ var rasters: std.ArrayList([]u8) = .empty;
+ defer {
+ for (sh.rasters.items) |old| if (for (rasters.items) |kept| {
+ if (kept.ptr == old.ptr) break false;
+ } else true) gpa.free(old);
+ sh.rasters.deinit(gpa);
+ sh.rasters = rasters;
+ sh.places.deinit(gpa);
+ sh.places = places;
+ }
+ for (0..try r.takeByte()) |_| {
+ var pl = std.mem.zeroes(Place);
+ pl.w = try r.takeInt(u32, .little);
+ pl.h = try r.takeInt(u32, .little);
+ inline for (.{ &pl.src.x, &pl.src.y, &pl.src.w, &pl.src.h, &pl.at.x, &pl.at.y, &pl.at.w, &pl.at.h }) |f| f.* = try r.takeInt(u32, .little);
+ inline for (.{ &pl.dst.x, &pl.dst.y, &pl.dst.w, &pl.dst.h }) |f| f.* = try r.takeInt(u16, .little);
+ pl.key = try r.takeInt(u64, .little);
+ const raster = if (try r.takeByte() != 0) try gpa.dupe(u8, try r.take(@as(usize, pl.w) * pl.h * 4)) else for (sh.places.items, sh.rasters.items) |old, held| {
+ if (old.key == pl.key) break held;
+ } else return error.BadShadow;
+ pl.rgba = raster.ptr;
+ try rasters.append(gpa, raster);
+ try places.append(gpa, pl);
+ }
+ }
+};
+
+/// What the authority last sent one peer of a pane of a kind: its rows'
+/// hashes and its placements' keys.
+pub const ShadowSent = struct {
+ serial: u32 = 0,
+ cols: u16 = 0,
+ hashes: std.ArrayList(Hash) = .empty,
+ keys: std.ArrayList(u64) = .empty,
+ prefix: Hash = 0,
+
+ pub fn deinit(s: *ShadowSent, gpa: std.mem.Allocator) void {
+ s.hashes.deinit(gpa);
+ s.keys.deinit(gpa);
+ s.* = .{};
+ }
+};
+
+/// Frame `f` of a pane (its prefix and `placed` from its slot) for a peer
+/// that was last sent `sent`: false, and nothing written, when it holds
+/// this frame already.
+pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Frame, prefix: []const u8, placed: bool, w: *std.Io.Writer) !bool {
+ var hashes: std.ArrayList(Hash) = .empty;
+ errdefer hashes.deinit(gpa);
+ for (0..f.rows) |y| try hashes.append(gpa, std.hash.Wyhash.hash(0, std.mem.sliceAsBytes(f.cells[y * f.cols ..][0..f.cols])));
+ const places = f.places[0..f.n];
+ const prefix_hash = std.hash.Wyhash.hash(@intFromBool(placed), prefix);
+ var same = sent.cols == f.cols and std.mem.eql(Hash, sent.hashes.items, hashes.items) and sent.prefix == prefix_hash and sent.keys.items.len == places.len;
+ if (same) for (places, sent.keys.items) |pl, k| {
+ if (pl.key != k) same = false;
+ };
+ if (same) {
+ hashes.deinit(gpa);
+ return false;
+ }
+ try w.writeInt(u16, f.cols, .little);
+ try w.writeInt(u16, f.rows, .little);
+ try w.writeByte(@intFromBool(placed));
+ try w.writeByte(@intCast(@min(prefix.len, 96)));
+ try w.writeAll(prefix[0..@min(prefix.len, 96)]);
+ for (hashes.items, 0..) |h, y| {
+ try w.writeInt(u64, h, .little);
+ const held = sent.cols == f.cols and y < sent.hashes.items.len and sent.hashes.items[y] == h;
+ try w.writeByte(@intFromBool(!held));
+ if (!held) for (f.cells[y * f.cols ..][0..f.cols]) |*c| {
+ try w.writeAll(&c.text);
+ try w.writeInt(u32, c.fg, .little);
+ try w.writeInt(u32, c.bg, .little);
+ try w.writeInt(u16, c.attrs, .little);
+ try w.writeByte(c.len);
+ try w.writeByte(c.flags);
+ };
+ }
+ try w.writeByte(@intCast(places.len));
+ for (places) |pl| {
+ try w.writeInt(u32, pl.w, .little);
+ try w.writeInt(u32, pl.h, .little);
+ inline for (.{ pl.src.x, pl.src.y, pl.src.w, pl.src.h, pl.at.x, pl.at.y, pl.at.w, pl.at.h }) |v| try w.writeInt(u32, v, .little);
+ inline for (.{ pl.dst.x, pl.dst.y, pl.dst.w, pl.dst.h }) |v| try w.writeInt(u16, v, .little);
+ try w.writeInt(u64, pl.key, .little);
+ const held = std.mem.indexOfScalar(u64, sent.keys.items, pl.key) != null;
+ try w.writeByte(@intFromBool(!held));
+ if (!held) try w.writeAll(pl.rgba[0 .. @as(usize, pl.w) * pl.h * 4]);
+ }
+ sent.hashes.deinit(gpa);
+ sent.hashes = hashes;
+ sent.cols = f.cols;
+ sent.prefix = prefix_hash;
+ sent.keys.clearRetainingCapacity();
+ for (places) |pl| try sent.keys.append(gpa, pl.key);
+ return true;
+}
+
+test "a pane of a kind crosses as its frame, a row or a raster the peer holds as its hash" {
+ const gpa = std.testing.allocator;
+ var cells: [3 * 2]Cell = @splat(std.mem.zeroes(Cell));
+ cells[0] = .{ .text = "a".* ++ @as([7]u8, @splat(0)), .fg = 1, .bg = 2, .attrs = 0, .len = 1, .flags = Kind.c.PA_PAINTED };
+ var pixels: [4]u8 = .{ 1, 2, 3, 4 };
+ var f: Kind.Frame = .{ .cells = &cells, .cols = 3, .rows = 2, .n = 1 };
+ f.buf[0] = std.mem.zeroInit(Place, .{ .rgba = &pixels, .w = 1, .h = 1, .key = 9 });
+ f.places = &f.buf;
+ var sent: ShadowSent = .{};
+ defer sent.deinit(gpa);
+ const sh = try gpa.create(Shadow);
+ sh.* = .{};
+ defer sh.deinit(gpa);
+ var out: std.Io.Writer.Allocating = .init(gpa);
+ defer out.deinit();
+ try std.testing.expect(try encodeShadow(gpa, &sent, &f, "fake", true, &out.writer));
+ try sh.apply(gpa, out.written());
+ try std.testing.expectEqualStrings("a", sh.cells[0].text[0..1]);
+ try std.testing.expectEqualStrings("fake", sh.prefix.slice());
+ try std.testing.expectEqual(@as(u8, 3), sh.rasters.items[0][2]);
+ // The same frame again: nothing to send.
+ out.clearRetainingCapacity();
+ try std.testing.expect(!try encodeShadow(gpa, &sent, &f, "fake", true, &out.writer));
+ // A cell of the second row moves: the first row and the raster go as hashes.
+ cells[4].flags = Kind.c.PA_PAINTED;
+ try std.testing.expect(try encodeShadow(gpa, &sent, &f, "fake", true, &out.writer));
+ try std.testing.expect(out.written().len < 20 + 2 * 9 + 3 * 20 + 60);
+ try sh.apply(gpa, out.written());
+ try std.testing.expectEqual(@as(u8, Kind.c.PA_PAINTED), sh.cells[4].flags);
+ try std.testing.expectEqual(@as(u8, 3), sh.rasters.items[0][2]);
+}
diff --git a/src/tape.zig b/src/tape.zig
index 1c58e886..3752af54 100644
--- a/src/tape.zig
+++ b/src/tape.zig
@@ -118,6 +118,9 @@ pub const Site = enum(u16) {
prompt,
/// What the authority alone holds and a step reads (`authority`).
authority,
+ /// What a call into a kind did (Kind.Outcome), and a kind's pane's
+ /// tag prefix: the authority's, whose core alone runs the kind.
+ addon,
};
/// What the sites return. wire.semantics hashes their shape with the
@@ -141,6 +144,7 @@ pub const results = .{
// Recent's entry for a closed jump, the closed panes' names.
?@import("Store.zig").Record, ?@import("recent.zig").Entry,
[]const @import("Store.zig").Named,
+ @import("Kind.zig").Outcome,
};
/// What a host reports to the core as failed (`shell_failed`, `save_failed`,
diff --git a/src/tui/tui.zig b/src/tui/tui.zig
index 68b97ba2..414fe776 100644
--- a/src/tui/tui.zig
+++ b/src/tui/tui.zig
@@ -150,6 +150,8 @@ const Msg = union(enum) {
pipe_done: selection_pipe.Response,
/// A streaming search's rows (pardes.Stream).
search_rows: pardes.Stream.Batch,
+ /// A kind's job done on a worker (host_io.KindJob).
+ kind_job: *host_io.KindJob,
files_changed,
fs_ready,
/// A replica's connection, handed over by the daemon (Peers).
@@ -1592,6 +1594,7 @@ fn localSession(
host_io.startWorkers(io, allocs);
defer host_io.stopWorkers();
+ options.addons = pardes.Addon.scan();
var core = if (upgrade.resumed) |*rec|
try upgrade.load(gpa, allocs.pardes, options, rec)
@@ -1654,6 +1657,7 @@ fn localSession(
sh.lsp_task = null;
}
sh.pipe_tasks.cancelAll(io);
+ sh.job_tasks.cancelAll(io);
// Its worker may be waiting on the loop's queue for room.
core.stream.cancelTask();
file_watch.stop(sh.inotify_fd);
@@ -1678,6 +1682,7 @@ fn localSession(
response.deinit(gpa);
},
.search_rows => |b| b.deinit(gpa),
+ .kind_job => |j| j.drop(),
.attach => |a| {
_ = libc.close(a.tty);
if (a.stub >= 0) _ = libc.close(a.stub);
@@ -1849,6 +1854,7 @@ const Shell = struct {
watch_task: ?std.Io.Future(anyerror!void) = null,
lsp_task: ?host_io.Lsp.Task = null,
pipe_tasks: PipeTasks = .{},
+ job_tasks: PipeTasks = .{},
threads_ok: bool = false,
check_files: bool = false,
// ponytail: one bracketed paste at a time for every terminal: keys
@@ -1957,6 +1963,7 @@ const Shell = struct {
.lsp = lsp,
.pipe = pipe,
.search = search,
+ .job = job,
};
fn clockNow(ctx: ?*anyopaque) u64 {
@@ -2552,6 +2559,10 @@ const Shell = struct {
core.update(.{ .search = b });
b.deinit(s.gpa);
},
+ .kind_job => |j| {
+ s.job_tasks.finish(s.io, j.id);
+ j.land(core);
+ },
}
return false;
}
@@ -2924,6 +2935,16 @@ const Shell = struct {
pardes.Stream.runHere(s.core, gen);
}
+ fn job(ctx: ?*anyopaque, pane: u8, serial: u32, token: u32) void {
+ const s = of(ctx);
+ if (!s.threads_ok) return host_io.KindJob.runHere(s.core, pane, serial, token);
+ host_io.KindJob.start(s.io, s.core, &s.job_tasks, pane, serial, token, s, struct {
+ fn deliver(c: ?*anyopaque, j: *host_io.KindJob) void {
+ of(c).loop.postEvent(.{ .kind_job = j }) catch j.drop();
+ }
+ }.deliver);
+ }
+
/// The loop's queue waits for room (a wait the worker's cancel ends).
fn deliverSearch(ctx: ?*anyopaque, batch: pardes.Stream.Batch) bool {
const s = of(ctx);
diff --git a/src/wire.zig b/src/wire.zig
index e2f85d18..fda526ce 100644
--- a/src/wire.zig
+++ b/src/wire.zig
@@ -393,7 +393,7 @@ fn clientTag(msg: ClientMsg) ClientTag {
return switch (msg) {
.event => |ev| switch (ev) {
// Machine-local reports and 9P requests belong to the session owner.
- .output, .eof, .exited, .lsp_resp, .pipe_resp, .search, .file_changed, .handed, .addon, .tick => unreachable,
+ .output, .eof, .exited, .lsp_resp, .pipe_resp, .search, .file_changed, .handed, .addon, .job, .tick => unreachable,
inline else => |_, t| @field(ClientTag, @tagName(t)),
},
inline else => |_, t| @field(ClientTag, @tagName(t)),
@@ -550,7 +550,7 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 {
try w.writeByte(@intFromBool(f.native_image_patches));
},
// See `clientTag`: no tag, so nothing to encode.
- .output, .eof, .exited, .lsp_resp, .pipe_resp, .search, .file_changed, .handed, .addon, .tick => unreachable,
+ .output, .eof, .exited, .lsp_resp, .pipe_resp, .search, .file_changed, .handed, .addon, .job, .tick => unreachable,
},
}
try finishMessage(&w, at);
@@ -649,7 +649,7 @@ pub fn clientBound(msg: ClientMsg) usize {
.paste => |b| b.len,
.command => |line| line.len,
// See `clientTag`: not on this wire in this direction.
- .output, .eof, .exited, .lsp_resp, .pipe_resp, .search, .file_changed, .handed, .addon, .tick => unreachable,
+ .output, .eof, .exited, .lsp_resp, .pipe_resp, .search, .file_changed, .handed, .addon, .job, .tick => unreachable,
},
};
}
diff --git a/test/behaviour/addons.zig b/test/behaviour/addons.zig
index c91bda1a..523aefb9 100644
--- a/test/behaviour/addons.zig
+++ b/test/behaviour/addons.zig
@@ -33,12 +33,12 @@ fn unload(s: *Session, name: []const u8) void {
test "addons: Addon load takes a library in, /addons lists it with the kinds compiled in, its words join /commands and Help, and Addon unload lets it go" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
- try std.testing.expect(has(try s.read("/addons"), "image 1 1.0 0 - - static\n"));
+ try std.testing.expect(has(try s.read("/addons"), "image 1 2.0 0 - - static\n"));
try load(&s, fake.ok);
defer unload(&s, "fake");
// `<kind> <version> <abi> <panes open> <path> <blake3> <state>`.
const listed = try s.read("/addons");
- const at = std.mem.indexOf(u8, listed, "\nfake 1 1.0 0 /").?;
+ const at = std.mem.indexOf(u8, listed, "\nfake 1 2.0 0 /").?;
var fields = std.mem.tokenizeScalar(u8, listed[at + 1 .. std.mem.indexOfScalarPos(u8, listed, at + 1, '\n').?], ' ');
for (0..5) |_| _ = fields.next();
try std.testing.expectEqual(@as(usize, 64), fields.next().?.len);
@@ -52,7 +52,7 @@ test "addons: Addon load takes a library in, /addons lists it with the kinds com
// A pane of it open, it stays; closed, it goes.
try s.write("/ctl", "Fake\n");
const pane = try paneOf(&s, "fake");
- try std.testing.expect(has(try s.read("/addons"), "\nfake 1 1.0 1 /"));
+ try std.testing.expect(has(try s.read("/addons"), "\nfake 1 2.0 1 /"));
try std.testing.expect(try s.refused("/ctl", "Addon unload fake\n") != 0);
try std.testing.expect(has(s.said, "1 fake pane is open"));
try s.write(s.at("/pane/{d}/ctl", .{pane}), "Del\n");
@@ -69,10 +69,10 @@ test "addons: a library of another ABI, one with no pardes_addon, and one whose
defer s.deinit();
for ([_][]const u8{ fake.abi, fake.no_export, fake.core_word }) |path| try load(&s, path);
const listed = try s.read("/addons");
- try std.testing.expect(has(listed, "\nfakeabi 1 2.0 0 /"));
- try std.testing.expect(has(listed, " refused: wants abi 2, this pardes speaks 1\n"));
+ try std.testing.expect(has(listed, "\nfakeabi 1 3.0 0 /"));
+ try std.testing.expect(has(listed, " refused: wants abi 3, this pardes speaks 2\n"));
try std.testing.expect(has(listed, " refused: it has no pardes_addon\n"));
- try std.testing.expect(has(listed, "\nfakeword 1 1.0 0 /"));
+ try std.testing.expect(has(listed, "\nfakeword 1 2.0 0 /"));
try std.testing.expect(has(listed, " refused: word Save is the core's\n"));
// A file that is no library at all: listed with what the loader said.
var tmp = std.testing.tmpDir(.{});
@@ -189,6 +189,40 @@ test "addons: Addon reload moves every pane of the kind to the library as it is
try std.testing.expect(!std.mem.eql(u8, before, try s.read("/addons")));
}
+test "addons: a job under way across a reload answers into nothing, its library kept until it has; the reloaded pane's own jobs land" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ const copy = struct {
+ fn of(ss: *Session, d: std.Io.Dir, from: []const u8) !void {
+ try d.writeFile(std.testing.io, .{ .sub_path = "fake.so", .data = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, from, ss.arena.allocator(), .limited(64 << 20)) });
+ }
+ }.of;
+ try copy(&s, tmp.dir, fake.ok);
+ try s.write("/ctl", s.at("Addon load {s}/fake.so\n", .{dir}));
+ defer unload(&s, "fake");
+ try s.write("/ctl", "Fake\n");
+ const pane = try paneOf(&s, "fake");
+ try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeBump 5\n");
+ // A job taken by a worker, still running as the library is replaced
+ // by another (one with no record: the pane comes back at its place, 5,
+ // then its words' last values, FakeBump 5: 10).
+ s.hold_jobs = true;
+ try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeJob\n");
+ try copy(&s, tmp.dir, fake.no_dump);
+ try s.write("/ctl", "Addon reload fake\n");
+ try std.testing.expect(has(try s.screen(), "count 10 "));
+ // It answers now, from the old library's code: of another generation,
+ // the answer is dropped, and the old library goes after it.
+ s.releaseJobs();
+ try std.testing.expect(has(try s.screen(), "count 10 "));
+ try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeJob\n");
+ try std.testing.expect(has(try s.screen(), "count 16 "));
+}
+
test "addons: a Look at a file whose extension an addon claims opens a pane of its kind, and its words are in Help" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
diff --git a/test/fs_namespace.zig b/test/fs_namespace.zig
index ac638e41..939232fc 100644
--- a/test/fs_namespace.zig
+++ b/test/fs_namespace.zig
@@ -303,8 +303,8 @@ test "images decode through explicit filesystem mounts, and a dump reads one on
defer p.deinit();
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
- // Its decode lands at the step after it (a job, run here at once).
- p.update(.tick);
+ // Its decode is a job its host runs: here, at once.
+ while (p.nextEffect()) |e| p.perform(e);
_ = try p.render(arena.allocator());
const shown = p.surface.placements.items;
try std.testing.expectEqual(@as(usize, 1), shown.len);
diff --git a/test/mode.zig b/test/mode.zig
index 86de0488..a03d9dc1 100644
--- a/test/mode.zig
+++ b/test/mode.zig
@@ -92,7 +92,7 @@ test "Mode from actual tag Exec preserves parked file and output insert transact
test "Mode leaves image and PDF document modes unchanged" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
- const picture = try pardes.Kind.open(p, 1, &panes.Image.kind, "/mode-test.png", "");
+ const picture = try pardes.Kind.open(p, 1, pardes.Kind.image, "/mode-test.png", "");
try mode(p, 1, .normal);
try std.testing.expect(picture.addon != null);
if (comptime panes.Pdf.enabled) {
diff --git a/test/panes.zig b/test/panes.zig
index f51c2b27..2a24e62e 100644
--- a/test/panes.zig
+++ b/test/panes.zig
@@ -2250,7 +2250,15 @@ const ImageTests = struct {
test "pane-local renderer choices are visible in the image tag" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
- const pane = try pardes.Kind.open(p, p.freeSlot().?, &image.kind, "/tmp/picture.ppm", null);
+ pardes.image.start(std.testing.io, std.testing.allocator);
+ defer pardes.image.stop();
+ // A picture shown (its decode landed, a frame drawn): the core's
+ // art words after the kind's `img`, then its path.
+ const id = p.freeSlot().?;
+ const pane = try pardes.Kind.open(p, id, pardes.Kind.image, "/tmp/picture.ppm", "P6\n1 1\n255\n\x01\x02\x03");
+ while (p.nextEffect()) |e| p.perform(e);
+ pardes.exec.placeNew(p, 0, 0, id, .doc);
+ _ = try p.render(p.scratch.allocator());
pane.addon.?.art = .{ .glyph = true, .palette = .terminal, .ascii = false };
try std.testing.expectEqualStrings(
config.tag_image ++ " petscii:on palette:terminal ascii:off /tmp/picture.ppm",
diff --git a/test/replicas.zig b/test/replicas.zig
index 280b85f4..338dae16 100644
--- a/test/replicas.zig
+++ b/test/replicas.zig
@@ -483,7 +483,8 @@ fn push(q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, m: Msg) !void {
fn flushAll(sc: *replica.Screens, core: *pardes.Pardes, ps: []const u16, list: *std.ArrayListUnmanaged([]u8), q: *std.PriorityQueue(Msg, void, Msg.order), nn: *u64, now: u64, dl: []Link, rnd: std.Random, ow: u64, jt: u64, vs: *std.AutoHashMapUnmanaged(u64, u64), tot: *Totals) !void {
for (ps, 0..) |peer, c| {
list.clearRetainingCapacity();
- try sc.flush(core, peer, list);
+ // No pane of a kind here: the view a Shadow is painted for is moot.
+ try sc.flush(core, peer, 0, list);
for (list.items) |bytes| {
tot.deltas += 1;
tot.delta_bytes += bytes.len;
diff --git a/test/session.zig b/test/session.zig
index 87bf784e..4919c0a3 100644
--- a/test/session.zig
+++ b/test/session.zig
@@ -34,6 +34,11 @@ pub const Session = struct {
/// What the language words' questions are asked of their servers
/// through (initLanguage); none, and they go unanswered.
languages: ?std.Io = null,
+ /// Kinds' jobs taken as a host's worker takes them and held, not run,
+ /// until `releaseJobs`: a job under way across what a test does
+ /// meanwhile (a reload).
+ hold_jobs: bool = false,
+ held: std.ArrayList(*pardes.host_io.KindJob) = .empty,
/// A session showing one file pane holding `text`, keeping what outlives
/// it in a database in memory (src/Db.zig).
@@ -87,8 +92,8 @@ pub const Session = struct {
}
pub fn deinit(s: *Session) void {
+ s.held.deinit(pardesOf(s).gpa);
pardesOf(s).deinit();
- pardes.Addon.unloadAll();
s.arena.deinit();
}
@@ -229,12 +234,26 @@ pub const Session = struct {
.save_file, .save_text => s.saved += 1,
// Into the session's state (a database in memory, or its file).
.write_dump => pardes.host_io.writeDump(pardesOf(s), pardesOf(s).dump_out.?),
- .addon => |a| pardes.Addon.perform(pardesOf(s), a.op, a.pane, a.arg.slice()),
+ .addon => pardesOf(s).perform(e),
+ // A kind's job, here and at once, as the host's worker would;
+ // held while a test holds them (`hold_jobs`).
+ .job => |j| if (!s.hold_jobs) pardesOf(s).perform(e) else if (pardes.host_io.KindJob.take(pardesOf(s), j.pane, j.serial, j.token)) |taken| s.held.append(pardesOf(s).gpa, taken) catch taken.drop(),
.lsp => |q| if (s.languages) |io| s.askServer(io, .{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }),
else => {},
};
}
+ /// The jobs held, run now and answered as the host's workers would.
+ pub fn releaseJobs(s: *Session) void {
+ s.hold_jobs = false;
+ for (s.held.items) |j| {
+ j.run();
+ j.land(pardesOf(s));
+ }
+ s.held.clearRetainingCapacity();
+ s.drain();
+ }
+
/// A language word's question asked of its server as a host asks it
/// (host_io.Lsp), here and at once: its answer the core's next event.
fn askServer(s: *Session, io: std.Io, req: pardes.host_io.Lsp.Request) void {