From 2348776791e9dd17afeb12a06ba42a4a71917bcf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 21:34:41 -0300 Subject: sketchbook: a drawing layer on top, host fixes for replay and migration --- src/host/fields.zig | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 1 deletion(-) (limited to 'src/host/fields.zig') diff --git a/src/host/fields.zig b/src/host/fields.zig index ba8fc03..2e70414 100644 --- a/src/host/fields.zig +++ b/src/host/fields.zig @@ -2,7 +2,82 @@ //! Shared by the console, the memory overlay, scripts and the probe. const std = @import("std"); const abi = @import("abi"); -const Leaf = @import("memory.zig").Layout.Leaf; +const Layout = @import("memory.zig").Layout; +const Leaf = Layout.Leaf; + +/// Writes what `path` names: one value for a number, bool or enum leaf, +/// or every leaf under it for an array, a vector or a struct, in memory +/// order (for a Zig struct not always the declared one): `{ 250, 3 }` +/// when they are its elements, otherwise `{ .count = 12, .hz = 60 }` or +/// `{ [0].x = 1, [1].x = 2 }`. +/// Shows the first `max_values`, then how many more there are. +pub fn formatPath(layout: Layout, memory: []const u8, path: []const u8, w: *std.Io.Writer) (std.Io.Writer.Error || error{UnknownField})!void { + if (layout.find(path)) |leaf| if (!leaf.whole()) return format(leaf, memory, w); + // Names under `path`, found leaf by leaf; sorted by offset afterwards. + const Found = struct { offset: usize, leaf: Leaf }; + var found: [max_values]Found = undefined; + var count: usize = 0; + var total: usize = 0; + var plain = true; + var name_buf: [256]u8 = undefined; + for (layout.fields) |leaf| { + if (leaf.whole()) continue; + var it = leaf.elements(); + while (it.next()) |offset| { + var nw: std.Io.Writer = .fixed(&name_buf); + leaf.name(offset, &nw) catch continue; + const name = nw.buffered(); + if (!under(name, path)) continue; + total += 1; + if (!isIndex(name[path.len..])) plain = false; + if (count < max_values) { + found[count] = .{ .offset = offset, .leaf = leaf }; + count += 1; + } else { + // Keep the lowest offsets. + var highest: usize = 0; + for (found[1..], 1..) |f, i| if (f.offset > found[highest].offset) { + highest = i; + }; + if (offset < found[highest].offset) found[highest] = .{ .offset = offset, .leaf = leaf }; + } + } + } + if (total == 0) return error.UnknownField; + std.mem.sortUnstable(Found, found[0..count], {}, struct { + fn less(_: void, a: Found, b: Found) bool { + return a.offset < b.offset; + } + }.less); + try w.writeAll("{ "); + for (found[0..count], 0..) |f, i| { + if (i > 0) try w.writeAll(", "); + if (!plain) { + var nw: std.Io.Writer = .fixed(&name_buf); + f.leaf.name(f.offset, &nw) catch {}; + try w.print("{s} = ", .{nw.buffered()[path.len..]}); + } + try format(f.leaf.at(f.offset), memory, w); + } + if (total > count) try w.print(", ... {d} more", .{total - count}); + try w.writeAll(" }"); +} + +pub const max_values = 64; + +/// `name` is `path` itself or a field or element inside it. +fn under(name: []const u8, path: []const u8) bool { + if (!std.mem.startsWith(u8, name, path)) return false; + if (name.len == path.len) return true; + return name[path.len] == '.' or name[path.len] == '['; +} + +/// `[3]`: one index and nothing more. +fn isIndex(suffix: []const u8) bool { + if (suffix.len < 3 or suffix[0] != '[' or suffix[suffix.len - 1] != ']') return false; + for (suffix[1 .. suffix.len - 1]) |ch| if (!std.ascii.isDigit(ch)) return false; + return true; +} pub fn format(leaf: Leaf, memory: []const u8, w: *std.Io.Writer) std.Io.Writer.Error!void { const bytes = leaf.bytes(memory); @@ -64,6 +139,22 @@ pub fn matches(leaf: Leaf, memory: []const u8, text: []const u8) bool { return false; } +/// A number, bool or enum leaf's value as a float; null for other kinds. +pub fn number(leaf: Leaf, memory: []const u8) ?f64 { + const bytes = leaf.bytes(memory); + return switch (leaf.kind) { + .int, .@"enum" => @floatFromInt(readInt(i64, bytes)), + .uint => @floatFromInt(readInt(u64, bytes)), + .bool => if (bytes[0] != 0) 1 else 0, + .float => switch (leaf.size) { + 4 => @as(f32, @bitCast(bytes[0..4].*)), + 8 => @as(f64, @bitCast(bytes[0..8].*)), + else => null, + }, + else => null, + }; +} + pub fn readInt(comptime T: type, bytes: []const u8) T { const n: usize = @min(bytes.len, 8); const value = std.mem.readVarInt(u64, bytes[0..n], .little); @@ -114,3 +205,47 @@ test "fields format, parse and compare by kind" { try std.testing.expect(s.on); try std.testing.expectError(error.InvalidValue, set(layout.find("x").?, bytes, "banana")); } + +test "indexed paths reach array elements and vector lanes" { + const S = struct { pos: @Vector(2, f32) = .{ 250, 3 }, boids: struct { buffer: [4]struct { pos: [2]f32 = .{ 0, 0 }, mood: enum(u8) { calm = 1, wild = 2 } = .calm } = @splat(.{}), len: u32 = 0 } = .{} }; + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const layout = try @import("memory.zig").Layout.copy(arena.allocator(), abi.layout.of(S, .state)); + var s: S = .{}; + const bytes = std.mem.asBytes(&s); + try std.testing.expect(matches(layout.find("pos[0]").?, bytes, "250")); + try std.testing.expectEqual(@as(?f64, 3), number(layout.find("pos[1]").?, bytes)); + try set(layout.find("boids.buffer[3].pos[1]").?, bytes, "20.5"); + try std.testing.expectEqual(@as(f32, 20.5), s.boids.buffer[3].pos[1]); + try set(layout.find("boids.buffer[2].mood").?, bytes, "wild"); + try std.testing.expectEqual(.wild, s.boids.buffer[2].mood); + try std.testing.expect(layout.find("boids.buffer[4].mood") == null); + try std.testing.expect(layout.find("pos[2]") == null); +} + +test "paths to vectors, arrays and structs print every value under them" { + const S = struct { pos: @Vector(2, f32) = .{ 250, 3 }, clock: struct { hz: f32 = 60, count: u64 = 12 } = .{}, list: struct { buffer: [3]struct { x: i32 = 1, on: bool = true } = @splat(.{}), len: u32 = 2 } = .{}, big: [100]u8 = @splat(7) }; + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const layout = try Layout.copy(arena.allocator(), abi.layout.of(S, .state)); + var s: S = .{}; + s.list.buffer[1].x = -4; + const bytes = std.mem.asBytes(&s); + var buf: [1024]u8 = undefined; + const cases = [_][2][]const u8{ + .{ "pos", "{ 250, 3 }" }, + .{ "pos[1]", "3" }, + .{ "clock", "{ .count = 12, .hz = 60 }" }, // memory order + .{ "list.buffer[1]", "{ .x = -4, .on = true }" }, + .{ "list.buffer", "{ [0].x = 1, [0].on = true, [1].x = -4, [1].on = true, [2].x = 1, [2].on = true }" }, + }; + for (cases) |case| { + var w: std.Io.Writer = .fixed(&buf); + try formatPath(layout, bytes, case[0], &w); + try std.testing.expectEqualStrings(case[1], w.buffered()); + } + var w: std.Io.Writer = .fixed(&buf); + try formatPath(layout, bytes, "big", &w); + try std.testing.expect(std.mem.endsWith(u8, w.buffered(), "7, 7, ... 36 more }")); + try std.testing.expectError(error.UnknownField, formatPath(layout, bytes, "po", &w)); +} -- cgit v1.3