diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Kind.zig | 11 | ||||
| -rw-r--r-- | src/Output.zig | 3 | ||||
| -rw-r--r-- | src/config.zig | 23 | ||||
| -rw-r--r-- | src/kinds/addon.zig | 29 |
4 files changed, 44 insertions, 22 deletions
diff --git a/src/Kind.zig b/src/Kind.zig index db3bee49..27cd1a6d 100644 --- a/src/Kind.zig +++ b/src/Kind.zig @@ -279,6 +279,8 @@ pub fn reopen(p: ?*Pardes, id: usize, s: *Slot, bytes: ?[]const u8) bool { // Its file not read (not here): it waits for it, a placeholder. if (s.ops != null and bytes == null and s.path.len > 0) _ = cl.refused.print("{s} is not here", .{s.path}) else if (s.ops) |ops| { const path_z = s.arena.allocator().dupeSentinel(u8, s.path, 0) catch ""; + // (the budget's from here: the file's hash and the tag are the core's) + if (checked) cl.began = @import("FrameStats.zig").nowNs(); 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; @@ -917,7 +919,8 @@ const Call = struct { return o; } }; -var current: ?*Call = null; +/// (The loop's: a job on a worker is in none.) +threadlocal var current: ?*Call = null; /// A call's budget, in Debug: a frame (17.4; 2 ms in ReleaseFast, which /// Debug's kinds run three to four times slower than). @@ -1052,6 +1055,12 @@ pub const api: c.pa_api = .{ .heap = struct { /// `heap`'s, any thread: realloc's, n 0 a free. fn f(ptr: ?*anyopaque, n: usize) callconv(.c) ?*anyopaque { + // Its time the core's, as an arena's is (a region's first + // allocation in Debug took 23 ms of a PDF's open). + const t0 = if (checked) @import("FrameStats.zig").nowNs(); + defer if (checked) if (current) |cl| { + cl.began += @import("FrameStats.zig").nowNs() -| t0; + }; if (n == 0) { kind_heap.free(ptr); return null; diff --git a/src/Output.zig b/src/Output.zig index c719f909..f1ce7ab9 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -244,7 +244,8 @@ test "plain output does not opt into location decoration" { pub fn setArg(o: *State, text: []const u8) error{ArgumentTooLong}!void { if (text.len > max_arg) return error.ArgumentTooLong; o.arg_len = @intCast(text.len); - @memcpy(o.arg_buf[0..o.arg_len], text); + // (its own arg again, a kind's rows found after its search) + std.mem.copyForwards(u8, o.arg_buf[0..o.arg_len], text); } pub const Traits = builtins.OutputTraits; diff --git a/src/config.zig b/src/config.zig index a2aed287..ee7d76f7 100644 --- a/src/config.zig +++ b/src/config.zig @@ -1489,8 +1489,9 @@ const doc_typ = struct { /// The other build's builtins (docs/typ/builtins-*.json), so the docs /// may name a window-only word in a terminal build's test, and back. var other_build: []const u8 = ""; - /// The shipped addons' sources (addons/*/*.c): their words are named - /// in the docs too, `PA_WORD("PdfFit", ...`. + /// The shipped addons' sources (addons/*/*.c and *.zig): their words + /// are named in the docs too, `PA_WORD("PdfFit", ...` or a Zig kind's + /// `.{ .name = "PdfFit"`. var addon_sources: []const u8 = ""; fn loadAddons(gpa: std.mem.Allocator) !void { @@ -1502,20 +1503,28 @@ const doc_typ = struct { var sub = try root.openDir(io, k.name, .{ .iterate = true }); defer sub.close(io); var files = sub.iterate(); - while (try files.next(io)) |f| if (std.mem.endsWith(u8, f.name, ".c")) { + while (try files.next(io)) |f| if (std.mem.endsWith(u8, f.name, ".c") or std.mem.endsWith(u8, f.name, ".zig")) { addon_sources = try std.mem.concat(gpa, u8, &.{ addon_sources, try sub.readFileAlloc(io, f.name, gpa, .limited(1 << 20)) }); }; }; } + /// One of the shipped addons' words, a C kind's or a Zig kind's. + fn addonWord(name: []const u8) bool { + var buf: [96]u8 = undefined; + const c = std.fmt.bufPrint(&buf, "PA_WORD(\"{s}\",", .{name}) catch return false; + if (std.mem.indexOf(u8, addon_sources, c) != null) return true; + const zig = std.fmt.bufPrint(&buf, ".{{ .name = \"{s}\"", .{name}) catch return false; + return std.mem.indexOf(u8, addon_sources, zig) != null; + } + fn word(w: []const u8) bool { const name = w[0 .. std.mem.indexOfScalar(u8, w, '+') orelse w.len]; if (std.meta.stringToEnum(Builtin, name) != null) return true; var buf: [96]u8 = undefined; const needle = std.fmt.bufPrint(&buf, "{{\"name\": \"{s}\",", .{name}) catch return false; if (std.mem.indexOf(u8, other_build, needle) != null) return true; - const addon = std.fmt.bufPrint(&buf, "PA_WORD(\"{s}\",", .{name}) catch return false; - return std.mem.indexOf(u8, addon_sources, addon) != null; + return addonWord(name); } fn chord(tok: []const u8) ?Chord { @@ -1605,9 +1614,7 @@ const doc_typ = struct { if (!std.mem.startsWith(u8, shown, "Term ") or !std.mem.endsWith(u8, shown, " Collapse Del")) return false; var mid = std.mem.tokenizeScalar(u8, shown["Term ".len .. shown.len - " Collapse Del".len], ' '); while (mid.next()) |x| { - var buf: [96]u8 = undefined; - const addon = std.fmt.bufPrint(&buf, "PA_WORD(\"{s}\",", .{x}) catch return false; - if (std.mem.indexOf(u8, addon_sources, addon) == null) return false; + if (!addonWord(x)) return false; } return true; } 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; |
