summaryrefslogtreecommitdiff
path: root/src/host/library.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/library.zig')
-rw-r--r--src/host/library.zig83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/host/library.zig b/src/host/library.zig
new file mode 100644
index 0000000..6c9338f
--- /dev/null
+++ b/src/host/library.zig
@@ -0,0 +1,83 @@
+//! Loading sketch libraries, and noticing when a build replaces one.
+//!
+//! `dlopen` returns the handle it already has for a path it loaded before, so
+//! every load goes through a private copy with a fresh name. The copy is
+//! deleted right after loading: the mapping keeps the code alive, and nothing
+//! is left behind if the host dies.
+const std = @import("std");
+const linux = std.os.linux;
+const c = @import("c");
+const abi = @import("abi");
+
+pub const Library = struct {
+ object: *c.SDL_SharedObject,
+ sketch: abi.Sketch,
+
+ pub fn open(path: [:0]const u8) !Library {
+ const object = c.SDL_LoadObject(path.ptr) orelse return error.LoadLibrary;
+ errdefer c.SDL_UnloadObject(object);
+ var sketch: abi.Sketch = undefined;
+ inline for (@typeInfo(abi.Sketch).@"struct".fields) |field| {
+ const symbol = @field(abi.Sketch.symbols, field.name);
+ const address = c.SDL_LoadFunction(object, symbol) orelse return error.MissingEntryPoint;
+ @field(sketch, field.name) = @ptrCast(address);
+ }
+ if (sketch.info().abi != abi.hash) return error.AbiMismatch;
+ return .{ .object = object, .sketch = sketch };
+ }
+
+ /// Loads `source` through a uniquely named copy in `scratch_dir`.
+ pub fn openCopy(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library {
+ var buf: [std.fs.max_path_bytes]u8 = undefined;
+ const copy = try std.fmt.bufPrintZ(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial });
+ const cwd = std.Io.Dir.cwd();
+ try cwd.copyFile(source, cwd, copy, io, .{});
+ defer cwd.deleteFile(io, copy) catch {};
+ return open(copy);
+ }
+
+ pub fn close(self: *Library) void {
+ c.SDL_UnloadObject(self.object);
+ }
+};
+
+/// inotify on a library's directory, filtered to its file name. Builds
+/// replace files by renaming (`MOVED_TO`) or rewrite them (`CLOSE_WRITE`).
+pub const Watch = struct {
+ fd: i32,
+ name: []const u8,
+
+ pub fn init(path: []const u8) !Watch {
+ const fd: i32 = @intCast(@as(isize, @bitCast(linux.inotify_init1(linux.IN.NONBLOCK | linux.IN.CLOEXEC))));
+ if (fd < 0) return error.WatchFailed;
+ errdefer _ = linux.close(fd);
+ var buf: [std.fs.max_path_bytes]u8 = undefined;
+ const dir = std.fs.path.dirname(path) orelse ".";
+ const dir_z = try std.fmt.bufPrintZ(&buf, "{s}", .{dir});
+ if (@as(isize, @bitCast(linux.inotify_add_watch(fd, dir_z, linux.IN.MOVED_TO | linux.IN.CLOSE_WRITE))) < 0) return error.WatchFailed;
+ return .{ .fd = fd, .name = std.fs.path.basename(path) };
+ }
+
+ pub fn deinit(self: Watch) void {
+ _ = linux.close(self.fd);
+ }
+
+ /// Drains pending events; true if any named the library.
+ pub fn changed(self: Watch) bool {
+ var found = false;
+ var buf: [4096]u8 align(@alignOf(linux.inotify_event)) = undefined;
+ while (true) {
+ const rc = linux.read(self.fd, &buf, buf.len);
+ const len: isize = @bitCast(rc);
+ if (len <= 0) return found;
+ var off: usize = 0;
+ while (off + @sizeOf(linux.inotify_event) <= @as(usize, @intCast(len))) {
+ const event: *const linux.inotify_event = @ptrCast(@alignCast(&buf[off]));
+ off += @sizeOf(linux.inotify_event) + event.len;
+ if (event.getName()) |name| {
+ if (std.mem.eql(u8, name, self.name)) found = true;
+ }
+ }
+ }
+ }
+};