MessagePort: implement disentanglement (#36654)

Implement
[disentangle](https://html.spec.whatwg.org/multipage/#disentangle)
Remove bespoke gc logic which now becomes unnecessary. 
Adds a wpt test that hits the "disentangle while in transfer" logic.
Updates streams code, fixing an error where disentanglement is
conditional on an error.

Test coverage: there are existing tests in
`/webmessaging/message-channels/close-event/explicitly-closed.tentative.window.js`
for the no transfer case, and the simple completed transfer case, and
this PR adds a test for the more complicated transfer in progress case.

Fix https://github.com/servo/servo/issues/36465

---------

Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com>
This commit is contained in:
Gregory Terzian 2025-04-30 12:49:38 +02:00 committed by GitHub
parent c46402e222
commit af5d665efa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 356 additions and 191 deletions

View file

@ -83,6 +83,20 @@ impl MessagePort {
*self.entangled_port.borrow_mut() = Some(other_id);
}
/// <https://html.spec.whatwg.org/multipage/#disentangle>
pub(crate) fn disentangle(&self) -> Option<MessagePortId> {
// Disentangle initiatorPort and otherPort, so that they are no longer entangled or associated with each other.
// Note: called from `disentangle_port` in the global, where the rest happens.
self.entangled_port.borrow_mut().take()
}
/// Has the port been disentangled?
/// Used when starting the port to fire the `close` event,
/// to cover the case of a disentanglement while in transfer.
pub(crate) fn disentangled(&self) -> bool {
self.entangled_port.borrow().is_none()
}
pub(crate) fn message_port_id(&self) -> &MessagePortId {
&self.message_port_id
}
@ -314,20 +328,28 @@ impl MessagePortMethods<crate::DomTypeHolder> for MessagePort {
}
/// <https://html.spec.whatwg.org/multipage/#dom-messageport-start>
fn Start(&self) {
fn Start(&self, can_gc: CanGc) {
if self.detached.get() {
return;
}
self.global().start_message_port(self.message_port_id());
self.global()
.start_message_port(self.message_port_id(), can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#dom-messageport-close>
fn Close(&self) {
fn Close(&self, can_gc: CanGc) {
if self.detached.get() {
return;
}
// Set this's [[Detached]] internal slot value to true.
self.detached.set(true);
self.global().close_message_port(self.message_port_id());
let global = self.global();
global.close_message_port(self.message_port_id());
// If this is entangled, disentangle it.
global.disentangle_port(self, can_gc);
}
/// <https://html.spec.whatwg.org/multipage/#handler-messageport-onmessage>
@ -340,15 +362,19 @@ impl MessagePortMethods<crate::DomTypeHolder> for MessagePort {
}
/// <https://html.spec.whatwg.org/multipage/#handler-messageport-onmessage>
fn SetOnmessage(&self, listener: Option<Rc<EventHandlerNonNull>>) {
fn SetOnmessage(&self, listener: Option<Rc<EventHandlerNonNull>>, can_gc: CanGc) {
if self.detached.get() {
return;
}
self.set_onmessage(listener);
// Note: we cannot use the event_handler macro, due to the need to start the port.
self.global().start_message_port(self.message_port_id());
self.global()
.start_message_port(self.message_port_id(), can_gc);
}
// <https://html.spec.whatwg.org/multipage/#handler-messageport-onmessageerror>
event_handler!(messageerror, GetOnmessageerror, SetOnmessageerror);
// <https://html.spec.whatwg.org/multipage/#handler-messageport-onclose>
event_handler!(close, GetOnclose, SetOnclose);
}