//! Comptime reflection of a sketch's memory into `abi.Layout`. //! //! Structs are flattened into dotted leaf paths; everything else (arrays, //! optionals, unions) is one leaf. Each leaf carries a structural hash of its //! type, so a host can tell, across a reload, which bytes still mean the same //! thing. //! //! What each kind of sketch memory may hold: //! - State is saved to disk and snapshotted: no pointers, and no host //! handles (images, sounds, buffers), which only mean something in the //! process that made them. //! - Session lives in this process: anything, as long as pointers go into //! memory the host owns (its heaps, mapped files), never into the sketch //! library, which a reload unmaps. //! - Store is a file mapped at the same address in every run: pointers into //! the store itself, but no host handles. const std = @import("std"); const abi = @import("abi.zig"); pub fn of(comptime T: type, comptime memory: abi.Memory) abi.Layout { return comptime blk: { @setEvalBranchQuota(1_000_000); switch (memory) { .state => check(T, "State", @typeName(T), false), .store => check(T, "Store", @typeName(T), true), .session => {}, } const Leaves = struct { const list = flatten(T, "", 0); const fields: [list.len]abi.Field = list[0..list.len].*; }; break :blk .{ .size = @sizeOf(T), .alignment = @alignOf(T), .hash = hash(T), .fields = &Leaves.fields, }; }; } fn flatten(comptime T: type, comptime prefix: []const u8, comptime base: usize) []const abi.Field { comptime { if (@typeInfo(T) != .@"struct") return &.{leaf(T, if (prefix.len == 0) "value" else prefix, base)}; var leaves: []const abi.Field = &.{}; const s = @typeInfo(T).@"struct"; for (s.field_names, s.field_types, s.field_attrs) |name, F, attrs| { if (attrs.@"comptime" or @sizeOf(F) == 0) continue; const path = if (prefix.len == 0) name else prefix ++ "." ++ name; const offset = base + @offsetOf(T, name); leaves = leaves ++ if (@typeInfo(F) == .@"struct" and @typeInfo(F).@"struct".backing_integer == null) flatten(F, path, offset) else &[_]abi.Field{leaf(F, path, offset)}; } return leaves; } } fn leaf(comptime T: type, comptime path: []const u8, comptime offset: usize) abi.Field { comptime { const kind: abi.Field.Kind = switch (@typeInfo(T)) { .int => |i| if (i.signedness == .signed) .int else .uint, .float => .float, .bool => .bool, .@"enum" => .@"enum", .array => .array, else => .bytes, }; return .{ .path = std.fmt.comptimePrint("{s}", .{path}), .offset = offset, .size = @sizeOf(T), .kind = kind, .hash = hash(T), .tags = if (kind == .@"enum") tags(T) else "", }; } } fn check(comptime T: type, comptime memory: []const u8, comptime path: []const u8, comptime pointers: bool) void { comptime { if (T == abi.Image or T == abi.Audio or T == abi.Buffer) @compileError(memory ++ " cannot hold host handles (" ++ path ++ "): they mean nothing to the next process. Keep them in Session."); switch (@typeInfo(T)) { // Not followed: types can point to themselves. .pointer => if (!pointers) @compileError(memory ++ " cannot hold pointers (" ++ path ++ "): it is saved to disk and outlives every address. Keep indices or offsets, or put it in Store or Session."), .array => |a| check(a.child, memory, path ++ "[]", pointers), .vector => |v| check(v.child, memory, path ++ "[]", pointers), .optional => |o| check(o.child, memory, path, pointers), .@"struct" => |s| for (s.field_names, s.field_types) |name, F| check(F, memory, path ++ "." ++ name, pointers), .@"union" => |u| for (u.field_names, u.field_types) |name, F| check(F, memory, path ++ "." ++ name, pointers), else => {}, } } } fn tags(comptime E: type) [:0]const u8 { comptime { var text: []const u8 = ""; const e = @typeInfo(E).@"enum"; for (e.field_names, e.field_values, 0..) |name, value, i| { text = text ++ (if (i == 0) "" else ",") ++ std.fmt.comptimePrint("{s}={d}", .{ name, value }); } return std.fmt.comptimePrint("{s}", .{text}); } } /// Equal hashes mean the bytes can be copied and keep their meaning. Enum /// leaves also carry their tags, so a host can remap them by name. pub fn hash(comptime T: type) u64 { return comptime blk: { @setEvalBranchQuota(1_000_000); break :blk std.hash.Wyhash.hash(0, signature(T)); }; } pub fn signature(comptime T: type) []const u8 { comptime { return switch (@typeInfo(T)) { .int => |i| std.fmt.comptimePrint("{c}{d}", .{ if (i.signedness == .signed) 'i' else 'u', i.bits }), .float => |f| std.fmt.comptimePrint("f{d}", .{f.bits}), .bool => "b", .void => "v", .@"enum" => |e| "e(" ++ signature(e.tag_type) ++ ":" ++ tags(T) ++ ")", .array => |a| std.fmt.comptimePrint("[{d}]", .{a.len}) ++ signature(a.child), .vector => |v| std.fmt.comptimePrint("@{d}", .{v.len}) ++ signature(v.child), .optional => |o| "?" ++ signature(o.child), .@"struct" => |s| blk: { var text: []const u8 = std.fmt.comptimePrint("s{d}{{", .{@sizeOf(T)}); for (s.field_names, s.field_types, s.field_attrs) |name, F, attrs| { if (attrs.@"comptime") continue; text = text ++ std.fmt.comptimePrint("{s}@{d}:", .{ name, @offsetOf(T, name) }) ++ signature(F) ++ ";"; } break :blk text ++ "}"; }, .@"union" => |u| blk: { var text: []const u8 = std.fmt.comptimePrint("u{d}{{", .{@sizeOf(T)}); if (u.tag_type) |tag| text = text ++ signature(tag) ++ "|"; for (u.field_names, u.field_types) |name, F| text = text ++ name ++ ":" ++ signature(F) ++ ";"; break :blk text ++ "}"; }, .pointer => |p| std.fmt.comptimePrint("*{s}:{s}", .{ @tagName(p.size), @typeName(p.child) }), .@"opaque" => "opaque", .@"fn" => @typeName(T), else => @compileError("unsupported type in sketch memory: " ++ @typeName(T)), }; } } test "leaves flatten structs, keep arrays whole and reject nothing pointer-free" { const Inner = extern struct { a: u8 = 0, b: enum(u8) { x, y = 5 } = .x }; const S = extern struct { inner: Inner = .{}, list: [4]f32 = @splat(0), flag: bool = false }; const l = of(S, .state); const fields = l.fields; try std.testing.expectEqual(4, fields.len); try std.testing.expectEqualStrings("inner.a", std.mem.span(fields[0].path)); try std.testing.expectEqualStrings("inner.b", std.mem.span(fields[1].path)); try std.testing.expectEqualStrings("x=0,y=5", std.mem.span(fields[1].tags)); try std.testing.expectEqual(abi.Field.Kind.array, fields[2].kind); try std.testing.expectEqual(@as(u32, 4), fields[2].offset); try std.testing.expect(hash([4]f32) != hash([5]f32)); try std.testing.expect(hash(enum(u8) { a }) != hash(enum(u8) { a, b })); }