Clean up the conversion routines

Functions returning `Root<T>` are prefixed by "root_" and the ones returning
`*const T` by "native_".

Functions taking `*mut JSObject` are now suffixed by "_from_object" and the ones
taking `&T` by "_from_reflector".
This commit is contained in:
Anthony Ramine 2015-10-25 11:11:23 +01:00
parent aa105d89b4
commit b290a3161d
10 changed files with 45 additions and 61 deletions

View file

@ -32,7 +32,6 @@
//! | sequences | `Vec<T>` |
//! | union types | `T` |
use core::nonzero::NonZero;
use dom::bindings::error::throw_type_error;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::Root;
@ -657,7 +656,7 @@ pub fn is_dom_proxy(obj: *mut JSObject) -> bool {
pub const DOM_OBJECT_SLOT: u32 = 0;
/// Get the private pointer of a DOM object from a given reflector.
unsafe fn private_from_reflector(obj: *mut JSObject) -> *const libc::c_void {
pub unsafe fn private_from_object(obj: *mut JSObject) -> *const libc::c_void {
let clasp = JS_GetClass(obj);
let value = if is_dom_class(clasp) {
JS_GetReservedSlot(obj, DOM_OBJECT_SLOT)
@ -672,11 +671,6 @@ unsafe fn private_from_reflector(obj: *mut JSObject) -> *const libc::c_void {
}
}
/// Get the DOM object from the given reflector.
pub unsafe fn native_from_reflector<T>(obj: *mut JSObject) -> *const T {
private_from_reflector(obj) as *const T
}
/// Get the `DOMClass` from `obj`, or `Err(())` if `obj` is not a DOM object.
pub unsafe fn get_dom_class(obj: *mut JSObject) -> Result<&'static DOMClass, ()> {
use dom::bindings::utils::DOMJSClass;
@ -727,7 +721,7 @@ pub unsafe fn private_from_proto_check<F>(mut obj: *mut JSObject, proto_check: F
if proto_check(dom_class) {
debug!("good prototype");
Ok(private_from_reflector(obj))
Ok(private_from_object(obj))
} else {
debug!("bad prototype");
Err(())
@ -740,28 +734,28 @@ pub unsafe fn private_from_proto_check<F>(mut obj: *mut JSObject, proto_check: F
/// Returns Err(()) if `obj` is an opaque security wrapper or if the object is
/// not a reflector for a DOM object of the given type (as defined by the
/// proto_id and proto_depth).
pub fn native_from_reflector_jsmanaged<T>(obj: *mut JSObject) -> Result<Root<T>, ()>
pub fn root_from_object<T>(obj: *mut JSObject) -> Result<Root<T>, ()>
where T: Reflectable + IDLInterface
{
unsafe {
private_from_proto_check(obj, T::derives).map(|obj| {
Root::new(NonZero::new(obj as *const T))
private_from_proto_check(obj, T::derives).map(|ptr| {
Root::from_ref(&*(ptr as *const T))
})
}
}
/// Get a Rooted<T> for a DOM object accessible from a HandleValue
pub fn native_from_handlevalue<T>(v: HandleValue) -> Result<Root<T>, ()>
/// Get a `Root<T>` for a DOM object accessible from a `HandleValue`.
pub fn root_from_handlevalue<T>(v: HandleValue) -> Result<Root<T>, ()>
where T: Reflectable + IDLInterface
{
native_from_reflector_jsmanaged(v.get().to_object())
root_from_object(v.get().to_object())
}
/// Get a Rooted<T> for a DOM object accessible from a HandleObject
pub fn native_from_handleobject<T>(obj: HandleObject) -> Result<Root<T>, ()>
/// Get a `Root<T>` for a DOM object accessible from a `HandleObject`.
pub fn root_from_handleobject<T>(obj: HandleObject) -> Result<Root<T>, ()>
where T: Reflectable + IDLInterface
{
native_from_reflector_jsmanaged(obj.get())
root_from_object(obj.get())
}
impl<T: Reflectable> ToJSValConvertible for Root<T> {