From f1f01f54ed727bb22cb2aa0d7786d66dbba4fe36 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Tue, 6 Oct 2026 19:26:15 +0800 Subject: [PATCH] fix(dom): unload child documents in their registered execution realm --- .../child_frame_runtime/document.rs | 11 ++- .../script_vm/tests/retained_child_window.rs | 81 +++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs index caf8a18a91f..89e6f9a36b8 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs @@ -23,8 +23,15 @@ impl PageScriptHost { handle: DomHandle, ) -> Option> { let document = self.child_browsing_context_document_handle(handle)?; - let window = self.existing_child_browsing_context_window_wrapper(scope, handle)?; - let context = window.get_creation_context(scope)?; + let owner = self.current_child_document_task_owner(handle)?; + // A stable WindowProxy can be parked on a facade between LocalWindow + // generations. Only the current Document's registered execution realm + // can create its wrapper; lifecycle work must not initialize a realm. + let (_, context) = self.window_execution_context( + scope, + WindowExecutionContextOwner::Frame(owner.local_window_id), + OwnerDispatchScope::Child(handle), + )?; let scope = &mut v8::ContextScope::new(scope, context); let host_ptr = self as *mut PageScriptHost; // Removal callbacks still belong to the retiring Document even after diff --git a/moli-renderer-v8/src/script_vm/tests/retained_child_window.rs b/moli-renderer-v8/src/script_vm/tests/retained_child_window.rs index 49e0c34930e..d54d0de5090 100644 --- a/moli-renderer-v8/src/script_vm/tests/retained_child_window.rs +++ b/moli-renderer-v8/src/script_vm/tests/retained_child_window.rs @@ -1,5 +1,86 @@ use super::*; +#[tokio::test] +async fn unload_skips_child_window_facade_awaiting_replacement_realm() { + let mut vm = new_storage_html_test_vm("https://child-facade-unload.test/"); + vm.eval( + r#" +globalThis.frame = document.createElement('iframe'); +frame.srcdoc = '

outgoing child'; +document.body.appendChild(frame); +"#, + ) + .expect("outgoing child navigation should queue"); + run_child_navigation_commit_and_host_load_for_test(&mut vm, "outgoing child Document").await; + vm.eval( + r#" +globalThis.heldWindow = frame.contentWindow; +globalThis.childUnloads = 0; +heldWindow.addEventListener('unload', () => childUnloads++); +"#, + ) + .expect("outgoing child Document and unload listener should be initialized"); + let previous = current_single_child_document_owner_for_test(&vm, "outgoing child"); + vm.eval( + r#" +frame.sandbox = 'allow-scripts'; +frame.srcdoc = '

opaque replacement'; +"#, + ) + .expect("opaque replacement should queue"); + for _ in 0..8 { + assert!( + vm.run_next_child_navigation_commit_body_for_test() + .expect("child navigation commit should succeed") + .is_some(), + "the pending replacement must retain its navigation task" + ); + if current_single_child_document_owner_for_test(&vm, "replacement child").local_window_id + != previous.local_window_id + { + break; + } + } + let current = current_single_child_document_owner_for_test(&vm, "replacement child"); + assert_ne!(current.local_window_id, previous.local_window_id); + assert_eq!(vm.child_frame_realm_store.len(), 0); + assert!(vm.has_pending_child_frame_realm_materialization()); + assert_eq!(vm.eval("String(childUnloads)").unwrap(), "1"); + + vm.with_default_context_scope(|scope, host_ptr| { + let host = unsafe { &mut *host_ptr }; + let child_handle = host.child_browsing_context_handles_in_document_order()[0]; + let window = host + .existing_child_browsing_context_window_wrapper(scope, child_handle) + .expect("the stable WindowProxy must remain available between realms"); + let facade = window.get_creation_context(scope).unwrap(); + assert!( + facade + .get_slot::() + .is_none(), + "a parked WindowProxy facade has no initialized execution realm" + ); + assert!( + host.window_execution_context( + scope, + crate::native_bridge::WindowExecutionContextOwner::Frame(current.local_window_id), + crate::native_bridge::OwnerDispatchScope::Child(child_handle), + ) + .is_none(), + "the replacement child Document must still await its own realm" + ); + // Exercise the same child unload path used by page close and frame + // removal, without requiring a Page-owned main Document in this VM. + crate::native_bridge::PageScriptHost::dispatch_document_open_descendant_frame_unload_lifecycle( + scope, host_ptr, host.document_handle(), + ); + Ok(()) + }) + .expect("closing before replacement realm initialization must not enter the facade"); + assert_eq!(vm.child_frame_realm_store.len(), 0); + assert_eq!(vm.eval("String(childUnloads)").unwrap(), "1"); +} + #[test] fn child_document_open_navigation_keeps_window_accessible_before_realm_turn() { for materialized in [false, true] {