auto merge of #4613 : saneyuki/servo/layoutjs, r=jdm

#4571
This commit is contained in:
bors-servo 2015-01-31 16:24:48 -07:00
commit 462940fc2a
14 changed files with 162 additions and 87 deletions

View file

@ -33,8 +33,9 @@ In the `_finalize()` function the pointer of the Rusty DOM object that is contai
For supporting SpiderMonkeys exact GC rooting, we introduce [some types](https://github.com/mozilla/servo/wiki/Using-DOM-types):
- `JS<T>` is used for the DOM typed field in a DOM type structure. The GC can trace them recursively while the enclosing DOM object (maybe root) is alive.
- `LayoutJS<T>` is specialized `JS<T>` to use in layout. `Layout*Helper` must be implemented on this type to prevent calling methods from non layout code.
- `Temporary<T>` is used as a return value for functions returning a DOM type. They are rooted for the duration of their lifetime. But a retun value gets moved around which can break the LIFO ordering constraint. Thus we need to introduce `Root<T>`.
- `Root<T>` contains the pointer to `JSObject` which the represented DOM type has. SpiderMonkey's conservative stack scanner scans it's pointers and marks a pointed `JSObject` as GC root.
- `JSRef` is just a reference to the value rooted by `Root<T>`.
- `RootCollection` is used to dynamically check if rooting satisfies LIFO ordering, because SpiderMonkey's GC requires LIFO order (See also: [Exact Stack Rooting - Storing a GCPointer on the CStack](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Internals/GC/Exact_Stack_Rooting)).
- `MutHeap<T>` is a version of `Cell<T>` that is safe to use for internal mutability of Spidermonkey heap objects like `JSVal` and `JS<T>`
- `MutHeap<T>` is a version of `Cell<T>` that is safe to use for internal mutability of Spidermonkey heap objects like `JSVal` and `JS<T>`

View file

@ -5220,7 +5220,7 @@ class GlobalGenRoots():
descriptors = config.getDescriptors(register=True, isCallback=False)
allprotos = [CGGeneric("#![allow(unused_imports)]\n"),
CGGeneric("use dom::types::*;\n"),
CGGeneric("use dom::bindings::js::{JS, JSRef, Temporary};\n"),
CGGeneric("use dom::bindings::js::{JS, JSRef, LayoutJS, Temporary};\n"),
CGGeneric("use dom::bindings::trace::JSTraceable;\n"),
CGGeneric("use dom::bindings::utils::Reflectable;\n"),
CGGeneric("use js::jsapi::JSTracer;\n\n"),
@ -5279,6 +5279,17 @@ pub trait ${castTraitName} : Sized {
}
}
#[inline(always)]
#[allow(unrooted_must_root)]
fn to_layout_js<T: ${toBound}+Reflectable>(base: &LayoutJS<T>) -> Option<LayoutJS<Self>> {
unsafe {
match (*base.unsafe_get()).${checkFn}() {
true => Some(base.transmute_copy()),
false => None
}
}
}
#[inline(always)]
fn from_ref<'a, T: ${fromBound}+Reflectable>(derived: JSRef<'a, T>) -> JSRef<'a, Self> {
unsafe { derived.transmute() }

View file

