mirror of
https://github.com/servo/servo.git
synced 2025-10-14 15:30:27 +01:00
Remove JS::get/get_mut to enforce sound rooting practices.
This commit is contained in:
parent
d7b96db33c
commit
dfdda0098a
13 changed files with 114 additions and 131 deletions
|
@ -2243,12 +2243,7 @@ class CGCallGenerator(CGThing):
|
|||
self.cgRoot.append(CGGeneric("result = result_fallible.unwrap();"))
|
||||
|
||||
if typeRetValNeedsRooting(returnType):
|
||||
rooted_value = CGGeneric("result.root(&roots).root_ref().unrooted()")
|
||||
if returnType.nullable():
|
||||
rooted_value = CGWrapper(rooted_value, pre="result.map(|result| ", post=")")
|
||||
rooted_value = CGWrapper(rooted_value, pre="let result = ", post=";")
|
||||
|
||||
self.cgRoot.append(rooted_value)
|
||||
self.cgRoot.append(CGGeneric("let result = result.root(&roots);"))
|
||||
|
||||
def define(self):
|
||||
return self.cgRoot.define()
|
||||
|
@ -4321,7 +4316,7 @@ class CGBindingRoot(CGThing):
|
|||
'js::glue::{RUST_JS_NumberValue, RUST_JSID_IS_STRING}',
|
||||
'dom::types::*',
|
||||
'dom::bindings',
|
||||
'dom::bindings::js::{JS, JSRef, RootCollection, RootedReference, Unrooted}',
|
||||
'dom::bindings::js::{JS, JSRef, RootCollection, RootedReference, Unrooted, OptionalRootable}',
|
||||
'dom::bindings::utils::{CreateDOMGlobal, CreateInterfaceObjects2}',
|
||||
'dom::bindings::utils::{ConstantSpec, cx_for_dom_object, Default}',
|
||||
'dom::bindings::utils::{dom_object_slot, DOM_OBJECT_SLOT, DOMClass}',
|
||||
|
@ -5320,19 +5315,6 @@ class GlobalGenRoots():
|
|||
derived += [CGGeneric('\n')]
|
||||
|
||||
cast = [CGGeneric(string.Template('''pub trait ${castTraitName} {
|
||||
#[inline(always)]
|
||||
fn from<T: ${fromBound}>(derived: &JS<T>) -> JS<Self> {
|
||||
unsafe { derived.clone().transmute() }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to<T: ${toBound}+Reflectable>(base: &JS<T>) -> Option<JS<Self>> {
|
||||
match base.get().${checkFn}() {
|
||||
true => unsafe { Some(base.clone().transmute()) },
|
||||
false => None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_ref<'a, 'b, T: ${toBound}+Reflectable>(base: &'a JSRef<'b, T>) -> Option<&'a JSRef<'b, Self>> {
|
||||
match base.get().${checkFn}() {
|
||||
|
@ -5350,19 +5332,16 @@ class GlobalGenRoots():
|
|||
}
|
||||
|
||||
#[inline(always)]
|
||||
unsafe fn to_unchecked<T: ${toBound}+Reflectable>(base: &JS<T>) -> JS<Self> {
|
||||
assert!(base.get().${checkFn}());
|
||||
base.clone().transmute()
|
||||
}
|
||||
|
||||
fn from_ref<'a, 'b, T: ${fromBound}>(derived: &'a JSRef<'b, T>) -> &'a JSRef<'b, Self> {
|
||||
unsafe { derived.transmute() }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn from_mut_ref<'a, 'b, T: ${fromBound}>(derived: &'a mut JSRef<'b, T>) -> &'a mut JSRef<'b, Self> {
|
||||
unsafe { derived.transmute_mut() }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn from_unrooted<T: ${fromBound}+Reflectable>(derived: Unrooted<T>) -> Unrooted<Self> {
|
||||
unsafe { derived.transmute() }
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* 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/. */
|
||||
|
||||
use dom::bindings::js::{JS, JSRef};
|
||||
use dom::bindings::js::{JS, JSRef, Root};
|
||||
use dom::bindings::str::ByteString;
|
||||
use dom::bindings::utils::{Reflectable, Reflector};
|
||||
use dom::bindings::utils::jsstring_to_str;
|
||||
|
@ -316,7 +316,7 @@ impl<T: Reflectable+IDLInterface> FromJSValConvertible<()> for JS<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Reflectable> ToJSValConvertible for JS<T> {
|
||||
impl<'a, 'b, T: Reflectable> ToJSValConvertible for Root<'a, 'b, T> {
|
||||
fn to_jsval(&self, cx: *JSContext) -> JSVal {
|
||||
self.reflector().to_jsval(cx)
|
||||
}
|
||||
|
|
|
@ -106,31 +106,23 @@ impl<T: Reflectable> JS<T> {
|
|||
}
|
||||
}
|
||||
|
||||
//XXXjdm This is disappointing. This only gets called from trace hooks, in theory,
|
||||
// so it's safe to assume that self is rooted and thereby safe to access.
|
||||
impl<T: Reflectable> Reflectable for JS<T> {
|
||||
fn reflector<'a>(&'a self) -> &'a Reflector {
|
||||
self.get().reflector()
|
||||
unsafe {
|
||||
(*self.unsafe_get()).reflector()
|
||||
}
|
||||
}
|
||||
|
||||
fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector {
|
||||
self.get_mut().mut_reflector()
|
||||
unsafe {
|
||||
(*self.unsafe_get()).mut_reflector()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Reflectable> JS<T> {
|
||||
pub fn get<'a>(&'a self) -> &'a T {
|
||||
let borrowed = self.ptr.borrow();
|
||||
unsafe {
|
||||
&**borrowed
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mut<'a>(&'a mut self) -> &'a mut T {
|
||||
let mut borrowed = self.ptr.borrow_mut();
|
||||
unsafe {
|
||||
&mut **borrowed
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.)
|
||||
|
@ -213,6 +205,16 @@ impl<T: Reflectable> OptionalRootable<T> for Option<Unrooted<T>> {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait OptionalRootedRootable<T> {
|
||||
fn root<'a, 'b>(&self, roots: &'a RootCollection) -> Option<Root<'a, 'b, T>>;
|
||||
}
|
||||
|
||||
impl<T: Reflectable> OptionalRootedRootable<T> for Option<JS<T>> {
|
||||
fn root<'a, 'b>(&self, roots: &'a RootCollection) -> Option<Root<'a, 'b, T>> {
|
||||
self.as_ref().map(|inner| inner.root(roots))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ResultRootable<T,U> {
|
||||
fn root<'a, 'b>(self, roots: &'a RootCollection) -> Result<Root<'a, 'b, T>, U>;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
use dom::bindings::codegen::PrototypeList;
|
||||
use dom::bindings::codegen::PrototypeList::MAX_PROTO_CHAIN_LENGTH;
|
||||
use dom::bindings::conversions::{FromJSValConvertible, IDLInterface};
|
||||
use dom::bindings::js::{JS, JSRef, RootCollection, Unrooted};
|
||||
use dom::bindings::js::{JS, JSRef, RootCollection, Unrooted, Root};
|
||||
use dom::bindings::trace::Untraceable;
|
||||
use dom::browsercontext;
|
||||
use dom::window;
|
||||
|
@ -602,13 +602,16 @@ pub extern fn wrap_for_same_compartment(cx: *JSContext, obj: *JSObject) -> *JSOb
|
|||
|
||||
pub extern fn outerize_global(_cx: *JSContext, obj: JSHandleObject) -> *JSObject {
|
||||
unsafe {
|
||||
let roots = RootCollection::new();
|
||||
debug!("outerizing");
|
||||
let obj = *obj.unnamed;
|
||||
let win: JS<window::Window> =
|
||||
let win: Root<window::Window> =
|
||||
unwrap_jsmanaged(obj,
|
||||
IDLInterface::get_prototype_id(None::<window::Window>),
|
||||
IDLInterface::get_prototype_depth(None::<window::Window>)).unwrap();
|
||||
win.get().browser_context.get_ref().window_proxy()
|
||||
IDLInterface::get_prototype_depth(None::<window::Window>))
|
||||
.unwrap()
|
||||
.root(&roots);
|
||||
win.deref().browser_context.get_ref().window_proxy()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue