From ce832a8063733b4df916ad1b7602a73051531966 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Wed, 21 May 2025 11:38:26 +0800 Subject: [PATCH] Rollback XHR/HTTP.client change This PR will be only for having the destructor hook. XHR/http.client changes to leverage this will be done in a subsequent PR. --- src/browser/dom/mutation_observer.zig | 2 +- src/browser/xhr/xhr.zig | 8 ------- src/http/client.zig | 7 ------ src/runtime/js.zig | 33 ++++++++++++++++----------- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/src/browser/dom/mutation_observer.zig b/src/browser/dom/mutation_observer.zig index 203a05a0..71459e87 100644 --- a/src/browser/dom/mutation_observer.zig +++ b/src/browser/dom/mutation_observer.zig @@ -103,7 +103,7 @@ pub const MutationObserver = struct { } } - pub fn jsCallScopeEnd(self: *MutationObserver, _: anytype) void { + pub fn jsCallScopeEnd(self: *MutationObserver) void { const record = self.observed.items; if (record.len == 0) { return; diff --git a/src/browser/xhr/xhr.zig b/src/browser/xhr/xhr.zig index be0fcfd6..67dfbca1 100644 --- a/src/browser/xhr/xhr.zig +++ b/src/browser/xhr/xhr.zig @@ -257,10 +257,6 @@ pub const XMLHttpRequest = struct { }; } - pub fn destructor(self: *XMLHttpRequest, _: anytype) void { - self._abort(); - } - pub fn reset(self: *XMLHttpRequest) void { self.url = null; @@ -539,10 +535,6 @@ pub const XMLHttpRequest = struct { } pub fn _abort(self: *XMLHttpRequest) void { - const request = &(self.request orelse return); - // safe to call even if the request is complete - request.abort(); - self.onErr(DOMError.Abort); } diff --git a/src/http/client.zig b/src/http/client.zig index 6b0ae83b..a6fa1615 100644 --- a/src/http/client.zig +++ b/src/http/client.zig @@ -261,13 +261,6 @@ pub const Request = struct { self._client.state_pool.release(self._state); } - pub fn abort(self: *Request) void { - const connection = self._connection orelse return; - self.destroyConnection(connection); - self._connection = null; - self.deinit(); - } - const DecomposedURL = struct { secure: bool, connect_port: u16, diff --git a/src/runtime/js.zig b/src/runtime/js.zig index 673fbfa3..1a296ca8 100644 --- a/src/runtime/js.zig +++ b/src/runtime/js.zig @@ -539,8 +539,15 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { // no init, started with executor.startScope() fn deinit(self: *Scope) void { - for (self.destructor_callbacks.items) |cb| { - cb.destructor(self); + { + // reverse order, as this has more chance of respecting any + // dependencies objects might have with each other. + const items = self.destructor_callbacks.items; + var i = items.len; + while (i > 0) { + i -= 1; + items[i].destructor(); + } } { @@ -1687,16 +1694,16 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { // called when the call scope ends const DestructorCallback = struct { ptr: *anyopaque, - destructorFn: *const fn (ptr: *anyopaque, scope: *Scope) void, + destructorFn: *const fn (ptr: *anyopaque) void, fn init(ptr: anytype) DestructorCallback { const T = @TypeOf(ptr); const ptr_info = @typeInfo(T); const gen = struct { - pub fn destructor(pointer: *anyopaque, scope: *Scope) void { + pub fn destructor(pointer: *anyopaque) void { const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.pointer.child.destructor(self, scope); + return ptr_info.pointer.child.destructor(self); } }; @@ -1706,8 +1713,8 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { }; } - pub fn destructor(self: DestructorCallback, scope: *Scope) void { - self.destructorFn(self.ptr, scope); + pub fn destructor(self: DestructorCallback) void { + self.destructorFn(self.ptr); } }; @@ -1715,16 +1722,16 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { // called when the call scope ends const CallScopeEndCallback = struct { ptr: *anyopaque, - callScopeEndFn: *const fn (ptr: *anyopaque, scope: *Scope) void, + callScopeEndFn: *const fn (ptr: *anyopaque) void, fn init(ptr: anytype) CallScopeEndCallback { const T = @TypeOf(ptr); const ptr_info = @typeInfo(T); const gen = struct { - pub fn callScopeEnd(pointer: *anyopaque, scope: *Scope) void { + pub fn callScopeEnd(pointer: *anyopaque) void { const self: T = @ptrCast(@alignCast(pointer)); - return ptr_info.pointer.child.jsCallScopeEnd(self, scope); + return ptr_info.pointer.child.jsCallScopeEnd(self); } }; @@ -1734,8 +1741,8 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { }; } - pub fn callScopeEnd(self: CallScopeEndCallback, scope: *Scope) void { - self.callScopeEndFn(self.ptr, scope); + pub fn callScopeEnd(self: CallScopeEndCallback) void { + self.callScopeEndFn(self.ptr); } }; }; @@ -1815,7 +1822,7 @@ fn Caller(comptime E: type, comptime State: type) type { // when a top-level (call_depth == 0) function ends. if (call_depth == 0) { for (scope.call_scope_end_callbacks.items) |cb| { - cb.callScopeEnd(scope); + cb.callScopeEnd(); } const arena: *ArenaAllocator = @alignCast(@ptrCast(scope.call_arena.ptr));