summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--abi/addon.zig24
-rw-r--r--addons/pdf/pdf.zig57
-rw-r--r--build/core.zig6
-rw-r--r--build/tests.zig1
-rw-r--r--core/Addon.zig2
-rw-r--r--core/Kind.zig17
6 files changed, 69 insertions, 38 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");
diff --git a/addons/pdf/pdf.zig b/addons/pdf/pdf.zig
index 8f314bbe..79d79219 100644
--- a/addons/pdf/pdf.zig
+++ b/addons/pdf/pdf.zig
@@ -759,7 +759,7 @@ pub const Pdf = extern struct {
const found = in.n > 0 and m.pardes_pdf_search_page(ld, @intCast(in.page), &q, &r) == ok;
defer if (found) m.pardes_pdf_drop_search_result(ld, r.handle);
res.ok, res.n, res.hits = .{ 1, @intCast(t.len), if (found) @intCast(r.hit_count) else 0 };
- return answer(gpa, res, std.mem.concat(gpa, u8, &.{ t.data[0..t.len], if (found) std.mem.sliceAsBytes(r.quads[0..r.quad_count]) else "" }) catch return null);
+ return answer(gpa, res, std.mem.concat(gpa, u8, &.{ addon.cSlice(t.data, t.len), if (found) std.mem.sliceAsBytes(addon.cSlice(r.quads, r.quad_count)) else "" }) catch return null);
}
// each page's count of hits, then the rows
var shown = @min(in.n, 200);
@@ -1011,7 +1011,7 @@ fn pageText(o: *Own, page: u32) []const u8 {
var t: m.pardes_pdf_text = undefined;
if (m.pardes_pdf_page_text(d.base, @intCast(page), &t) != ok) return "";
defer m.pardes_pdf_drop_text(d.base, t.handle);
- o.read.bytes = heap.dupe(u8, t.data[0..t.len]) catch &.{};
+ o.read.bytes = heap.dupe(u8, addon.cSlice(t.data, t.len)) catch &.{};
return o.read.bytes;
}
@@ -1030,7 +1030,7 @@ fn hitsOf(st: *const Pdf, o: *Own) []const m.pardes_pdf_search_quad {
var r: m.pardes_pdf_search_result = undefined;
if (m.pardes_pdf_search_page(d.base, @intCast(f.hits_page), &q, &r) != ok) return &.{};
defer m.pardes_pdf_drop_search_result(d.base, r.handle);
- o.hits = heap.dupe(m.pardes_pdf_search_quad, r.quads[0..r.quad_count]) catch &.{};
+ o.hits = heap.dupe(m.pardes_pdf_search_quad, addon.cSlice(r.quads, r.quad_count)) catch &.{};
return o.hits;
}
@@ -1073,8 +1073,8 @@ fn selectHere(o: *Own, page: u32, from: Point, to: Point) ?usize {
var t: m.pardes_pdf_owned_text = undefined;
if (m.pardes_pdf_copy_selection(base, @intCast(page), sel.start, sel.end, &t) != ok) return null;
defer m.pardes_pdf_drop_owned_text(base, t.handle);
- o.sel_quads = heap.dupe(Quad, sel.quads[0..sel.quad_count]) catch &.{};
- o.sel_text = heap.dupe(u8, t.data[0..t.len]) catch &.{};
+ o.sel_quads = heap.dupe(Quad, addon.cSlice(sel.quads, sel.quad_count)) catch &.{};
+ o.sel_text = heap.dupe(u8, addon.cSlice(t.data, t.len)) catch &.{};
return sel.quad_count;
}
@@ -1691,11 +1691,11 @@ fn probeAt(st: *Pdf, id: u8, col: u16, row: u16) ?struct { page: u32, quad: Quad
const uri = struct {
var buf: [1024]u8 = undefined;
};
- @memcpy(uri.buf[0..link.uri_len], link.uri[0..link.uri_len]);
+ @memcpy(uri.buf[0..link.uri_len], addon.cSlice(link.uri, link.uri_len));
link.uri = &uri.buf;
var text: []u8 = &.{};
if (m.pardes_pdf_copy_rectangle(base, @intCast(at.page), link.quad, &t) == ok) {
- text = heap.dupe(u8, t.data[0..t.len]) catch &.{};
+ text = heap.dupe(u8, addon.cSlice(t.data, t.len)) catch &.{};
m.pardes_pdf_drop_owned_text(base, t.handle);
}
return .{ .page = at.page, .quad = link.quad, .text = text, .link = link };
@@ -1706,7 +1706,7 @@ fn probeAt(st: *Pdf, id: u8, col: u16, row: u16) ?struct { page: u32, quad: Quad
if (sel.quad_count == 0 or m.pardes_pdf_copy_selection(base, @intCast(at.page), sel.start, sel.end, &t) != ok) return null;
defer m.pardes_pdf_drop_owned_text(base, t.handle);
if (t.len == 0) return null;
- return .{ .page = at.page, .quad = sel.quads[0], .text = heap.dupe(u8, t.data[0..t.len]) catch return null, .link = null };
+ return .{ .page = at.page, .quad = addon.cSlice(sel.quads, sel.quad_count)[0], .text = heap.dupe(u8, addon.cSlice(t.data, t.len)) catch return null, .link = null };
}
/// A link followed: a place in the document scrolled to, else looked at
@@ -1714,7 +1714,7 @@ fn probeAt(st: *Pdf, id: u8, col: u16, row: u16) ?struct { page: u32, quad: Quad
/// both, a +Links.
fn follow(st: *Pdf, link: m.pardes_pdf_link, text: []const u8) void {
if (link.destination_kind == m.PARDES_PDF_OUTLINE_DESTINATION_INTERNAL) return goToXY(st, @intCast(link.page), link.has_x != 0, link.x, link.has_y != 0, link.y);
- const uri = link.uri[0..link.uri_len];
+ const uri = addon.cSlice(link.uri, link.uri_len);
var shown: [512]u8 = undefined;
var k: usize = 0;
for (text) |ch| if (ch > ' ' and k < shown.len) {
@@ -1749,10 +1749,10 @@ fn goToXY(st: *Pdf, page: u32, has_x: bool, x: f32, has_y: bool, y: f32) void {
/// from 1), indented by depth; an entry with nowhere to go takes its first
/// child's that has, and an http(s) one is its URI.
fn usable(o: *const m.pardes_pdf_outline_result, i: usize) bool {
- const it = o.items[i];
+ const it = addon.cSlice(o.items, o.item_count)[i];
if (it.destination_kind == m.PARDES_PDF_OUTLINE_DESTINATION_INTERNAL) return true;
if (it.destination_kind != m.PARDES_PDF_OUTLINE_DESTINATION_EXTERNAL or it.uri_len == 0 or it.uri_len > 256) return false;
- const u = o.bytes[it.uri_offset..][0..it.uri_len];
+ const u = addon.cSlice(o.bytes, o.bytes_len)[it.uri_offset..][0..it.uri_len];
return (std.mem.startsWith(u8, u, "http://") or std.mem.startsWith(u8, u, "https://")) and for (u) |ch| {
if (ch <= 0x20 or ch == 0x7f) break false;
} else true;
@@ -1760,9 +1760,10 @@ fn usable(o: *const m.pardes_pdf_outline_result, i: usize) bool {
/// Entry `i`'s place: its own, else the first usable one below it.
fn resolve(o: *const m.pardes_pdf_outline_result, i: usize) ?usize {
- if (o.items[i].destination_kind != m.PARDES_PDF_OUTLINE_DESTINATION_NONE) return if (usable(o, i)) i else null;
+ const items = addon.cSlice(o.items, o.item_count);
+ if (items[i].destination_kind != m.PARDES_PDF_OUTLINE_DESTINATION_NONE) return if (usable(o, i)) i else null;
var k = i + 1;
- while (k < o.item_count and o.items[k].depth > o.items[i].depth) : (k += 1) if (usable(o, k)) return k;
+ while (k < items.len and items[k].depth > items[i].depth) : (k += 1) if (usable(o, k)) return k;
return null;
}
@@ -1774,13 +1775,15 @@ fn sections(st: *Pdf) void {
const rows = heap.alloc(u8, o.item_count * 300 + 1) catch return;
defer heap.free(rows);
var w: std.Io.Writer = .fixed(rows);
- for (o.items[0..o.item_count], 0..) |it, i| {
- const to = o.items[resolve(&o, i) orelse continue];
- if (to.destination_kind == m.PARDES_PDF_OUTLINE_DESTINATION_INTERNAL) w.print("{s}:{d}:{d} ", .{ ownOf(st).name, to.page + 1, i + 1 }) catch {} else w.print("{s} ", .{o.bytes[to.uri_offset..][0..to.uri_len]}) catch {};
+ const items = addon.cSlice(o.items, o.item_count);
+ const bytes = addon.cSlice(o.bytes, o.bytes_len);
+ for (items, 0..) |it, i| {
+ const to = items[resolve(&o, i) orelse continue];
+ if (to.destination_kind == m.PARDES_PDF_OUTLINE_DESTINATION_INTERNAL) w.print("{s}:{d}:{d} ", .{ ownOf(st).name, to.page + 1, i + 1 }) catch {} else w.print("{s} ", .{bytes[to.uri_offset..][0..to.uri_len]}) catch {};
w.splatByteAll(' ', 2 * it.depth) catch {};
// the title on one line: control and invalid characters a space
// between words, none at either end
- const t = o.bytes[it.title_offset..][0..it.title_len];
+ const t = bytes[it.title_offset..][0..it.title_len];
const start = w.end;
var gap = false;
var i_: usize = 0;
@@ -1811,9 +1814,9 @@ fn section(st: *Pdf, page: u32, ord: u32) bool {
defer if (o.handle != null) m.pardes_pdf_drop_outline_result(base, o.handle);
var said: [200]u8 = undefined;
const r = if (m.pardes_pdf_load_outline(base, &o) == ok and ord > 0 and ord <= o.item_count) resolve(&o, ord - 1) else null;
- const to = o.items[r orelse return miss(std.fmt.bufPrint(&said, "PdfSections: {s} has no section {d}", .{ ownOf(st).name, ord }))];
+ const to = addon.cSlice(o.items, o.item_count)[r orelse return miss(std.fmt.bufPrint(&said, "PdfSections: {s} has no section {d}", .{ ownOf(st).name, ord }))];
if (to.destination_kind == m.PARDES_PDF_OUTLINE_DESTINATION_EXTERNAL) {
- addon.look(o.bytes[to.uri_offset..][0..@min(to.uri_len, 256)]);
+ addon.look(addon.cSlice(o.bytes, o.bytes_len)[to.uri_offset..][0..@min(to.uri_len, 256)]);
return true;
}
if (to.page + 1 != page) return miss(std.fmt.bufPrint(&said, "PdfSections: section {d} of {s} is on page {d}, not {d}", .{ ord, ownOf(st).name, to.page + 1, page }));
@@ -1831,3 +1834,19 @@ fn miss(said: anyerror![]const u8) bool {
fn noText() bool {
return miss("has no text to address; its address is a page, :N");
}
+
+test "every pointer the bridge's structs hand back is read through addon.cSlice: none of them sliced or indexed bare (a NULL for none sliced at 0 was a release build's abort)" {
+ var src: []const u8 = @embedFile("pdf.zig"); // (searched at run time: at comptime it is over the branch quota)
+ _ = &src;
+ @setEvalBranchQuota(1_000_000); // (the translated header's every declaration)
+ inline for (@typeInfo(m).@"struct".decl_names) |name| {
+ if (comptime !std.mem.startsWith(u8, name, "pardes_pdf_") or @TypeOf(@field(m, name)) != type or @typeInfo(@field(m, name)) != .@"struct") continue;
+ const S = @typeInfo(@field(m, name)).@"struct";
+ inline for (S.field_names, S.field_types) |field, F| if (comptime @typeInfo(F) == .pointer and @typeInfo(F).pointer.size == .c) {
+ if (std.mem.indexOf(u8, src, "." ++ field ++ "[")) |at| {
+ std.debug.print("pdf.zig:{d}: {s}.{s} read bare: addon.cSlice(ptr, count)\n", .{ std.mem.count(u8, src[0..at], "\n") + 1, name, field });
+ return error.BareCPointer;
+ }
+ };
+ }
+}
diff --git a/build/core.zig b/build/core.zig
index 32228ab2..f9cb450f 100644
--- a/build/core.zig
+++ b/build/core.zig
@@ -310,8 +310,10 @@ pub const Parts = struct {
pdf: ?*std.Build.Module,
term: *std.Build.Module,
plain: *std.Build.Module,
- /// The kinds' C ABI (abi/pardes_addon.h), translated.
+ /// The kinds' C ABI (abi/pardes_addon.h), translated, and what an addon
+ /// in Zig imports over it (abi/addon.zig: the core's `cSlice` too).
kind: *std.Build.Module,
+ addon: *std.Build.Module,
themes: *std.Build.Module,
ts: TreeSitter,
mvzr: *std.Build.Module,
@@ -401,6 +403,7 @@ pub fn module(b: *std.Build, p: Parts, c: Core) Modules {
if (c.terminal) x.addImport("term", p.term);
x.addImport("plain", p.plain);
x.addImport("pardes_addon", p.kind);
+ x.addImport("addon", p.addon);
x.addImport("generated_themes", p.themes);
x.addImport("tree-sitter", p.ts.mod);
x.addImport("ts_queries", ts_queries);
@@ -632,6 +635,7 @@ pub fn assemble(b: *std.Build, c: struct {
.term = term_mod,
.plain = plain_mod,
.kind = kind_mod,
+ .addon = addon_mod,
.themes = themes_mod,
.ts = ts,
.mvzr = mvzr_mod,
diff --git a/build/tests.zig b/build/tests.zig
index e0b33911..5e913501 100644
--- a/build/tests.zig
+++ b/build/tests.zig
@@ -162,6 +162,7 @@ pub fn add(b: *std.Build, c: struct {
run_replicas.has_side_effects = true;
b.step("replicas", "one authority and several replicas in process, each client typing over a delayed link: digests at quiescence, rollback rate and costs per RTT (-- [--seeds N] [--steps M] [--clients C] [--rtt 30,80,207])").dependOn(&run_replicas.step);
for ([_]*std.Build.Module{ snake_mod, life_mod, liar_mod, c.parts.image }) |m| addon_test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m })).step);
+ if (c.pdf_addon) |lib| addon_test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = lib.root_module })).step);
// ...and the pdf addon's, which mupdf-check runs too (its script
// opens docs/design.pdf, from the build root).
const libs = [_]struct { ?*std.Build.Step.Compile, []const u8 }{ .{ fake_addon_lib.?, "addons/fake/fake" }, .{ snake_lib, "addons/snake/snake" }, .{ c.snake_shader_lib, "addons/snake_shader/snake_shader" }, .{ life_lib, "addons/life/life" }, .{ c.pdf_addon, "addons/pdf/pdf" } };
diff --git a/core/Addon.zig b/core/Addon.zig
index aa3195f9..e93a43dc 100644
--- a/core/Addon.zig
+++ b/core/Addon.zig
@@ -532,7 +532,7 @@ pub const LoadedWord = struct { name: []const u8, leader: []const u8 = "", arg:
/// `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)];
+ const head_words = @import("addon").cSlice(h.words, @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),
diff --git a/core/Kind.zig b/core/Kind.zig
index 49076f78..5d2dcdb0 100644
--- a/core/Kind.zig
+++ b/core/Kind.zig
@@ -17,6 +17,7 @@
const std = @import("std");
const builtin = @import("builtin");
pub const c = @import("pardes_addon");
+const cSlice = @import("addon").cSlice;
const pardes = @import("root.zig");
const walk = @import("walk.zig");
const surface = @import("surface.zig");
@@ -1130,7 +1131,7 @@ pub const api: c.pa_api = .{
.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]);
+ if (!cl.quiet and text != null) cl.look.set(cSlice(text, n));
}
}.f,
.say = struct {
@@ -1138,8 +1139,8 @@ pub const api: c.pa_api = .{
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]);
+ if (err != 0 and cl.opening) return cl.refused.set(cSlice(text, n));
+ cl.said.set(cSlice(text, n));
cl.err = err != 0;
}
}.f,
@@ -1149,8 +1150,8 @@ pub const api: c.pa_api = .{
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 "";
+ cl.pane_name.set(cSlice(name, name_len));
+ cl.pane_text = p.scratch.allocator().dupe(u8, cSlice(bytes, n)) catch "";
return p.next_serial + 1;
}
}.f,
@@ -1181,7 +1182,7 @@ pub const api: c.pa_api = .{
.exec = struct {
fn f(text: [*c]const u8, n: u32) callconv(.c) void {
const cl = current orelse return;
- if (!cl.quiet and text != null) cl.exec.set(text[0..n]);
+ if (!cl.quiet and text != null) cl.exec.set(cSlice(text, n));
}
}.f,
.select = struct {
@@ -1189,7 +1190,7 @@ pub const api: c.pa_api = .{
const cl = current orelse return;
if (cl.quiet) return;
const p = cl.p orelse return;
- cl.selected = p.scratch.allocator().dupe(u8, if (text == null) "" else text[0..n]) catch return;
+ cl.selected = p.scratch.allocator().dupe(u8, cSlice(text, n)) catch return;
}
}.f,
.cancel = struct {
@@ -1234,7 +1235,7 @@ pub const api: c.pa_api = .{
const cl = current orelse return;
const p = cl.p orelse return;
if (cl.quiet or rows == null) return;
- cl.rows = std.mem.concat(p.scratch.allocator(), u8, &.{ cl.rows, rows[0..n] }) catch return;
+ cl.rows = std.mem.concat(p.scratch.allocator(), u8, &.{ cl.rows, cSlice(rows, n) }) catch return;
}
}.f,
};