From 6dc4488bc7bee559021b8d0d780f0734d7cf5723 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Thu, 4 Jun 2020 12:46:25 -0400 Subject: [PATCH] Remove unnecessary generic from private_from_proto_check. --- components/script/dom/bindings/conversions.rs | 28 +++++++++++++------ components/script/dom/bindings/utils.rs | 9 +++--- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/components/script/dom/bindings/conversions.rs b/components/script/dom/bindings/conversions.rs index da4334207bf..97bd3338d8d 100644 --- a/components/script/dom/bindings/conversions.rs +++ b/components/script/dom/bindings/conversions.rs @@ -404,6 +404,11 @@ pub unsafe fn get_dom_class(obj: *mut JSObject) -> Result<&'static DOMClass, ()> 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 /// 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 /// proto_id and proto_depth). #[inline] -pub unsafe fn private_from_proto_check( +pub(crate) unsafe fn private_from_proto_check( mut obj: *mut JSObject, cx: *mut JSContext, - proto_check: F, -) -> Result<*const libc::c_void, ()> -where - F: Fn(&'static DOMClass) -> bool, -{ + proto_check: PrototypeCheck, +) -> Result<*const libc::c_void, ()> { let dom_class = get_dom_class(obj).or_else(|_| { if IsWrapper(obj) { 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"); Ok(private_from_object(obj)) } else { @@ -471,7 +480,10 @@ pub fn native_from_object(obj: *mut JSObject, cx: *mut JSContext) -> Result<* where 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 diff --git a/components/script/dom/bindings/utils.rs b/components/script/dom/bindings/utils.rs index 80217fea8fb..be513aa0244 100644 --- a/components/script/dom/bindings/utils.rs +++ b/components/script/dom/bindings/utils.rs @@ -7,7 +7,9 @@ use crate::dom::bindings::codegen::InterfaceObjectMap; use crate::dom::bindings::codegen::PrototypeList; 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::inheritance::TopTypeId; use crate::dom::bindings::str::DOMString; @@ -507,9 +509,8 @@ unsafe fn generic_call( } else { GetNonCCWObjectGlobal(JS_CALLEE(cx, vp).to_object_or_null()) }); - let depth = (*info).__bindgen_anon_3.depth; - let proto_check = - |class: &'static DOMClass| class.interface_chain[depth as usize] as u16 == proto_id; + let depth = (*info).__bindgen_anon_3.depth as usize; + let proto_check = PrototypeCheck::Depth { depth, proto_id }; let this = match private_from_proto_check(obj.get(), cx, proto_check) { Ok(val) => val, Err(()) => {