diff options
Diffstat (limited to 'components/script/dom/bindings/reflector.rs')
-rw-r--r-- | components/script/dom/bindings/reflector.rs | 67 |
1 files changed, 44 insertions, 23 deletions
diff --git a/components/script/dom/bindings/reflector.rs b/components/script/dom/bindings/reflector.rs index 896cf74a34f..04bf9fdbbed 100644 --- a/components/script/dom/bindings/reflector.rs +++ b/components/script/dom/bindings/reflector.rs @@ -1,37 +1,37 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ //! The `Reflector` struct. -use dom::bindings::conversions::DerivedFrom; -use dom::bindings::js::Root; -use dom::globalscope::GlobalScope; -use js::jsapi::{HandleObject, JSContext, JSObject, Heap}; +use crate::dom::bindings::conversions::DerivedFrom; +use crate::dom::bindings::iterable::{Iterable, IterableIterator}; +use crate::dom::bindings::root::{Dom, DomRoot, Root}; +use crate::dom::bindings::trace::JSTraceable; +use crate::dom::globalscope::GlobalScope; +use crate::script_runtime::JSContext; +use js::jsapi::{Heap, JSObject}; +use js::rust::HandleObject; use std::default::Default; /// Create the reflector for a new DOM object and yield ownership to the /// reflector. -pub fn reflect_dom_object<T, U>( - obj: Box<T>, - global: &U, - wrap_fn: unsafe fn(*mut JSContext, &GlobalScope, Box<T>) -> Root<T>) - -> Root<T> - where T: DomObject, U: DerivedFrom<GlobalScope> +pub fn reflect_dom_object<T, U>(obj: Box<T>, global: &U) -> DomRoot<T> +where + T: DomObject + DomObjectWrap, + U: DerivedFrom<GlobalScope>, { let global_scope = global.upcast(); - unsafe { - wrap_fn(global_scope.get_cx(), global_scope, obj) - } + unsafe { T::WRAP(global_scope.get_cx(), global_scope, obj) } } /// A struct to store a reference to the reflector of a DOM object. #[allow(unrooted_must_root)] -#[derive(HeapSizeOf)] -#[must_root] +#[derive(MallocSizeOf)] +#[unrooted_must_root_lint::must_root] // If you're renaming or moving this field, update the path in plugins::reflector as well pub struct Reflector { - #[ignore_heap_size_of = "defined and measured in rust-mozjs"] + #[ignore_malloc_size_of = "defined and measured in rust-mozjs"] object: Heap<*mut JSObject>, } @@ -46,11 +46,12 @@ impl Reflector { /// Get the reflector. #[inline] pub fn get_jsobject(&self) -> HandleObject { - self.object.handle() + // 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.) - pub fn set_jsobject(&mut self, object: *mut JSObject) { + pub unsafe fn set_jsobject(&self, object: *mut JSObject) { assert!(self.object.get().is_null()); assert!(!object.is_null()); self.object.set(object); @@ -72,12 +73,15 @@ impl Reflector { } /// A trait to provide access to the `Reflector` for a DOM object. -pub trait DomObject { +pub trait DomObject: JSTraceable + 'static { /// Returns the receiver's reflector. fn reflector(&self) -> &Reflector; /// Returns the global scope of the realm that the DomObject was created in. - fn global(&self) -> Root<GlobalScope> where Self: Sized { + fn global(&self) -> DomRoot<GlobalScope> + where + Self: Sized, + { GlobalScope::from_reflector(self) } } @@ -91,11 +95,28 @@ impl DomObject for Reflector { /// A trait to initialize the `Reflector` for a DOM object. pub trait MutDomObject: DomObject { /// Initializes the Reflector - fn init_reflector(&mut self, obj: *mut JSObject); + unsafe fn init_reflector(&self, obj: *mut JSObject); } impl MutDomObject for Reflector { - fn init_reflector(&mut self, obj: *mut JSObject) { + unsafe fn init_reflector(&self, obj: *mut JSObject) { self.set_jsobject(obj) } } + +/// A trait to provide a function pointer to wrap function for DOM objects. +pub trait DomObjectWrap: Sized + DomObject { + /// Function pointer to the general wrap function type + const WRAP: unsafe fn(JSContext, &GlobalScope, Box<Self>) -> Root<Dom<Self>>; +} + +/// A trait to provide a function pointer to wrap function for +/// DOM iterator interfaces. +pub trait DomObjectIteratorWrap: DomObjectWrap + JSTraceable + Iterable { + /// Function pointer to the wrap function for IterableIterator<T> + const ITER_WRAP: unsafe fn( + JSContext, + &GlobalScope, + Box<IterableIterator<Self>>, + ) -> Root<Dom<IterableIterator<Self>>>; +} |