Miscellaneous script splitting preparation changes (#36216)

* script: Move num module to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Make JS reflector creation generic over DOM trait.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move bindings-specific lock to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move DOM proto array code to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move finalizer implementations to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move some error routines to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move some DOM interface conversion routines to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Make is_array_like generic over DOM trait.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Use generic interfaces for conditional exposure functions.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move a bunch of routines used by codegen to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Formatting.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Fix clippy warnings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-03-29 04:11:27 -04:00 committed by GitHub
parent 2c94110952
commit c30ad5a30e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 836 additions and 691 deletions

View file

@ -5,11 +5,12 @@
//! Machinery to conditionally expose things.
use js::rust::HandleObject;
use script_bindings::codegen::Globals::Globals;
use servo_config::prefs::get;
use crate::dom::bindings::codegen::InterfaceObjectMap;
use crate::DomTypes;
use crate::dom::bindings::interface::is_exposed_in;
use crate::dom::globalscope::GlobalScope;
use crate::dom::globalscope::GlobalScopeHelpers;
use crate::realms::{AlreadyInRealm, InRealm};
use crate::script_runtime::JSContext;
@ -28,7 +29,7 @@ impl<T: Clone + Copy> Guard<T> {
/// Expose the value if the conditions are satisfied.
///
/// The passed handle is the object on which the value may be exposed.
pub(crate) fn expose(
pub(crate) fn expose<D: DomTypes>(
&self,
cx: JSContext,
obj: HandleObject,
@ -45,7 +46,7 @@ impl<T: Clone + Copy> Guard<T> {
exposed_on_global |= is_exposed_in(global, *globals);
true
},
_ => c.is_satisfied(cx, obj, global),
_ => c.is_satisfied::<D>(cx, obj, global),
});
if conditions_satisfied && exposed_on_global {
@ -64,21 +65,21 @@ pub(crate) enum Condition {
/// The condition is satisfied if the preference is set.
Pref(&'static str),
// The condition is satisfied if the interface is exposed in the global.
Exposed(InterfaceObjectMap::Globals),
Exposed(Globals),
SecureContext(),
/// The condition is always satisfied.
Satisfied,
}
fn is_secure_context(cx: JSContext) -> bool {
fn is_secure_context<D: DomTypes>(cx: JSContext) -> bool {
unsafe {
let in_realm_proof = AlreadyInRealm::assert_for_cx(JSContext::from_ptr(*cx));
GlobalScope::from_context(*cx, InRealm::Already(&in_realm_proof)).is_secure_context()
D::GlobalScope::from_context(*cx, InRealm::Already(&in_realm_proof)).is_secure_context()
}
}
impl Condition {
pub(crate) fn is_satisfied(
pub(crate) fn is_satisfied<D: DomTypes>(
&self,
cx: JSContext,
obj: HandleObject,
@ -88,7 +89,7 @@ impl Condition {
Condition::Pref(name) => get().get_value(name).try_into().unwrap_or(false),
Condition::Func(f) => f(cx, obj),
Condition::Exposed(globals) => is_exposed_in(global, globals),
Condition::SecureContext() => is_secure_context(cx),
Condition::SecureContext() => is_secure_context::<D>(cx),
Condition::Satisfied => true,
}
}