mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Move various reflector types and traits to script_bindings (#35279)
* script: Move Reflector to script_bindings. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * script: Extract global() helper from DomObject into new trait. Move DomObject and related traits to script_bindings. Signed-off-by: Josh Matthews <josh@joshmatthews.net> --------- Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
parent
0d51578cc3
commit
c94ac5bccb
212 changed files with 357 additions and 319 deletions
|
@ -15,12 +15,11 @@ use js::jsapi::{
|
|||
};
|
||||
use js::jsval::{JSVal, ObjectValue, UndefinedValue};
|
||||
use js::rust::wrappers::{JS_GetProperty, JS_WrapObject};
|
||||
use js::rust::{HandleObject, MutableHandleObject, Runtime};
|
||||
use js::rust::{MutableHandleObject, Runtime};
|
||||
|
||||
use crate::dom::bindings::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods;
|
||||
use crate::dom::bindings::error::{report_pending_exception, Error, Fallible};
|
||||
use crate::dom::bindings::inheritance::Castable;
|
||||
use crate::dom::bindings::reflector::DomObject;
|
||||
use crate::dom::bindings::root::{Dom, DomRoot};
|
||||
use crate::dom::bindings::settings_stack::{AutoEntryScript, AutoIncumbentScript};
|
||||
use crate::dom::bindings::utils::AsCCharPtrPtr;
|
||||
|
@ -206,21 +205,7 @@ impl CallbackInterface {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) trait ThisReflector {
|
||||
fn jsobject(&self) -> *mut JSObject;
|
||||
}
|
||||
|
||||
impl<T: DomObject> ThisReflector for T {
|
||||
fn jsobject(&self) -> *mut JSObject {
|
||||
self.reflector().get_jsobject().get()
|
||||
}
|
||||
}
|
||||
|
||||
impl ThisReflector for HandleObject<'_> {
|
||||
fn jsobject(&self) -> *mut JSObject {
|
||||
self.get()
|
||||
}
|
||||
}
|
||||
pub(crate) use script_bindings::callback::ThisReflector;
|
||||
|
||||
/// Wraps the reflector for `p` into the realm of `cx`.
|
||||
pub(crate) fn wrap_call_this_object<T: ThisReflector>(
|
||||
|
|
|
@ -40,11 +40,11 @@ pub(crate) use js::conversions::{
|
|||
use js::error::throw_type_error;
|
||||
use js::glue::{GetProxyReservedSlot, IsWrapper, JS_GetReservedSlot, UnwrapObjectDynamic};
|
||||
use js::jsapi::{Heap, IsWindowProxy, JSContext, JSObject, JS_IsExceptionPending};
|
||||
use js::jsval::{ObjectValue, UndefinedValue};
|
||||
use js::jsval::UndefinedValue;
|
||||
use js::rust::wrappers::{IsArrayObject, JS_GetProperty, JS_HasProperty};
|
||||
use js::rust::{
|
||||
get_object_class, is_dom_class, is_dom_object, maybe_wrap_value, HandleId, HandleObject,
|
||||
HandleValue, MutableHandleValue,
|
||||
get_object_class, is_dom_class, is_dom_object, HandleId, HandleObject, HandleValue,
|
||||
MutableHandleValue,
|
||||
};
|
||||
use num_traits::Float;
|
||||
pub(crate) use script_bindings::conversions::*;
|
||||
|
@ -52,7 +52,7 @@ pub(crate) use script_bindings::conversions::*;
|
|||
use crate::dom::bindings::error::{Error, Fallible};
|
||||
use crate::dom::bindings::inheritance::Castable;
|
||||
use crate::dom::bindings::num::Finite;
|
||||
use crate::dom::bindings::reflector::{DomObject, Reflector};
|
||||
use crate::dom::bindings::reflector::DomObject;
|
||||
use crate::dom::bindings::root::DomRoot;
|
||||
use crate::dom::bindings::str::DOMString;
|
||||
use crate::dom::bindings::trace::{JSTraceable, RootedTraceableBox};
|
||||
|
@ -169,15 +169,6 @@ pub(crate) unsafe fn jsid_to_string(cx: *mut JSContext, id: HandleId) -> Option<
|
|||
None
|
||||
}
|
||||
|
||||
impl ToJSValConvertible for Reflector {
|
||||
unsafe fn to_jsval(&self, cx: *mut JSContext, mut rval: MutableHandleValue) {
|
||||
let obj = self.get_jsobject().get();
|
||||
assert!(!obj.is_null());
|
||||
rval.set(ObjectValue(obj));
|
||||
maybe_wrap_value(cx, rval);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether `obj` is a DOM object implemented as a proxy.
|
||||
pub(crate) fn is_dom_proxy(obj: *mut JSObject) -> bool {
|
||||
use js::glue::IsProxyHandlerFamily;
|
||||
|
|
|
@ -35,7 +35,7 @@ pub(crate) mod base {
|
|||
pub(crate) use crate::dom::bindings::error::{throw_dom_exception, Fallible};
|
||||
pub(crate) use crate::dom::bindings::num::Finite;
|
||||
pub(crate) use crate::dom::bindings::proxyhandler::CrossOriginProperties;
|
||||
pub(crate) use crate::dom::bindings::reflector::DomObject;
|
||||
pub(crate) use crate::dom::bindings::reflector::{DomGlobal, DomObject};
|
||||
pub(crate) use crate::dom::bindings::root::DomRoot;
|
||||
pub(crate) use crate::dom::bindings::str::{ByteString, DOMString, USVString};
|
||||
pub(crate) use crate::dom::bindings::trace::RootedTraceableBox;
|
||||
|
|
|
@ -21,7 +21,7 @@ use crate::dom::bindings::codegen::Bindings::IterableIteratorBinding::{
|
|||
};
|
||||
use crate::dom::bindings::error::Fallible;
|
||||
use crate::dom::bindings::reflector::{
|
||||
reflect_dom_object, DomObjectIteratorWrap, DomObjectWrap, Reflector,
|
||||
reflect_dom_object, DomGlobal, DomObjectIteratorWrap, DomObjectWrap, Reflector,
|
||||
};
|
||||
use crate::dom::bindings::root::{Dom, DomRoot, Root};
|
||||
use crate::dom::bindings::trace::{JSTraceable, RootedTraceableBox};
|
||||
|
|
|
@ -4,9 +4,6 @@
|
|||
|
||||
//! The `Reflector` struct.
|
||||
|
||||
use std::default::Default;
|
||||
|
||||
use js::jsapi::{Heap, JSObject};
|
||||
use js::rust::HandleObject;
|
||||
|
||||
use crate::dom::bindings::conversions::DerivedFrom;
|
||||
|
@ -42,64 +39,7 @@ where
|
|||
unsafe { T::WRAP(GlobalScope::get_cx(), global_scope, proto, obj, can_gc) }
|
||||
}
|
||||
|
||||
/// A struct to store a reference to the reflector of a DOM object.
|
||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
||||
#[derive(MallocSizeOf)]
|
||||
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
|
||||
// If you're renaming or moving this field, update the path in plugins::reflector as well
|
||||
pub(crate) struct Reflector {
|
||||
#[ignore_malloc_size_of = "defined and measured in rust-mozjs"]
|
||||
object: Heap<*mut JSObject>,
|
||||
}
|
||||
|
||||
#[cfg_attr(crown, allow(crown::unrooted_must_root))]
|
||||
impl PartialEq for Reflector {
|
||||
fn eq(&self, other: &Reflector) -> bool {
|
||||
self.object.get() == other.object.get()
|
||||
}
|
||||
}
|
||||
|
||||
impl Reflector {
|
||||
/// Get the reflector.
|
||||
#[inline]
|
||||
pub(crate) fn get_jsobject(&self) -> HandleObject {
|
||||
// We're rooted, so it's safe to hand out a handle to object in Heap
|
||||
unsafe { HandleObject::from_raw(self.object.handle()) }
|
||||
}
|
||||
|
||||
/// Initialize the reflector. (May be called only once.)
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The provided [`JSObject`] pointer must point to a valid [`JSObject`].
|
||||
pub(crate) unsafe fn set_jsobject(&self, object: *mut JSObject) {
|
||||
assert!(self.object.get().is_null());
|
||||
assert!(!object.is_null());
|
||||
self.object.set(object);
|
||||
}
|
||||
|
||||
/// Return a pointer to the memory location at which the JS reflector
|
||||
/// object is stored. Used to root the reflector, as
|
||||
/// required by the JSAPI rooting APIs.
|
||||
pub(crate) fn rootable(&self) -> &Heap<*mut JSObject> {
|
||||
&self.object
|
||||
}
|
||||
|
||||
/// Create an uninitialized `Reflector`.
|
||||
// These are used by the bindings and do not need `default()` functions.
|
||||
#[allow(clippy::new_without_default)]
|
||||
pub(crate) fn new() -> Reflector {
|
||||
Reflector {
|
||||
object: Heap::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait to provide access to the `Reflector` for a DOM object.
|
||||
pub(crate) trait DomObject: JSTraceable + 'static {
|
||||
/// Returns the receiver's reflector.
|
||||
fn reflector(&self) -> &Reflector;
|
||||
|
||||
pub trait DomGlobal: 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`.
|
||||
|
@ -112,27 +52,9 @@ pub(crate) trait DomObject: JSTraceable + 'static {
|
|||
}
|
||||
}
|
||||
|
||||
impl DomObject for Reflector {
|
||||
fn reflector(&self) -> &Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
impl<T: DomObject> DomGlobal for T {}
|
||||
|
||||
/// A trait to initialize the `Reflector` for a DOM object.
|
||||
pub(crate) trait MutDomObject: DomObject {
|
||||
/// Initializes the Reflector
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The provided [`JSObject`] pointer must point to a valid [`JSObject`].
|
||||
unsafe fn init_reflector(&self, obj: *mut JSObject);
|
||||
}
|
||||
|
||||
impl MutDomObject for Reflector {
|
||||
unsafe fn init_reflector(&self, obj: *mut JSObject) {
|
||||
self.set_jsobject(obj)
|
||||
}
|
||||
}
|
||||
pub(crate) use script_bindings::reflector::{DomObject, MutDomObject, Reflector};
|
||||
|
||||
/// A trait to provide a function pointer to wrap function for DOM objects.
|
||||
pub(crate) trait DomObjectWrap: Sized + DomObject {
|
||||
|
|
|
@ -255,8 +255,6 @@ unsafe_no_jsmanaged_fields!(Box<dyn TaskBox>);
|
|||
|
||||
unsafe_no_jsmanaged_fields!(IncompleteParserContexts);
|
||||
|
||||
unsafe_no_jsmanaged_fields!(Reflector);
|
||||
|
||||
#[allow(dead_code)]
|
||||
/// Trace a `JSScript`.
|
||||
pub(crate) fn trace_script(tracer: *mut JSTracer, description: &str, script: &Heap<*mut JSScript>) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue