From 9589f6878ffd2501b4ce5a034ead58119d38749c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 20:57:28 -0300 Subject: kinds #42, what a Zig kind over a C library needs of the module and the core: addon.zig's `init` at load (a library's own setup, MuPDF's allocator) and a body the kind holds (`body(s) []const u8`, any length: a PDF page's text is past body_max), the Debug budget counting only the kind's own time (the file's hash before `open` and pa_api.heap's allocations are the core's: a region's first took 23 ms of a PDF's open, which was the breach), `current` the loop's thread's (a job's heap call is in no call), an Output's arg set from itself (a kind's rows found after its search refill its +Search with its own arg: the @memcpy aliased), and the docs test reading the words of Zig kinds (`.{ .name = "PdfFit"`) beside C's Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/kinds/addon.zig | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'src/kinds') diff --git a/src/kinds/addon.zig b/src/kinds/addon.zig index 804630ee..f921768f 100644 --- a/src/kinds/addon.zig +++ b/src/kinds/addon.zig @@ -2,23 +2,25 @@ //! built to (pardes_addon.h, translated: `c`), which stays the contract. //! It imports nothing of the core. A kind is a struct whose `head` names //! it and whose functions are any of pa_ops's, each over a `*T` that -//! `open` is handed zeroed (its defaults set) in the pane's arena: `open`, -//! `close`, `tick`, `prefix`, `paint` (required), `view`, `key`, `mouse`, +//! `open` is handed zeroed (its defaults set) in the pane's arena: `init` +//! (once, at load), `open`, `close`, `tick`, `prefix`, `paint` (required), `view`, `key`, `mouse`, //! `body`, `address`, `where`, `ctl`, `ctlRead`, `jobRun`, `jobDone`, //! `dump`, `load` and `search`, with these types: //! +//! init() bool false refuses the addon //! open(s, path: ?[]const u8, bytes: ?[]const u8) void //! tick(s, now: u64, flags: Flags) u64 the next step's time, 0 none //! prefix(s, w: *Writer) !void its tag's start, after every tick //! paint(s, Window, Paint) void view(s, View) void //! key(s, Key) bool mouse(s, Mouse) bool address(s, text) bool //! body, where, ctlRead(s, w: *Writer) !void ctl(s, line, reply: *Writer) bool +//! body(s) []const u8 a body the kind holds, of any length //! jobRun(in, Allocator) ?[]u8 jobDone(s, token, out) void //! dump(s, buf) usize load(s, bytes, version) bool search(s, pat) void //! //! and pardes's side (pa_api) as the functions below: `arena` and `heap` //! as Allocators, `seed`, `say`, `job`, `cancel`, `look`, `text`, `exec`, -//! `select` and `found`. A body is at most `body_max` bytes. +//! `select` and `found`. A body written is at most `body_max` bytes. //! //! export const pardes_addon = addon.ops(Snake); //! @@ -248,7 +250,7 @@ pub fn ops(comptime T: type) c.pa_ops { } fn init(a: [*c]const c.pa_api) callconv(.c) c_int { api = a; - return c.PA_OK; + return if (!@hasDecl(T, "init") or T.init()) c.PA_OK else 1; } fn open(path: [*c]const u8, bytes: [*c]const u8, len: u64) callconv(.c) ?*anyopaque { const s = arena.create(T) catch return null; @@ -276,14 +278,17 @@ pub fn ops(comptime T: type) c.pa_ops { fn mouse(st: ?*anyopaque, m: [*c]const c.pa_mouse) callconv(.c) c_int { return @intFromBool(T.mouse(self(st) orelse return 0, @as(*const Mouse, @ptrCast(m)).*)); } - /// The body, written whole and read in pieces. + /// The body, the kind's own or written whole, read in pieces. fn text_(st: ?*anyopaque, buf: *[body_max]u8) []const u8 { - var w: std.Io.Writer = .fixed(buf); - T.body(self(st) orelse return "", &w) catch if (@import("builtin").mode == .debug and !body_cut) { - body_cut = true; - std.log.warn("{s}: a body cut at {d} bytes (addon.body_max)", .{ T.head.name, body_max }); - }; - return w.buffered(); + const s = self(st) orelse return ""; + if (comptime @typeInfo(@TypeOf(T.body)).@"fn".param_types.len == 1) return T.body(s) else { + var w: std.Io.Writer = .fixed(buf); + T.body(s, &w) catch if (@import("builtin").mode == .debug and !body_cut) { + body_cut = true; + std.log.warn("{s}: a body cut at {d} bytes (addon.body_max)", .{ T.head.name, body_max }); + }; + return w.buffered(); + } } fn body_len(st: ?*anyopaque) callconv(.c) u64 { var buf: [body_max]u8 = undefined; @@ -344,7 +349,7 @@ pub fn ops(comptime T: type) c.pa_ops { // A word for its pane goes to `ctl`. if (!@hasDecl(T, "ctl")) for (T.head.words) |w| if (!w.opens) @compileError(@typeName(T) ++ "'s word " ++ w.name ++ " opens no pane, and it has no ctl to take it"); // A public function of another name (a typo) would be left out unsaid. - const names = [_][]const u8{ "open", "paint" } ++ blk: { + const names = [_][]const u8{ "init", "open", "paint" } ++ blk: { var ns: [fns.len][]const u8 = undefined; for (fns, &ns) |m, *n| n.* = m[0]; break :blk ns; -- cgit v1.3