@ -129,8 +129,32 @@ pub struct JS<T> {
ptr: NonZero<*const T>
}
impl<T> JS<T> {
/// Returns `LayoutJS<T>` containing the same pointer.
fn to_layout(self) -> LayoutJS<T> {
LayoutJS {
ptr: self.ptr.clone()
}
}
}
/// This is specialized `JS<T>` to use in under `layout` crate.
/// `Layout*Helpers` traits must be implemented on this.
pub struct LayoutJS<T> {
ptr: NonZero<*const T>
}
impl<T: Reflectable> LayoutJS<T> {
/// Get the reflector.
pub unsafe fn get_jsobject(&self) -> *mut JSObject {
(**self.ptr).reflector().get_jsobject()
}
}
impl<T> Copy for JS<T> {}
impl<T> Copy for LayoutJS<T> {}
impl<T> PartialEq for JS<T> {
#[allow(unrooted_must_root)]
fn eq(&self, other: &JS<T>) -> bool {
@ -138,6 +162,13 @@ impl<T> PartialEq for JS<T> {
}
}
impl<T> PartialEq for LayoutJS<T> {
#[allow(unrooted_must_root)]
fn eq(&self, other: &LayoutJS<T>) -> bool {
self.ptr == other.ptr
}
}
impl <T> Clone for JS<T> {
#[inline]
fn clone(&self) -> JS<T> {
@ -147,6 +178,15 @@ impl <T> Clone for JS<T> {
}
}
impl <T> Clone for LayoutJS<T> {
#[inline]
fn clone(&self) -> LayoutJS<T> {
LayoutJS {
ptr: self.ptr.clone()
}
}
}
impl JS<Node> {
/// Create a new JS-owned value wrapped from an address known to be a `Node` pointer.
pub unsafe fn from_trusted_node_address(inner: TrustedNodeAddress) -> JS<Node> {
@ -158,6 +198,16 @@ impl JS<Node> {
}
}
impl LayoutJS<Node> {
/// Create a new JS-owned value wrapped from an address known to be a `Node` pointer.
pub unsafe fn from_trusted_node_address(inner: TrustedNodeAddress) -> LayoutJS<Node> {
let TrustedNodeAddress(addr) = inner;
LayoutJS {
ptr: NonZero::new(addr as *const Node)
}
}
}
impl<T: Reflectable> JS<T> {
/// Create a new JS-owned value wrapped from a raw Rust pointer.
pub unsafe fn from_raw(raw: *const T) -> JS<T> {
@ -296,6 +346,12 @@ impl<T: Reflectable> MutNullableJS<T> {
self.ptr.get()
}
/// Retrieve a copy of the inner optional `JS<T>` as `LayoutJS<T>`.
/// For use by layout, which can't use safe types like Temporary.
pub unsafe fn get_inner_as_layout(&self) -> Option<LayoutJS<T>> {
self.get_inner().map(|js| js.to_layout())
}
/// Retrieve a copy of the current inner value. If it is `None`, it is
/// initialized with the result of `cb` first.
pub fn or_init<F>(&self, cb: F) -> Temporary<T>
@ -313,9 +369,8 @@ impl<T: Reflectable> MutNullableJS<T> {
}
impl<T: Reflectable> JS<T> {
/// Returns an unsafe pointer to the interior of this object. This is the
/// only method that be safely accessed from layout. (The fact that this is
/// unsafe is what necessitates the layout wrappers.)
/// Returns an unsafe pointer to the interior of this object.
/// This should only be used by the DOM bindings.
pub unsafe fn unsafe_get(&self) -> *const T {
*self.ptr
}
@ -328,19 +383,28 @@ impl<T: Reflectable> JS<T> {
}
}
impl<From> JS<From> {
/// Return `self` as a `JS` of another type.
//XXXjdm It would be lovely if this could be private.
pub unsafe fn transmute<To>(self) -> JS<To> {
mem::transmute(self)
impl<T: Reflectable> LayoutJS<T> {
/// Returns an unsafe pointer to the interior of this JS object without touching the borrow
/// flags. This is the only method that be safely accessed from layout. (The fact that this
/// is unsafe is what necessitates the layout wrappers.)
pub unsafe fn unsafe_get(&self) -> *const T {
*self.ptr
}
}
impl<From> JS<From> {
/// Return `self` as a `JS` of another type.
pub unsafe fn transmute_copy<To>(&self) -> JS<To> {
mem::transmute_copy(self)
}
}
impl<From> LayoutJS<From> {
/// Return `self` as a `LayoutJS` of another type.
pub unsafe fn transmute_copy<To>(&self) -> LayoutJS<To> {
mem::transmute_copy(self)
}
}
/// Get an `Option<JSRef<T>>` out of an `Option<Root<T>>`
pub trait RootedReference<T> {