Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Switch to using the new rooted!/RootedGuard API for rooting.
  • Loading branch information
eddyb committed Jul 4, 2016
1 parent a77cc99 commit 0db1faf
Show file tree
Hide file tree
Showing 28 changed files with 238 additions and 237 deletions.
22 changes: 11 additions & 11 deletions components/script/devtools.rs
Expand Up @@ -22,7 +22,7 @@ use dom::element::Element;
use dom::node::Node;
use dom::window::Window;
use ipc_channel::ipc::IpcSender;
use js::jsapi::{JSAutoCompartment, ObjectClassName, RootedObject, RootedValue};
use js::jsapi::{JSAutoCompartment, ObjectClassName};
use js::jsval::UndefinedValue;
use msg::constellation_msg::PipelineId;
use script_thread::get_browsing_context;
Expand All @@ -38,24 +38,24 @@ pub fn handle_evaluate_js(global: &GlobalRef, eval: String, reply: IpcSender<Eva
let cx = global.get_cx();
let globalhandle = global.reflector().get_jsobject();
let _ac = JSAutoCompartment::new(cx, globalhandle.get());
let mut rval = RootedValue::new(cx, UndefinedValue());
rooted!(in(cx) let mut rval = UndefinedValue());
global.evaluate_js_on_global_with_result(&eval, rval.handle_mut());

if rval.ptr.is_undefined() {
if rval.is_undefined() {
EvaluateJSReply::VoidValue
} else if rval.ptr.is_boolean() {
EvaluateJSReply::BooleanValue(rval.ptr.to_boolean())
} else if rval.ptr.is_double() || rval.ptr.is_int32() {
} else if rval.is_boolean() {
EvaluateJSReply::BooleanValue(rval.to_boolean())
} else if rval.is_double() || rval.is_int32() {
EvaluateJSReply::NumberValue(FromJSValConvertible::from_jsval(cx, rval.handle(), ())
.unwrap())
} else if rval.ptr.is_string() {
EvaluateJSReply::StringValue(String::from(jsstring_to_str(cx, rval.ptr.to_string())))
} else if rval.ptr.is_null() {
} else if rval.is_string() {
EvaluateJSReply::StringValue(String::from(jsstring_to_str(cx, rval.to_string())))
} else if rval.is_null() {
EvaluateJSReply::NullValue
} else {
assert!(rval.ptr.is_object());
assert!(rval.is_object());

let obj = RootedObject::new(cx, rval.ptr.to_object());
rooted!(in(cx) let obj = rval.to_object());
let class_name = CStr::from_ptr(ObjectClassName(cx, obj.handle()));
let class_name = str::from_utf8(class_name.to_bytes()).unwrap();

Expand Down
35 changes: 17 additions & 18 deletions components/script/dom/bindings/callback.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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())
}
}

Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 0db1faf

Please sign in to comment.