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

@ -2,14 +2,103 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use js::rust::{HandleObject, MutableHandleObject};
use std::cell::RefCell;
use std::rc::Rc;
use std::thread::LocalKey;
use crate::script_runtime::JSContext;
use js::conversions::ToJSValConvertible;
use js::glue::JSPrincipalsCallbacks;
use js::jsapi::{CallArgs, HandleObject as RawHandleObject, JSContext as RawJSContext, JSObject};
use js::rust::{HandleObject, MutableHandleObject};
use servo_url::{MutableOrigin, ServoUrl};
use crate::DomTypes;
use crate::codegen::PrototypeList;
use crate::conversions::DerivedFrom;
use crate::error::Error;
use crate::realms::InRealm;
use crate::reflector::{DomObject, DomObjectWrap};
use crate::root::DomRoot;
use crate::script_runtime::{CanGc, JSContext};
use crate::settings_stack::StackEntry;
use crate::utils::ProtoOrIfaceArray;
/// Operations that must be invoked from the generated bindings.
pub trait DomHelpers<D: DomTypes> {
fn throw_dom_exception(cx: JSContext, global: &D::GlobalScope, result: Error, can_gc: CanGc);
fn call_html_constructor<T: DerivedFrom<D::Element> + DomObject>(
cx: JSContext,
args: &CallArgs,
global: &D::GlobalScope,
proto_id: PrototypeList::ID,
creator: unsafe fn(JSContext, HandleObject, *mut ProtoOrIfaceArray),
can_gc: CanGc,
) -> bool;
fn settings_stack() -> &'static LocalKey<RefCell<Vec<StackEntry<D>>>>;
fn principals_callbacks() -> &'static JSPrincipalsCallbacks;
fn is_platform_object_same_origin(cx: JSContext, obj: RawHandleObject) -> bool;
fn interface_map() -> &'static phf::Map<&'static [u8], for<'a> fn(JSContext, HandleObject)>;
fn push_new_element_queue();
fn pop_current_element_queue(can_gc: CanGc);
fn reflect_dom_object<T, U>(obj: Box<T>, global: &U, can_gc: CanGc) -> DomRoot<T>
where
T: DomObject + DomObjectWrap<D>,
U: DerivedFrom<D::GlobalScope>;
fn report_pending_exception(cx: JSContext, dispatch_event: bool, realm: InRealm, can_gc: CanGc);
}
/// Operations that must be invoked from the generated bindings.
#[allow(unsafe_code)]
pub trait GlobalScopeHelpers<D: DomTypes> {
/// # Safety
/// `cx` must point to a valid, non-null RawJSContext.
unsafe fn from_context(cx: *mut RawJSContext, realm: InRealm) -> DomRoot<D::GlobalScope>;
fn get_cx() -> JSContext;
/// # Safety
/// `obj` must point to a valid, non-null JSObject.
unsafe fn from_object(obj: *mut JSObject) -> DomRoot<D::GlobalScope>;
fn from_reflector(reflector: &impl DomObject, realm: InRealm) -> DomRoot<D::GlobalScope>;
/// # Safety
/// `obj` must point to a valid, non-null JSObject.
/// `cx` must point to a valid, non-null RawJSContext.
unsafe fn from_object_maybe_wrapped(
obj: *mut JSObject,
cx: *mut RawJSContext,
) -> DomRoot<D::GlobalScope>;
fn origin(&self) -> &MutableOrigin;
fn incumbent() -> Option<DomRoot<D::GlobalScope>>;
fn perform_a_microtask_checkpoint(&self, can_gc: CanGc);
fn get_url(&self) -> ServoUrl;
fn is_secure_context(&self) -> bool;
}
pub trait DocumentHelpers {
fn ensure_safe_to_run_script_or_layout(&self);
}
/// Operations that must be invoked from the generated bindings.
pub trait PromiseHelpers<D: crate::DomTypes> {
fn new_resolved(
global: &D::GlobalScope,
cx: JSContext,
value: impl ToJSValConvertible,
) -> Rc<D::Promise>;
}
pub trait ServoInternalsHelpers {
fn is_servo_internal(cx: JSContext, global: HandleObject) -> bool;
}