|
| 1 | +//! Allocator-backed bytes whose ownership can be transferred to JavaScript. |
| 2 | +//! Like other Zig owning values, an OwnedBuffer must not be copied or deinitialized after transfer. |
| 3 | + |
| 4 | +const std = @import("std"); |
| 5 | +const c = @import("c.zig").c; |
| 6 | +const Env = @import("Env.zig"); |
| 7 | +const Value = @import("Value.zig"); |
| 8 | + |
| 9 | +allocator: std.mem.Allocator, |
| 10 | +data: []u8, |
| 11 | + |
| 12 | +const OwnedBuffer = @This(); |
| 13 | + |
| 14 | +const FinalizerContext = struct { |
| 15 | + allocator: std.mem.Allocator, |
| 16 | + data: []u8, |
| 17 | +}; |
| 18 | + |
| 19 | +/// Takes ownership of `data`, which must have been allocated by `allocator`. |
| 20 | +/// The allocator must remain valid until the buffer is deinitialized or finalized by JavaScript. |
| 21 | +pub fn fromOwnedSlice(allocator: std.mem.Allocator, data: []u8) OwnedBuffer { |
| 22 | + return .{ |
| 23 | + .allocator = allocator, |
| 24 | + .data = data, |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +/// Copies `data` into a new owned allocation. |
| 29 | +pub fn fromSlice(allocator: std.mem.Allocator, data: []const u8) !OwnedBuffer { |
| 30 | + return .fromOwnedSlice(allocator, try allocator.dupe(u8, data)); |
| 31 | +} |
| 32 | + |
| 33 | +/// Releases a buffer that has not been transferred to JavaScript. |
| 34 | +pub fn deinit(self: *OwnedBuffer) void { |
| 35 | + self.allocator.free(self.data); |
| 36 | + self.* = undefined; |
| 37 | +} |
| 38 | + |
| 39 | +/// Transfers ownership to a JavaScript Buffer. |
| 40 | +/// This consumes the buffer even on failure; the caller must not deinitialize it afterwards. |
| 41 | +/// On success, JavaScript releases the allocation through the N-API finalizer. |
| 42 | +pub fn intoValue(self: OwnedBuffer, env: Env) !Value { |
| 43 | + const allocator = self.allocator; |
| 44 | + const data = self.data; |
| 45 | + |
| 46 | + if (data.len == 0) { |
| 47 | + defer allocator.free(data); |
| 48 | + return try env.createBuffer(0, null); |
| 49 | + } |
| 50 | + |
| 51 | + const context = try createFinalizerContext(self); |
| 52 | + |
| 53 | + return env.createExternalBuffer(data, finalize, context) catch |err| { |
| 54 | + if (err == error.NoExternalBuffersAllowed) { |
| 55 | + defer release(context); |
| 56 | + return try env.createBufferCopy(data, null); |
| 57 | + } |
| 58 | + // Other failures may occur after the finalizer has taken ownership. |
| 59 | + return err; |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +fn createFinalizerContext(self: OwnedBuffer) !*FinalizerContext { |
| 64 | + const context = self.allocator.create(FinalizerContext) catch |err| { |
| 65 | + self.allocator.free(self.data); |
| 66 | + return err; |
| 67 | + }; |
| 68 | + context.* = .{ |
| 69 | + .allocator = self.allocator, |
| 70 | + .data = self.data, |
| 71 | + }; |
| 72 | + return context; |
| 73 | +} |
| 74 | + |
| 75 | +fn finalize( |
| 76 | + _: c.napi_env, |
| 77 | + finalize_data: ?*anyopaque, |
| 78 | + finalize_hint: ?*anyopaque, |
| 79 | +) callconv(.c) void { |
| 80 | + const context: *FinalizerContext = @ptrCast(@alignCast(finalize_hint orelse unreachable)); |
| 81 | + std.debug.assert(finalize_data == @as(?*anyopaque, @ptrCast(context.data.ptr))); |
| 82 | + release(context); |
| 83 | +} |
| 84 | + |
| 85 | +fn release(context: *FinalizerContext) void { |
| 86 | + const allocator = context.allocator; |
| 87 | + allocator.free(context.data); |
| 88 | + allocator.destroy(context); |
| 89 | +} |
| 90 | + |
| 91 | +test "OwnedBuffer fromSlice owns an independent copy" { |
| 92 | + var source = [_]u8{ 1, 2, 3 }; |
| 93 | + var buffer = try OwnedBuffer.fromSlice(std.testing.allocator, &source); |
| 94 | + defer buffer.deinit(); |
| 95 | + |
| 96 | + source[0] = 9; |
| 97 | + try std.testing.expectEqualSlices(u8, &.{ 1, 2, 3 }, buffer.data); |
| 98 | +} |
| 99 | + |
| 100 | +test "OwnedBuffer releases data when finalizer context allocation fails" { |
| 101 | + const data = try std.testing.allocator.dupe(u8, "external"); |
| 102 | + |
| 103 | + var failing_allocator = std.testing.FailingAllocator.init(std.testing.allocator, .{ |
| 104 | + .fail_index = 0, |
| 105 | + }); |
| 106 | + const buffer = OwnedBuffer.fromOwnedSlice(failing_allocator.allocator(), data); |
| 107 | + |
| 108 | + try std.testing.expectError(error.OutOfMemory, createFinalizerContext(buffer)); |
| 109 | + try std.testing.expectEqual(@as(usize, 1), failing_allocator.deallocations); |
| 110 | +} |
0 commit comments