use destructor callback for FetchContext

This commit is contained in:
Muki Kiboigo
2025-09-08 07:07:07 -07:00
parent 9251180501
commit 20463a662b
2 changed files with 17 additions and 10 deletions

View File

@@ -43,7 +43,7 @@ pub const Interfaces = .{
@import("Response.zig"),
};
const FetchContext = struct {
pub const FetchContext = struct {
arena: std.mem.Allocator,
js_ctx: *Env.JsContext,
promise_resolver: v8.Persistent(v8.PromiseResolver),
@@ -79,6 +79,17 @@ const FetchContext = struct {
.url = self.url,
};
}
pub fn destructor(self: *FetchContext) void {
if (self.transfer) |_| {
const resolver = Env.PromiseResolver{
.js_context = self.js_ctx,
.resolver = self.promise_resolver.castToPromiseResolver(),
};
resolver.reject("TypeError") catch unreachable;
}
}
};
// https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch
@@ -107,6 +118,9 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
.url = req.url,
};
// Add destructor callback for FetchContext.
try page.main_context.destructor_callbacks.append(arena, Env.DestructorCallback.init(fetch_ctx));
try page.http_client.request(.{
.ctx = @ptrCast(fetch_ctx),
.url = req.url,
@@ -189,13 +203,6 @@ pub fn fetch(input: RequestInput, options: ?RequestInit, page: *Page) !Env.Promi
.err = err,
.source = "fetch error",
});
const promise_resolver: Env.PromiseResolver = .{
.js_context = self.js_ctx,
.resolver = self.promise_resolver.castToPromiseResolver(),
};
promise_resolver.reject(@errorName(err)) catch unreachable;
}
}.errorCallback,
});

View File

@@ -2901,11 +2901,11 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
// An interface for types that want to have their jsDeinit function to be
// called when the call context ends
const DestructorCallback = struct {
pub const DestructorCallback = struct {
ptr: *anyopaque,
destructorFn: *const fn (ptr: *anyopaque) void,
fn init(ptr: anytype) DestructorCallback {
pub fn init(ptr: anytype) DestructorCallback {
const T = @TypeOf(ptr);
const ptr_info = @typeInfo(T);