script: Use enums for event loop senders and receivers (#34896)

Previously, senders and receivers to different kinds of event loops (the
main `ScriptThread`, different types of workers) used a rust `trait`
mechanism to implement dynamic behavior. This led to having many unused
implementations of this `trait`. This change moves to using an `enum`
based approach for these senders and receivers and removes all of the
dead code.

In addition, to allowing for use of rust's dead code detection, it
simplifies the code a great deal. All of these generic senders and
receivers are moved to the `messaging.rs` file and given proper
documentation.

Finally, empty an `JSTraceable` implementation is made for all
crossbeam `Sender<...>`s to avoid having to manually skip them everytime
they are included in structs. The pre-existing empty `MallocSizeOf`
implementation is used more thoroughly.

Other unecessary wrappers around these senders and receivers are removed
as well.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-01-08 22:33:29 +01:00 committed by GitHub
parent 82ac8d41d0
commit 77bc7f415d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 217 additions and 325 deletions

View file

@ -53,7 +53,10 @@ mod dummy {
pub use self::dummy::LIVE_REFERENCES;
/// A pointer to a Rust DOM object that needs to be destroyed.
struct TrustedReference(*const libc::c_void);
#[derive(MallocSizeOf)]
struct TrustedReference(
#[ignore_malloc_size_of = "This is a shared reference."] *const libc::c_void,
);
unsafe impl Send for TrustedReference {}
impl TrustedReference {
@ -158,10 +161,13 @@ impl TrustedPromise {
/// DOM object is guaranteed to live at least as long as the last outstanding
/// `Trusted<T>` instance.
#[crown::unrooted_must_root_lint::allow_unrooted_interior]
#[derive(MallocSizeOf)]
pub struct Trusted<T: DomObject> {
/// A pointer to the Rust DOM object of type T, but void to allow
/// sending `Trusted<T>` between threads, regardless of T's sendability.
#[conditional_malloc_size_of]
refcount: Arc<TrustedReference>,
#[ignore_malloc_size_of = "These are shared by all `Trusted` types."]
owner_thread: *const LiveDOMReferences,
phantom: PhantomData<T>,
}

View file

@ -37,6 +37,7 @@ use std::hash::{BuildHasher, Hash};
use std::mem;
use std::ops::{Deref, DerefMut};
use crossbeam_channel::Sender;
use indexmap::IndexMap;
/// A trait to allow tracing (only) DOM objects.
pub use js::gc::Traceable as JSTraceable;
@ -107,6 +108,10 @@ unsafe impl<T: JSTraceable> CustomTraceable for OnceCell<T> {
}
}
unsafe impl<T> CustomTraceable for Sender<T> {
unsafe fn trace(&self, _: *mut JSTracer) {}
}
/// Wrapper type for nop traceble
///
/// SAFETY: Inner type must not impl JSTraceable