Remove unnecessary generic from private_from_proto_check.

This commit is contained in:
Josh Matthews 2020-06-04 12:46:25 -04:00
parent 9788c5c7f4
commit 6dc4488bc7
2 changed files with 25 additions and 12 deletions

View file

@ -404,6 +404,11 @@ pub unsafe fn get_dom_class(obj: *mut JSObject) -> Result<&'static DOMClass, ()>
Err(()) Err(())
} }
pub(crate) enum PrototypeCheck {
Derive(fn(&'static DOMClass) -> bool),
Depth { depth: usize, proto_id: u16 },
}
/// Get a `*const libc::c_void` for the given DOM object, unwrapping any /// Get a `*const libc::c_void` for the given DOM object, unwrapping any
/// wrapper around it first, and checking if the object is of the correct type. /// wrapper around it first, and checking if the object is of the correct type.
/// ///
@ -411,14 +416,11 @@ pub unsafe fn get_dom_class(obj: *mut JSObject) -> Result<&'static DOMClass, ()>
/// not an object for a DOM object of the given type (as defined by the /// not an object for a DOM object of the given type (as defined by the
/// proto_id and proto_depth). /// proto_id and proto_depth).
#[inline] #[inline]
pub unsafe fn private_from_proto_check<F>( pub(crate) unsafe fn private_from_proto_check(
mut obj: *mut JSObject, mut obj: *mut JSObject,
cx: *mut JSContext, cx: *mut JSContext,
proto_check: F, proto_check: PrototypeCheck,
) -> Result<*const libc::c_void, ()> ) -> Result<*const libc::c_void, ()> {
where
F: Fn(&'static DOMClass) -> bool,
{
let dom_class = get_dom_class(obj).or_else(|_| { let dom_class = get_dom_class(obj).or_else(|_| {
if IsWrapper(obj) { if IsWrapper(obj) {
trace!("found wrapper"); trace!("found wrapper");
@ -437,7 +439,14 @@ where
} }
})?; })?;
if proto_check(dom_class) { let prototype_matches = match proto_check {
PrototypeCheck::Derive(f) => (f)(dom_class),
PrototypeCheck::Depth { depth, proto_id } => {
dom_class.interface_chain[depth] as u16 == proto_id
},
};
if prototype_matches {
trace!("good prototype"); trace!("good prototype");
Ok(private_from_object(obj)) Ok(private_from_object(obj))
} else { } else {
@ -471,7 +480,10 @@ pub fn native_from_object<T>(obj: *mut JSObject, cx: *mut JSContext) -> Result<*
where where
T: DomObject + IDLInterface, T: DomObject + IDLInterface,
{ {
unsafe { private_from_proto_check(obj, cx, T::derives).map(|ptr| ptr as *const T) } unsafe {
private_from_proto_check(obj, cx, PrototypeCheck::Derive(T::derives))
.map(|ptr| ptr as *const T)
}
} }
/// Get a `*const T` for a DOM object accessible from a `JSObject`, where the DOM object /// Get a `*const T` for a DOM object accessible from a `JSObject`, where the DOM object

View file

@ -7,7 +7,9 @@
use crate::dom::bindings::codegen::InterfaceObjectMap; use crate::dom::bindings::codegen::InterfaceObjectMap;
use crate::dom::bindings::codegen::PrototypeList; use crate::dom::bindings::codegen::PrototypeList;
use crate::dom::bindings::codegen::PrototypeList::{MAX_PROTO_CHAIN_LENGTH, PROTO_OR_IFACE_LENGTH}; use crate::dom::bindings::codegen::PrototypeList::{MAX_PROTO_CHAIN_LENGTH, PROTO_OR_IFACE_LENGTH};
use crate::dom::bindings::conversions::{jsstring_to_str, private_from_proto_check}; use crate::dom::bindings::conversions::{
jsstring_to_str, private_from_proto_check, PrototypeCheck,
};
use crate::dom::bindings::error::throw_invalid_this; use crate::dom::bindings::error::throw_invalid_this;
use crate::dom::bindings::inheritance::TopTypeId; use crate::dom::bindings::inheritance::TopTypeId;
use crate::dom::bindings::str::DOMString; use crate::dom::bindings::str::DOMString;
@ -507,9 +509,8 @@ unsafe fn generic_call(
} else { } else {
GetNonCCWObjectGlobal(JS_CALLEE(cx, vp).to_object_or_null()) GetNonCCWObjectGlobal(JS_CALLEE(cx, vp).to_object_or_null())
}); });
let depth = (*info).__bindgen_anon_3.depth; let depth = (*info).__bindgen_anon_3.depth as usize;
let proto_check = let proto_check = PrototypeCheck::Depth { depth, proto_id };
|class: &'static DOMClass| class.interface_chain[depth as usize] as u16 == proto_id;
let this = match private_from_proto_check(obj.get(), cx, proto_check) { let this = match private_from_proto_check(obj.get(), cx, proto_check) {
Ok(val) => val, Ok(val) => val,
Err(()) => { Err(()) => {