summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/abi.zig11
-rw-r--r--src/host/Host.zig24
-rw-r--r--src/sketch/web.zig2
3 files changed, 30 insertions, 7 deletions
diff --git a/src/abi.zig b/src/abi.zig
index aecb654..3991a35 100644
--- a/src/abi.zig
+++ b/src/abi.zig
@@ -647,8 +647,8 @@ pub const Host = struct {
samples: *const fn (*anyopaque, Audio) []const f32,
/// The whole file, mapped for the life of the host; empty if missing.
readFile: *const fn (*anyopaque, []const u8) []const u8,
- /// See `Host.appendFile`: name, then bytes.
- appendFile: *const fn (*anyopaque, [*]const u8, usize, [*]const u8, usize) callconv(.c) [*:0]const u8,
+ /// See `Host.appendFile`: name, then bytes; `replace` for `writeFile`.
+ appendFile: *const fn (*anyopaque, [*]const u8, usize, [*]const u8, usize, bool) callconv(.c) [*:0]const u8,
env:*const fn (*anyopaque, []const u8) ?[]const u8,
/// A monotonic clock in nanoseconds, for measuring, not for state:
/// `update` must use `Input` time to stay replayable.
@@ -730,7 +730,12 @@ pub const Host = struct {
/// replayed, like sound. `readFile` maps a file once: what is appended
/// to it afterwards is not in what it returned.
pub fn appendFile(h: Host, name: []const u8, bytes: []const u8) []const u8 {
- return std.mem.span(h.vtable.appendFile(h.ctx, name.ptr, name.len, bytes.ptr, bytes.len));
+ return std.mem.span(h.vtable.appendFile(h.ctx, name.ptr, name.len, bytes.ptr, bytes.len, false));
+ }
+ /// `appendFile` to a new, empty file of that name: what the old one
+ /// held is gone (a `readFile` of it keeps its bytes).
+ pub fn writeFile(h: Host, name: []const u8, bytes: []const u8) []const u8 {
+ return std.mem.span(h.vtable.appendFile(h.ctx, name.ptr, name.len, bytes.ptr, bytes.len, true));
}
pub fn env(h: Host, name: []const u8) ?[]const u8 {
return h.vtable.env(h.ctx, name);
diff --git a/src/host/Host.zig b/src/host/Host.zig
index 75b49b3..3a2a25f 100644
--- a/src/host/Host.zig
+++ b/src/host/Host.zig
@@ -106,6 +106,8 @@ commands: std.ArrayList(abi.Command) = .empty,
pending: std.ArrayList(abi.Event) = .empty,
input: abi.Input = .{},
files: std.StringHashMapUnmanaged([]align(memory.page) const u8) = .empty,
+/// Mappings of files `writeFile` replaced: the sketch may still hold them.
+retired: std.ArrayList([]align(memory.page) const u8) = .empty,
/// The paths `appendFile` wrote, each once, 0-terminated: the sketch keeps
/// them.
written: std.StringHashMapUnmanaged(void) = .empty,
@@ -254,6 +256,8 @@ pub fn deinit(self: *Host) void {
std.posix.munmap(entry.value_ptr.*);
}
self.files.deinit(self.gpa);
+ for (self.retired.items) |bytes| std.posix.munmap(bytes);
+ self.retired.deinit(self.gpa);
var written = self.written.keyIterator();
while (written.next()) |path| self.gpa.free(path.ptr[0 .. path.len + 1]);
self.written.deinit(self.gpa);
@@ -1081,7 +1085,7 @@ fn apiReadFile(ctx: *anyopaque, path: []const u8) []const u8 {
self.files.put(self.gpa, key, bytes) catch self.gpa.free(key);
return bytes;
}
-fn apiAppendFile(ctx: *anyopaque, name_ptr: [*]const u8, name_len: usize, bytes: [*]const u8, len: usize) callconv(.c) [*:0]const u8 {
+fn apiAppendFile(ctx: *anyopaque, name_ptr: [*]const u8, name_len: usize, bytes: [*]const u8, len: usize, replace: bool) callconv(.c) [*:0]const u8 {
const self = hostOf(ctx);
const name = name_ptr[0..name_len];
if (name.len == 0 or std.mem.indexOfScalar(u8, name, '/') != null) {
@@ -1090,6 +1094,14 @@ fn apiAppendFile(ctx: *anyopaque, name_ptr: [*]const u8, name_len: usize, bytes:
}
var buf: [std.fs.max_path_bytes]u8 = undefined;
const path = std.mem.print(&buf, "{s}.{s}", .{ self.options.state, name }) catch return "";
+ if (replace) {
+ // A new file: a mapping of the old one stays valid.
+ std.Io.Dir.cwd().deleteFile(self.io, path) catch {};
+ if (self.files.fetchRemove(path)) |old| {
+ self.gpa.free(old.key);
+ self.retired.append(self.gpa, old.value) catch {};
+ }
+ }
const file = std.Io.Dir.cwd().createFile(self.io, path, .{ .truncate = false }) catch |err| {
self.print("appendFile: {s}: {t}", .{ path, err });
return "";
@@ -1319,6 +1331,7 @@ test "appendFile appends to a file beside the state and names it for readFile" {
host.gpa = std.testing.allocator;
host.io = io;
host.written = .empty;
+ host.files = .empty;
host.options = .{ .state = try std.mem.print(&buf, ".zig-cache/tmp/{s}/t.state", .{&tmp.sub_path}) };
defer {
var written = host.written.keyIterator();
@@ -1326,10 +1339,15 @@ test "appendFile appends to a file beside the state and names it for readFile" {
host.written.deinit(host.gpa);
}
const name = "take-1.f32";
- const path = std.mem.span(apiAppendFile(&host, name, name.len, "ab", 2));
+ const path = std.mem.span(apiAppendFile(&host, name, name.len, "ab", 2, false));
try std.testing.expect(std.mem.endsWith(u8, path, "/t.state.take-1.f32"));
- try std.testing.expectEqual(path.ptr, apiAppendFile(&host, name, name.len, "cd", 2));
+ try std.testing.expectEqual(path.ptr, apiAppendFile(&host, name, name.len, "cd", 2, false));
const bytes = try tmp.dir.readFileAlloc(io, "t.state.take-1.f32", host.gpa, .unlimited);
defer host.gpa.free(bytes);
try std.testing.expectEqualStrings("abcd", bytes);
+ // writeFile: a new file, its old bytes gone.
+ _ = apiAppendFile(&host, name, name.len, "e", 1, true);
+ const again = try tmp.dir.readFileAlloc(io, "t.state.take-1.f32", host.gpa, .unlimited);
+ defer host.gpa.free(again);
+ try std.testing.expectEqualStrings("e", again);
}
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index 9efad0f..49b9d42 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -475,7 +475,7 @@ fn Driver(comptime App: type) type {
return &.{};
}
/// No files on the web: nothing is written, and the path is "".
- fn appendFile(_: *anyopaque, _: [*]const u8, _: usize, _: [*]const u8, _: usize) callconv(.c) [*:0]const u8 {
+ fn appendFile(_: *anyopaque, _: [*]const u8, _: usize, _: [*]const u8, _: usize, _: bool) callconv(.c) [*:0]const u8 {
return "";
}
fn env(_: *anyopaque, _: []const u8) ?[]const u8 {