Repository: servo/servo
File: components/script/dom/messageport.rs (Rust)

Code before:
            return Ok(());
        }
        self.post_message_impl(cx, message, transfer)
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-messageport-postmessage>
    fn PostMessage_(
        &self,
        cx: SafeJSContext,
        message: HandleValue,
        options: RootedTraceableBox<StructuredSerializeOptions>,
    ) -> ErrorResult {
        if self.detached.get() {
            return Ok(());
        }
        let mut rooted = CustomAutoRooter::new(
            options
                .transfer
                .iter()
                .map(|js: &RootedTraceableBox<Heap<*mut JSObject>>| js.get())
                .collect(),
        );
        let guard = CustomAutoRooterGuard::new(*cx, &mut rooted);
        self.post_message_impl(cx, message, guard)
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-messageport-start>
    fn Start(&self, can_gc: CanGc) {
        if self.detached.get() {
            return;
        }
        self.global()
            .start_message_port(self.message_port_id(), can_gc);
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-messageport-close>
    fn Close(&self, can_gc: CanGc) {
        // Set this's [[Detached]] internal slot value to true.
        self.detached.set(true);

        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>
    fn GetOnmessage(&self, can_gc: CanGc) -> Option<Rc<EventHandlerNonNull>> {
        if self.detached.get() {
            return None;

Review comment (suggestion):
I tried to push the change to `MessagePort::post_message_impl`, but that function is also used in `MessagePort::pack_and_post_message`, which seems to affect a lot of places. I am not sure if those changes are in scope.

Code after:
        if self.detached.get() {
            return Ok(());
        }
        self.post_message_impl(cx.into(), message, transfer)
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-messageport-postmessage>
    fn PostMessage_(
        &self,
        cx: &mut JSContext,
        message: HandleValue,
        options: RootedTraceableBox<StructuredSerializeOptions>,
    ) -> ErrorResult {
        if self.detached.get() {
            return Ok(());
        }
        let mut rooted = CustomAutoRooter::new(
            options
                .transfer
                .iter()
                .map(|js: &RootedTraceableBox<Heap<*mut JSObject>>| js.get())
                .collect(),
        );
        #[expect(unsafe_code)]
        let guard = unsafe { CustomAutoRooterGuard::new(cx.raw_cx(), &mut rooted) };
        self.post_message_impl(cx.into(), message, guard)
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-messageport-start>
    fn Start(&self, can_gc: CanGc) {
        if self.detached.get() {
            return;
        }
        self.global()
            .start_message_port(self.message_port_id(), can_gc);
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-messageport-close>
    fn Close(&self, can_gc: CanGc) {
        // Set this's [[Detached]] internal slot value to true.
        self.detached.set(true);

        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>
    fn GetOnmessage(&self, can_gc: CanGc) -> Option<Rc<EventHandlerNonNull>> {