Move generated bindings to script_bindings (#36323)

This is the final step of #1799, where the majority of the generated
code for the JS bindings is now compiled as part of the script_bindings
build step. The remaining pieces in script must live there because they
refer to concrete DOM types; all code in script_bindings is generic over
the
[DomTypes](https://doc.servo.org/script/dom/bindings/codegen/DomTypes/trait.DomTypes.html)
trait.

My testing with incremental builds shows me a 12 second reduction in
build times on my 2024 M4 Macbook Pro when modifying code in the script
crate after these changes. Before this PR those changes took 20 seconds
to rebuild Servo, and now they take 8 seconds.

Testing: Existing WPT tests ensure no regressions.
Fixes: #1799

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-04-04 02:45:08 -04:00 committed by GitHub
parent 277c0b82dd
commit b4079b3ff3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
44 changed files with 1932 additions and 1829 deletions

View file

@ -6,6 +6,13 @@ use js::jsapi::{Heap, JSObject};
use js::rust::HandleObject;
use malloc_size_of_derive::MallocSizeOf;
use crate::interfaces::GlobalScopeHelpers;
use crate::iterable::{Iterable, IterableIterator};
use crate::realms::{AlreadyInRealm, InRealm};
use crate::root::{Dom, DomRoot, Root};
use crate::script_runtime::{CanGc, JSContext};
use crate::{DomTypes, JSTraceable};
/// A struct to store a reference to the reflector of a DOM object.
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
#[derive(MallocSizeOf)]
@ -90,3 +97,55 @@ impl MutDomObject for Reflector {
self.set_jsobject(obj)
}
}
pub trait DomGlobalGeneric<D: DomTypes>: DomObject {
/// Returns the [`GlobalScope`] of the realm that the [`DomObject`] was created in. If this
/// object is a `Node`, this will be different from it's owning `Document` if adopted by. For
/// `Node`s it's almost always better to use `NodeTraits::owning_global`.
fn global_(&self, realm: InRealm) -> DomRoot<D::GlobalScope>
where
Self: Sized,
{
D::GlobalScope::from_reflector(self, realm)
}
/// Returns the [`GlobalScope`] of the realm that the [`DomObject`] was created in. If this
/// object is a `Node`, this will be different from it's owning `Document` if adopted by. For
/// `Node`s it's almost always better to use `NodeTraits::owning_global`.
fn global(&self) -> DomRoot<D::GlobalScope>
where
Self: Sized,
{
let realm = AlreadyInRealm::assert_for_cx(D::GlobalScope::get_cx());
D::GlobalScope::from_reflector(self, InRealm::already(&realm))
}
}
impl<D: DomTypes, T: DomObject> DomGlobalGeneric<D> for T {}
/// A trait to provide a function pointer to wrap function for DOM objects.
pub trait DomObjectWrap<D: DomTypes>: Sized + DomObject + DomGlobalGeneric<D> {
/// Function pointer to the general wrap function type
#[allow(clippy::type_complexity)]
const WRAP: unsafe fn(
JSContext,
&D::GlobalScope,
Option<HandleObject>,
Box<Self>,
CanGc,
) -> Root<Dom<Self>>;
}
/// A trait to provide a function pointer to wrap function for
/// DOM iterator interfaces.
pub trait DomObjectIteratorWrap<D: DomTypes>: DomObjectWrap<D> + JSTraceable + Iterable {
/// Function pointer to the wrap function for `IterableIterator<T>`
#[allow(clippy::type_complexity)]
const ITER_WRAP: unsafe fn(
JSContext,
&D::GlobalScope,
Option<HandleObject>,
Box<IterableIterator<D, Self>>,
CanGc,
) -> Root<Dom<IterableIterator<D, Self>>>;
}