Switch to using the new rooted!/RootedGuard API for rooting.

This commit is contained in:
Eduard Burtescu 2016-07-04 20:59:01 +03:00
parent a77cc9950f
commit 0db1faf876
28 changed files with 238 additions and 237 deletions

View file

@ -9,14 +9,14 @@ use dom::bindings::global::global_root_from_object;
use dom::bindings::reflector::Reflectable;
use js::jsapi::GetGlobalForObjectCrossCompartment;
use js::jsapi::JSAutoCompartment;
use js::jsapi::{Heap, MutableHandleObject, RootedObject, RootedValue};
use js::jsapi::{Heap, MutableHandleObject, RootedObject};
use js::jsapi::{IsCallable, JSContext, JSObject, JS_WrapObject};
use js::jsapi::{JSCompartment, JS_EnterCompartment, JS_LeaveCompartment};
use js::jsapi::{JS_GetProperty, JS_IsExceptionPending, JS_ReportPendingException};
use js::jsval::{JSVal, UndefinedValue};
use js::rust::RootedGuard;
use std::default::Default;
use std::ffi::CString;
use std::intrinsics::return_address;
use std::ptr;
use std::rc::Rc;
@ -114,20 +114,20 @@ impl CallbackInterface {
/// Returns the property with the given `name`, if it is a callable object,
/// or an error otherwise.
pub fn get_callable_property(&self, cx: *mut JSContext, name: &str) -> Fallible<JSVal> {
let mut callable = RootedValue::new(cx, UndefinedValue());
let obj = RootedObject::new(cx, self.callback());
rooted!(in(cx) let mut callable = UndefinedValue());
rooted!(in(cx) let obj = self.callback());
unsafe {
let c_name = CString::new(name).unwrap();
if !JS_GetProperty(cx, obj.handle(), c_name.as_ptr(), callable.handle_mut()) {
return Err(Error::JSFailed);
}
if !callable.ptr.is_object() || !IsCallable(callable.ptr.to_object()) {
if !callable.is_object() || !IsCallable(callable.to_object()) {
return Err(Error::Type(format!("The value of the {} property is not callable",
name)));
}
}
Ok(callable.ptr)
Ok(callable.get())
}
}
@ -147,11 +147,9 @@ pub fn wrap_call_this_object<T: Reflectable>(cx: *mut JSContext,
/// A class that performs whatever setup we need to safely make a call while
/// this class is on the stack. After `new` returns, the call is safe to make.
pub struct CallSetup {
pub struct CallSetup<'a> {
/// The compartment for reporting exceptions.
/// As a RootedObject, this must be the first field in order to
/// determine the final address on the stack correctly.
exception_compartment: RootedObject,
exception_compartment: RootedGuard<'a, *mut JSObject>,
/// The `JSContext` used for the call.
cx: *mut JSContext,
/// The compartment we were in before the call.
@ -160,20 +158,21 @@ pub struct CallSetup {
handling: ExceptionHandling,
}
impl CallSetup {
impl<'a> CallSetup<'a> {
/// Performs the setup needed to make a call.
#[allow(unrooted_must_root)]
pub fn new<T: CallbackContainer>(callback: &T, handling: ExceptionHandling) -> CallSetup {
pub fn new<T: CallbackContainer>(exception_compartment: &'a mut RootedObject,
callback: &T,
handling: ExceptionHandling)
-> CallSetup<'a> {
let global = unsafe { global_root_from_object(callback.callback()) };
let cx = global.r().get_cx();
let exception_compartment = unsafe {
exception_compartment.ptr = unsafe {
GetGlobalForObjectCrossCompartment(callback.callback())
};
CallSetup {
exception_compartment: RootedObject::new_with_addr(cx,
exception_compartment,
unsafe { return_address() }),
exception_compartment: RootedGuard::new(cx, exception_compartment),
cx: cx,
old_compartment: unsafe { JS_EnterCompartment(cx, callback.callback()) },
handling: handling,
@ -186,7 +185,7 @@ impl CallSetup {
}
}
impl Drop for CallSetup {
impl<'a> Drop for CallSetup<'a> {
fn drop(&mut self) {
unsafe {
JS_LeaveCompartment(self.cx, self.old_compartment);
@ -195,7 +194,7 @@ impl Drop for CallSetup {
unsafe { JS_IsExceptionPending(self.cx) };
if need_to_deal_with_exception {
unsafe {
let _ac = JSAutoCompartment::new(self.cx, self.exception_compartment.ptr);
let _ac = JSAutoCompartment::new(self.cx, *self.exception_compartment);
JS_ReportPendingException(self.cx);
}
}