Lightpanda: the headless browser designed for AI and automation
v8: When re-using window, detach from original context first
Karl Seguinad3cf997df62
diff --git a/src/browser/Session.zig b/src/browser/Session.zigindex 4d113012f..50901ae82 100644--- a/src/browser/Session.zig+++ b/src/browser/Session.zig@@ -660,6 +660,7 @@ fn _processFrameNavigation(self: *Session, frame: *Frame, qn: *QueuedNavigation)const frame_id = frame._frame_id;const reuse_window = frame.window;const page = frame._page;+ frame.js.detachGlobal();frame.deinit();frame.* = undefined;@@ -705,6 +706,7 @@ fn processPopupNavigation(_: *Session, frame: *Frame, qn: *QueuedNavigation) !voconst frame_id = frame._frame_id;const page = frame._page;+ frame.js.detachGlobal();frame.deinit();frame.* = undefined;diff --git a/src/browser/js/Context.zig b/src/browser/js/Context.zigindex 7cc777f23..ec77a7e87 100644--- a/src/browser/js/Context.zig+++ b/src/browser/js/Context.zig@@ -229,6 +229,24 @@ pub fn deinit(self: *Context) void {v8.v8__MicrotaskQueue__DELETE(self.microtask_queue);}+// The global (e.g. Window) can be reused across contexts. If you do:+//+// var w = iframe.contentWindow;+// iframe.src = 'two.html';+// w === iframe.contentWindow (must be true)+//+// so when we navigate, the Window/Global is re-used. That's fine with v8, but+// we need to explicitly detach it from the original before we can safely attach+// it to the new+pub fn detachGlobal(self: *Context) void {+ var hs: js.HandleScope = undefined;+ hs.init(self.isolate);+ defer hs.deinit();++ const local_v8_context: *const v8.Context = @ptrCast(v8.v8__Global__Get(&self.handle, self.isolate.handle));+ v8.v8__Context__DetachGlobal(local_v8_context);+}+// setOrigin is called at navigation (opaque -> real origin) and again when a// script sets document.domain (real origin -> '!'-marked effective domain).pub fn setOrigin(self: *Context, key: ?[]const u8) !void {