diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/FrameStats.zig | 2 | ||||
| -rw-r--r-- | src/Store.zig | 2 | ||||
| -rw-r--r-- | src/Stream.zig | 2 | ||||
| -rw-r--r-- | src/dump.zig | 23 | ||||
| -rw-r--r-- | src/exec.zig | 2 | ||||
| -rw-r--r-- | src/image.zig | 22 | ||||
| -rw-r--r-- | src/tape.zig | 4 | ||||
| -rw-r--r-- | src/terminal.zig | 4 | ||||
| -rw-r--r-- | src/walk.zig | 38 |
9 files changed, 71 insertions, 28 deletions
diff --git a/src/FrameStats.zig b/src/FrameStats.zig index 2287002e..c27e74af 100644 --- a/src/FrameStats.zig +++ b/src/FrameStats.zig @@ -173,7 +173,7 @@ pub fn record(stats: *FrameStats, end_ns: u64, moving: bool, skipped: bool) void if (stats.armed and @as(u64, gap) * std.time.ns_per_us * 2 > stats.period_ns * 3) stats.jank += 1; if (@as(u64, frame.update_us + frame.render_us + frame.present_us) * std.time.ns_per_us > stats.period_ns) stats.over_budget += 1; if (skipped) stats.skipped += 1; - stats.ring[stats.count % capacity] = frame; + stats.ring[@intCast(stats.count % capacity)] = frame; stats.count += 1; stats.last_end_ns = end_ns; stats.armed = moving; diff --git a/src/Store.zig b/src/Store.zig index 95f5368f..36215234 100644 --- a/src/Store.zig +++ b/src/Store.zig @@ -78,7 +78,7 @@ cap: u64 = max_bytes, session: u64 = 0, pid: i32 = 0, /// The file as read so far: its inode, generation and how far. -inode: std.Io.File.INode = 0, +inode: std.Io.File.INode = std.mem.zeroes(std.Io.File.INode), gen: u64 = 0, end: u64 = 0, /// Its length when first read whole: a file past twice that (and 1 MiB) diff --git a/src/Stream.zig b/src/Stream.zig index 8d9663dd..665241df 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -512,7 +512,7 @@ pub fn parseFor(p: *Pardes, id: usize) void { // Here and now: a small text, a host with no workers. var got: Collect = .{ .gpa = p.gpa }; defer got.batches.deinit(p.gpa); - parseWork(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); + parseWork(if (comptime pardes.hosted) std.Io.Threaded.global_single_threaded.io() else std.Io.failing, job, &got, Collect.take); for (got.batches.items) |*b| { receive(p, b); b.deinit(p.gpa); diff --git a/src/dump.zig b/src/dump.zig index abc52f5a..c33ac02c 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -142,11 +142,13 @@ pub fn defaultDirectory(buf: []u8) ?[]const u8 { /// A session's dump: its core, everything it is but what is the host's, /// the frame's or worked out again (`replica` in each type, src/walk.zig), -/// then its 9P mounts, which live with the 9P server's own state, and which -/// of its panes' files were on disk when it was made (one gone by its load -/// is said deleted: its text is nowhere else now). -const Session = walk.Dump(&.{ Pardes, []const filesystem.Mount, OnDisk }); -const SessionLoad = walk.Load(&.{ Pardes, []const filesystem.Mount, OnDisk }); +/// then its 9P mounts, which live with the 9P server's own state, which of +/// its panes' files were on disk when it was made (one gone by its load is +/// said deleted: its text is nowhere else now), and its theme's name: the +/// setting is a place in this build's ring of themes, another build's +/// ring is another. +const Session = walk.Dump(&.{ Pardes, []const filesystem.Mount, OnDisk, []const u8 }); +const SessionLoad = walk.Load(&.{ Pardes, []const filesystem.Mount, OnDisk, []const u8 }); const OnDisk = [MAX_PANES]bool; fn onDisk(pane: *const pardes.panes.Pane) bool { @@ -176,6 +178,8 @@ pub fn dumpBytes(p: *Pardes) ![]u8 { on.* = onDisk(pane); }; try d.put(OnDisk, &on_disk); + const theme: []const u8 = colors.themes[@min(p.settings.theme, colors.themes.len - 1)].name; + try d.put([]const u8, &theme); return d.finish(); } @@ -233,6 +237,12 @@ fn loadKnowing(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: } const was = try l.get(OnDisk); if (on_disk) |out| out.* = was; + const theme = try l.get([]const u8); + defer gpa.free(theme); + // This build's place for that theme; one it has not, its first. + p.settings.theme = for (colors.themes, 0..) |t, i| { + if (std.mem.eql(u8, t.name, theme)) break i; + } else if (p.settings.theme < colors.themes.len) p.settings.theme else 0; // A mount this process was started with stands; one of the same name // to elsewhere is refused, never silently put over it. for (mounts) |m| { @@ -411,7 +421,8 @@ fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool // Its file gone from disk since the dump: its text is nowhere else, so // it is said deleted, and Del, Exit and Restore ask first. One never on // disk (a name not yet saved) comes back as it was. - if (was_on_disk and !onDisk(pane)) pardes.panes.File.deleted(p, @intCast(id)); + // (A host with no disk of its own, the web's, asks nothing.) + if (comptime pardes.hosted) if (was_on_disk and !onDisk(pane)) pardes.panes.File.deleted(p, @intCast(id)); } // ---- one pane to and from a dump: the store's, a handover's ---- diff --git a/src/exec.zig b/src/exec.zig index 8f84d2c6..df397b37 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -483,6 +483,8 @@ pub fn isFile(path: []const u8) bool { /// What `path` is on this host, null when it is not there; the turn out /// while it asks (it may be inside a mount this editor serves). pub fn kindOf(path: []const u8) ?std.Io.File.Kind { + // A freestanding build has no host to ask. + if (comptime !pardes.hosted) return null; var buf: [4096]u8 = undefined; const z = std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0) catch return null; return pardes.tape.site(.kind, null, kindHost, .{z}); diff --git a/src/image.zig b/src/image.zig index 8f193674..9163730d 100644 --- a/src/image.zig +++ b/src/image.zig @@ -755,6 +755,13 @@ var started = false; extern fn stbi_info_from_memory(buffer: [*]const u8, len: c_int, x: *c_int, y: *c_int, comp: *c_int) c_int; +/// stb_image's header reader; a freestanding build has no stb_image (its +/// decoder is the shim in src/web/zstbi.zig), so no header reads as one. +fn stbiInfo(bytes: []const u8, w: *c_int, h: *c_int, c: *c_int) bool { + if (comptime !pardes.hosted) return false; + return stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), w, h, c) == 1; +} + /// PNG and JPEG are ghostty's decoders (wuffs, which ghostty-vt links for /// kitty graphics) wherever ghostty-vt is in the build; stb_image is then /// compiled without them, and without the inflate only its PNG used @@ -787,7 +794,7 @@ fn headerSize(bytes: []const u8) ?Size { var iw: c_int = 0; var ih: c_int = 0; var ic: c_int = 0; - if (stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &iw, &ih, &ic) != 1) return null; + if (!stbiInfo(bytes, &iw, &ih, &ic)) return null; return .{ .w = @intCast(@max(0, iw)), .h = @intCast(@max(0, ih)) }; } @@ -828,7 +835,7 @@ pub fn readable(bytes: []const u8) bool { var c: c_int = 0; if (!started) return true; // ponytail: no stb_image yet (a unit test), so no judging if (comptime wuffs_images) if (format(bytes) != .other) return headerSize(bytes) != null; - return stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &w, &h, &c) == 1; + return stbiInfo(bytes, &w, &h, &c); } /// decode + downscale to RGBA, gpa-owned. Returns null on any failure — the @@ -929,6 +936,9 @@ const GridKey = struct { ascii: bool = true, }; +/// A host's thread; a freestanding build has none to have. +const Decoder = if (pardes.hosted) std.Thread else void; + pub const State = struct { /// What the replay digest leaves out (src/walk.zig): the picture as /// decoded from its bytes, off the loop as the render first wants it, @@ -947,12 +957,12 @@ pub const State = struct { grid: GlyphArt.Grid = .{ .cells = &.{}, .cols = 0, .rows = 0 }, grid_key: GridKey = .{}, /// The decode under way off the loop (ensureDecoded), and its answer. - decoder: ?std.Thread = null, + decoder: ?Decoder = null, decoded: std.atomic.Value(bool) = .init(false), answer: ?Decoded = null, pub fn deinit(state: *State, gpa: std.mem.Allocator) void { - if (state.decoder) |t| t.join(); + if (comptime pardes.hosted) if (state.decoder) |t| t.join(); if (state.answer) |a| gpa.free(a.rgba); gpa.free(state.path); if (state.rgba.len > 0) gpa.free(state.rgba); @@ -1026,11 +1036,11 @@ pub fn tagPrefix(arena: std.mem.Allocator, state: *const State, glyphs: bool) ![ /// docs' screenshot: all of it once on the frame before). /// ponytail: a thread a picture; a pool if many open at once. fn ensureDecoded(p: *pardes.Pardes, state: *State) void { - if (state.decoder) |t| { + if (comptime pardes.hosted) if (state.decoder) |t| { if (!state.decoded.load(.acquire)) return; t.join(); state.decoder = null; - } + }; if (state.answer) |decoded| { state.answer = null; state.rgba = decoded.rgba; diff --git a/src/tape.zig b/src/tape.zig index d4fb7756..84bdce29 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -163,6 +163,7 @@ pub const Tape = struct { } fn flush(t: *Tape) void { + if (comptime !pardes.hosted) return; // A tape in memory (a replica's, an authority's stream) is taken // by whoever holds it. if (t.fd < 0) return; @@ -710,6 +711,8 @@ pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@Ty fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? { const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?; + // No tape where there is no host to record or replay one. + if (comptime !pardes.hosted) return if (yields) out(f, args) else @call(.auto, f, args); const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); const id: u16 = @as(u16, host_fns.len) + @backingInt(s); if (t.replay) { @@ -928,6 +931,7 @@ pub fn digestOf(p: *const Pardes) u64 { /// PARDES_RECORD set: the tape is written from the session's first call. pub fn startFromEnv() void { + if (comptime !pardes.hosted) return; if (on != null) return; const prefix = std.mem.span(libc.getenv("PARDES_RECORD") orelse return); if (prefix.len == 0) return; diff --git a/src/terminal.zig b/src/terminal.zig index ad35784f..bcb70110 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -254,7 +254,7 @@ pub const State = struct { return; } if (bytes.len < 8) return error.EndOfDump; - const n = std.mem.bytesToValue(u64, bytes[0..8]); + const n = std.math.cast(usize, std.mem.bytesToValue(u64, bytes[0..8])) orelse return error.EndOfDump; if (bytes.len - 8 < n) return error.EndOfDump; var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); @@ -797,7 +797,7 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { // defaults to direct alone, which `kitten icat` and timg detect and // fall back from, base64 through the pty. What the program can read // it could send itself: it runs as the user, under pardes. - .kitty_image_loading_limits = if (images_enabled) .allWithTempDir(tempDir()) else .direct, + .kitty_image_loading_limits = if (images_enabled) .allWithTempDir(tempDir()) else if (@FieldType(ghostty_vt.Terminal.Options, "kitty_image_loading_limits") == void) {} else .direct, }); state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa }); state.reply_len = 0; diff --git a/src/walk.zig b/src/walk.zig index 328557a7..6f9cc34b 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -843,6 +843,10 @@ fn allocOf(comptime T: type, comptime name: []const u8) ?[]const u8 { return null; } +fn countOf(n: u64) DecodeError!usize { + return std.math.cast(usize, n) orelse error.BadDump; +} + /// An allocation a load made: what a reference to it finds, and what a /// failed load frees. const Made = struct { bytes: []u8, alignment: std.mem.Alignment, gpa: std.mem.Allocator, dead: bool = false }; @@ -931,6 +935,11 @@ pub fn Load(comptime roots: []const type) type { return std.mem.bytesToValue(T, try l.take(@sizeOf(T))); } + /// A count, as this machine counts (a wasm32 one has 32 bits). + fn count(l: *Self) DecodeError!usize { + return countOf(try l.scalar(u64)); + } + fn name(l: *Self) DecodeError![]const u8 { return l.take(try l.scalar(u16)); } @@ -962,9 +971,9 @@ pub fn Load(comptime roots: []const type) type { }, .optional, .one, .slice, .prefix, .error_union => t.child = try l.scalar(u32), .@"struct", .@"union" => { - const count = try l.scalar(u32); - if (count > l.bytes.len) return error.BadDump; - const list = try a.alloc(SrcField, count); + const nfields = try l.scalar(u32); + if (nfields > l.bytes.len) return error.BadDump; + const list = try a.alloc(SrcField, nfields); for (list) |*f| f.* = .{ .name = try l.name(), .ty = try l.scalar(u32) }; t.fields = list; }, @@ -1073,6 +1082,14 @@ pub fn Load(comptime roots: []const type) type { out.* = .{ .raw = raw }; return; } + // A field this build compiles out (a PDF slot where there is no + // MuPDF): passed over, said only when it held something. + if (comptime kindOf(Canon(T)) == .void) if (!l.same and l.src[ty].kind != .void) { + const held = l.src[ty].kind != .optional or (l.at < l.bytes.len and l.bytes[l.at] != 0); + try l.skip(ty); + if (held) l.note("{s}: not in this build", .{l.field}); + return; + }; if (!try l.kindMatches(T, ty)) return error.Incompatible; const s = l.src[ty]; if (comptime isList(T)) { @@ -1194,8 +1211,7 @@ pub fn Load(comptime roots: []const type) type { } else out.* = try l.errorNamed(@typeInfo(T).error_union.error_set); }, .custom => { - const n = try l.scalar(u64); - const bytes = try l.take(n); + const bytes = try l.take(try l.count()); T.dumpRead(out, l.gpa, bytes) catch |err| { if (err == error.OutOfMemory) return error.OutOfMemory; l.note("{s}: {t}", .{ l.field, err }); @@ -1204,7 +1220,7 @@ pub fn Load(comptime roots: []const type) type { if (comptime @hasDecl(T, "dumpFree")) try l.customs.append(l.own, .{ .ptr = out, .gpa = l.gpa, .free = @ptrCast(&T.dumpFree) }); }, .bitset => { - const bits = try l.scalar(u64); + const bits = try l.count(); out.* = try .initEmpty(l.gpa, bits); const words = (bits + @bitSizeOf(usize) - 1) / @bitSizeOf(usize); if (words > 0) @memcpy(std.mem.sliceAsBytes(out.masks[0..words]), try l.take(words * @sizeOf(usize))); @@ -1232,7 +1248,7 @@ pub fn Load(comptime roots: []const type) type { fn slice(l: *Self, comptime E: type, s: SrcType) DecodeError![]E { if (try l.ref()) |again| return @as([*]E, @ptrCast(@alignCast(again.ptr)))[0 .. again.len / @max(1, @sizeOf(E))]; - const n = try l.scalar(u64); + const n = try l.count(); if (n == 0) return &.{}; if (n > l.bytes.len) return error.BadDump; const many = try l.gpa.alloc(E, n); @@ -1264,7 +1280,7 @@ pub fn Load(comptime roots: []const type) type { // cannot be read is left out, not the array. const droppable = comptime @typeInfo(E) == .optional and @typeInfo(@typeInfo(E).optional.child) == .pointer; var failed = false; - for (0..n) |i| { + for (0..try countOf(n)) |i| { if (i >= keep) { try l.skip(ty); continue; @@ -1394,8 +1410,8 @@ pub fn Load(comptime roots: []const type) type { .error_union => if ((try l.take(1))[0] != 0) try l.skip(s.child) else { _ = try l.name(); }, - .custom => _ = try l.take(try l.scalar(u64)), - .bitset => _ = try l.take((try l.scalar(u64) + 63) / 64 * 8), + .custom => _ = try l.take(try l.count()), + .bitset => _ = try l.take((try l.count() + 63) / 64 * 8), } } @@ -1407,7 +1423,7 @@ pub fn Load(comptime roots: []const type) type { _ = try l.take(@intCast(n * size)); return; } - for (0..n) |_| try l.skip(ty); + for (0..try countOf(n)) |_| try l.skip(ty); } }; } |
