Add finalizer to Response and use an pooled arena

Unlike XHR, Response is a bit more complicated as it can exist in Zig code
without ever being given to v8. So we need to track this handoff to know who is
responsible for freeing it (zig code, on error/shutdown) or v8 code after
promise resolution.

This also cleansup a bad merge for the XHR finalizer and adds cleaning up the
`XMLHttpRequestEventTarget` callbacks.
This commit is contained in:
Karl Seguin
2026-01-26 19:18:32 +08:00
parent 1f14eb62d4
commit a11ae912b4
8 changed files with 147 additions and 57 deletions

View File

@@ -186,7 +186,7 @@ pub fn abort(self: *Client) void {
while (n) |node| {
n = node.next;
const transfer: *Transfer = @fieldParentPtr("_node", node);
self.transfer_pool.destroy(transfer);
transfer.kill();
}
self.queue = .{};
@@ -392,6 +392,8 @@ fn requestFailed(self: *Client, transfer: *Transfer, err: anyerror, comptime exe
if (execute_callback) {
transfer.req.error_callback(transfer.ctx, err);
} else if (transfer.req.shutdown_callback) |cb| {
cb(transfer.ctx);
}
}
@@ -781,6 +783,7 @@ pub const Request = struct {
data_callback: *const fn (transfer: *Transfer, data: []const u8) anyerror!void,
done_callback: *const fn (ctx: *anyopaque) anyerror!void,
error_callback: *const fn (ctx: *anyopaque, err: anyerror) void,
shutdown_callback: ?*const fn (ctx: *anyopaque) void = null,
const ResourceType = enum {
document,
@@ -995,6 +998,9 @@ pub const Transfer = struct {
if (self._handle != null) {
self.client.endTransfer(self);
}
if (self.req.shutdown_callback) |cb| {
cb(self.ctx);
}
self.deinit();
}