summaryrefslogtreecommitdiff
path: root/src/layout.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-28 21:07:13 -0300
committerGabriel Schneider <[email protected]>2026-11-12 13:22:02 -0300
commit590ba7b9b579b97e34b51ab7e3460ebc85713343 (patch)
tree6a73a7570ff4f79f41deff078455877b9bc6571b /src/layout.zig
parentbc8e6eb1bfef0c5e9b3c40fc1e4fd5a1659e2b03 (diff)
downloadsketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.tar.gz
sketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.zip
sketchbook: Zig 0.17, a Store on disk, requests, a scope (waveform, spectrum, spectrogram), audio codecs and fonts, web host
Diffstat (limited to 'src/layout.zig')
-rw-r--r--src/layout.zig76
1 files changed, 45 insertions, 31 deletions
diff --git a/src/layout.zig b/src/layout.zig
index e779260..5c97ca1 100644
--- a/src/layout.zig
+++ b/src/layout.zig
@@ -5,18 +5,26 @@
//! type, so a host can tell, across a reload, which bytes still mean the same
//! thing.
//!
-//! State is saved to disk and snapshotted, so it may not hold pointers at all.
-//! Session memory lives only in the host process and may point into memory
-//! the host owns (its heap, mapped files), never into the sketch library.
+//! 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 const Pointers = enum { forbidden, host_only };
-
-pub fn of(comptime T: type, comptime pointers: Pointers) abi.Layout {
+pub fn of(comptime T: type, comptime memory: abi.Memory) abi.Layout {
return comptime blk: {
@setEvalBranchQuota(1_000_000);
- if (pointers == .forbidden) noPointers(T, @typeName(T));
+ 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].*;
@@ -34,14 +42,15 @@ fn flatten(comptime T: type, comptime prefix: []const u8, comptime base: usize)
comptime {
if (@typeInfo(T) != .@"struct") return &.{leaf(T, if (prefix.len == 0) "value" else prefix, base)};
var leaves: []const abi.Field = &.{};
- for (@typeInfo(T).@"struct".fields) |field| {
- if (field.is_comptime or @sizeOf(field.type) == 0) continue;
- const path = if (prefix.len == 0) field.name else prefix ++ "." ++ field.name;
- const offset = base + @offsetOf(T, field.name);
- leaves = leaves ++ if (@typeInfo(field.type) == .@"struct" and @typeInfo(field.type).@"struct".backing_integer == null)
- flatten(field.type, path, offset)
+ 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(field.type, path, offset)};
+ &[_]abi.Field{leaf(F, path, offset)};
}
return leaves;
}
@@ -68,23 +77,28 @@ fn leaf(comptime T: type, comptime path: []const u8, comptime offset: usize) abi
}
}
-fn noPointers(comptime T: type, comptime path: []const u8) void {
- comptime switch (@typeInfo(T)) {
- .pointer => @compileError("State cannot hold pointers (" ++ path ++ "): it is saved to disk and outlives every address. Store handles, indices or offsets, or keep it in Session."),
- .array => |a| noPointers(a.child, path ++ "[]"),
- .vector => |v| noPointers(v.child, path ++ "[]"),
- .optional => |o| noPointers(o.child, path),
- .@"struct" => |s| for (s.fields) |f| noPointers(f.type, path ++ "." ++ f.name),
- .@"union" => |u| for (u.fields) |f| noPointers(f.type, path ++ "." ++ f.name),
- 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 = "";
- for (@typeInfo(E).@"enum".fields, 0..) |field, i| {
- text = text ++ (if (i == 0) "" else ",") ++ std.fmt.comptimePrint("{s}={d}", .{ field.name, field.value });
+ 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});
}
@@ -112,16 +126,16 @@ pub fn signature(comptime T: type) []const u8 {
.optional => |o| "?" ++ signature(o.child),
.@"struct" => |s| blk: {
var text: []const u8 = std.fmt.comptimePrint("s{d}{{", .{@sizeOf(T)});
- for (s.fields) |field| {
- if (field.is_comptime) continue;
- text = text ++ std.fmt.comptimePrint("{s}@{d}:", .{ field.name, @offsetOf(T, field.name) }) ++ signature(field.type) ++ ";";
+ 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.fields) |field| text = text ++ field.name ++ ":" ++ signature(field.type) ++ ";";
+ 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) }),
@@ -135,7 +149,7 @@ pub fn signature(comptime T: type) []const u8 {
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, .forbidden);
+ 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));