diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-23 09:15:57 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-23 09:25:44 -0300 |
| commit | c8f0d65a69274dc8a072c35a6c13cb07a6770007 (patch) | |
| tree | b3e13754d99250ee596be3368d07539eb4437bfb /abi/addon.zig | |
| parent | 0733d0a0d89bacb64f45d71b06ffbcf1abe6ffb3 (diff) | |
| download | pardes-c8f0d65a69274dc8a072c35a6c13cb07a6770007.tar.gz pardes-c8f0d65a69274dc8a072c35a6c13cb07a6770007.zip | |
pdf, abi, core: the pointer and count a C struct or a C caller hands over read through one helper, `addon.cSlice` (abi/addon.zig), NULL being none whatever the count: the bridge says none with a NULL (pardes_pdf_search_page with no hit, pardes_pdf_select over no text, pardes_pdf_load_outline of no entries: each OK with a NULL and a count of 0), and Zig slices a NULL C pointer with a safety panic even at length 0; every site in pdf.zig (jobRun's page text and hits, pageText, hitsOf, selectHere, probeAt, follow, the outline's usable, resolve, sections and section, its bytes now bounded by bytes_len), abi/addon.zig's own (its private `span` was that helper for bytes alone; open's bytes), and the core's kind-ABI callbacks (Kind.zig look, say, text, exec, select, found; Addon.zig loadedOf's words), the core taking the addons' module (`addon`) for it; what holds the next site: pdf.zig's test (run by addon-test, which now runs the pdf module's tests) takes every typed pointer field of the bridge's structs from the translated header at comptime and fails on any `.field[` in pdf.zig, naming the line (shown by putting one site back: "pdf.zig:1014: pardes_pdf_text.data read bare"); the test of the change before passes, with every PDF behaviour test (8/8, Debug)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'abi/addon.zig')
| -rw-r--r-- | abi/addon.zig | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/abi/addon.zig b/abi/addon.zig index 1fa2279a..059e016d 100644 --- a/abi/addon.zig +++ b/abi/addon.zig @@ -248,8 +248,14 @@ pub fn loadBytes(s: anytype, bytes: []const u8, version: u32) bool { pub const body_max = 4096; var body_cut = false; -fn span(p: [*c]const u8, n: u64) []const u8 { - return if (p == null) "" else p[0..n]; +/// A C pointer and its count as a slice; NULL is none, whatever the count: +/// C says none with a NULL (a page's search without a hit, a selection +/// over no text, an outline of no entries), and a NULL C pointer sliced +/// is a safety panic even at length 0 (a release build's abort). Every +/// pointer a C struct or a C caller hands with its count is read through +/// it: pdf.zig's test holds the bridge's to it. +pub fn cSlice(ptr: anytype, n: usize) []const std.meta.Child(@TypeOf(ptr)) { + return if (ptr == null) &.{} else ptr[0..n]; } /// `pardes_addon` exported, when built as a library alone: the kind is its @@ -282,7 +288,7 @@ pub fn ops(comptime T: type) c.pa_ops { fn open(path: [*c]const u8, bytes: [*c]const u8, len: u64) callconv(.c) ?*anyopaque { const s = arena.create(T) catch return null; s.* = std.mem.zeroInit(T, .{}); - if (@hasDecl(T, "open")) T.open(s, if (path == null) null else std.mem.span(path), if (bytes == null) null else bytes[0..len]); + if (@hasDecl(T, "open")) T.open(s, if (path == null) null else std.mem.span(path), if (bytes == null) null else cSlice(bytes, len)); return s; } fn close(st: ?*anyopaque) callconv(.c) void { @@ -329,7 +335,7 @@ pub fn ops(comptime T: type) c.pa_ops { return @intCast(from.len); } fn address(st: ?*anyopaque, t: [*c]const u8, n: u32) callconv(.c) c_int { - return @intFromBool(T.address(self(st) orelse return 0, span(t, n))); + return @intFromBool(T.address(self(st) orelse return 0, cSlice(t, n))); } fn where(st: ?*anyopaque, buf: [*c]u8, n: u32) callconv(.c) u32 { return write(T.where, self(st) orelse return 0, buf, n); @@ -337,26 +343,26 @@ pub fn ops(comptime T: type) c.pa_ops { fn ctl(st: ?*anyopaque, line: [*c]const u8, n: u32, reply: [*c]u8, cap: u32) callconv(.c) c_int { var w: std.Io.Writer = .fixed(reply[0..cap -| 1]); defer reply[w.end] = 0; - return if (T.ctl(self(st) orelse return 1, span(line, n), &w)) c.PA_OK else 1; + return if (T.ctl(self(st) orelse return 1, cSlice(line, n), &w)) c.PA_OK else 1; } fn ctl_read(st: ?*anyopaque, buf: [*c]u8, n: u32) callconv(.c) u32 { return write(T.ctlRead, self(st) orelse return 0, buf, n); } fn job_run(in: [*c]const u8, n: u32, out: [*c]c.pa_out) callconv(.c) u32 { const gpa: Allocator = .{ .ptr = out, .vtable = &.{ .alloc = jobAlloc, .resize = Allocator.noResize, .remap = Allocator.noRemap, .free = Allocator.noFree } }; - return @intCast((T.jobRun(span(in, n), gpa) orelse return 0).len); + return @intCast((T.jobRun(cSlice(in, n), gpa) orelse return 0).len); } fn job_done(st: ?*anyopaque, token: u32, out: [*c]const u8, n: u32) callconv(.c) void { - T.jobDone(self(st) orelse return, token, span(out, n)); + T.jobDone(self(st) orelse return, token, cSlice(out, n)); } fn dump(st: ?*anyopaque, buf: [*c]u8, n: u32) callconv(.c) u32 { return @intCast(T.dump(self(st) orelse return 0, buf[0..n])); } fn load(st: ?*anyopaque, buf: [*c]const u8, n: u32, version: u32) callconv(.c) c_int { - return @intFromBool(!T.load(self(st) orelse return 1, span(buf, n), version)); + return @intFromBool(!T.load(self(st) orelse return 1, cSlice(buf, n), version)); } fn search(st: ?*anyopaque, pat: [*c]const u8, n: u32) callconv(.c) void { - T.search(self(st) orelse return, span(pat, n)); + T.search(self(st) orelse return, cSlice(pat, n)); } }; if (T.head.name.len > 15) @compileError(@typeName(T) ++ "'s name " ++ T.head.name ++ " is over 15 bytes"); |
