mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
Introduce IDLInterface::derives()
This method is given a DOMClass value and returns whether it derives from Self. Interfaces with no descendants directly check whether the given DOMClass is the same as their own.
This commit is contained in:
parent
7fb3c51bbb
commit
c77d3b965f
3 changed files with 35 additions and 41 deletions
|
@ -2246,20 +2246,24 @@ class CGIDLInterface(CGThing):
|
||||||
self.descriptor = descriptor
|
self.descriptor = descriptor
|
||||||
|
|
||||||
def define(self):
|
def define(self):
|
||||||
replacer = {
|
interface = self.descriptor.interface
|
||||||
'type': self.descriptor.name,
|
name = self.descriptor.name
|
||||||
'depth': self.descriptor.interface.inheritanceDepth(),
|
if (interface.getUserData("hasConcreteDescendant", False) or
|
||||||
}
|
interface.getUserData("hasProxyDescendant", False)):
|
||||||
return string.Template("""\
|
depth = len(self.descriptor.prototypeChain)
|
||||||
impl IDLInterface for ${type} {
|
check = "class.interface_chain[%s] == PrototypeList::ID::%s" % (depth - 1, name)
|
||||||
fn get_prototype_id() -> PrototypeList::ID {
|
elif self.descriptor.proxy:
|
||||||
PrototypeList::ID::${type}
|
check = "class as *const _ == &Class as *const _"
|
||||||
}
|
else:
|
||||||
fn get_prototype_depth() -> usize {
|
check = "class as *const _ == &Class.dom_class as *const _"
|
||||||
${depth}
|
return """\
|
||||||
|
impl IDLInterface for %(name)s {
|
||||||
|
#[inline]
|
||||||
|
fn derives(class: &'static DOMClass) -> bool {
|
||||||
|
%(check)s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
""").substitute(replacer)
|
""" % {'check': check, 'name': name}
|
||||||
|
|
||||||
|
|
||||||
class CGAbstractExternMethod(CGAbstractMethod):
|
class CGAbstractExternMethod(CGAbstractMethod):
|
||||||
|
@ -5820,7 +5824,7 @@ class GlobalGenRoots():
|
||||||
|
|
||||||
descriptors = config.getDescriptors(register=True, isCallback=False)
|
descriptors = config.getDescriptors(register=True, isCallback=False)
|
||||||
imports = [CGGeneric("use dom::types::*;\n"),
|
imports = [CGGeneric("use dom::types::*;\n"),
|
||||||
CGGeneric("use dom::bindings::conversions::get_dom_class;\n"),
|
CGGeneric("use dom::bindings::conversions::{IDLInterface, get_dom_class};\n"),
|
||||||
CGGeneric("use dom::bindings::js::{JS, LayoutJS, Root};\n"),
|
CGGeneric("use dom::bindings::js::{JS, LayoutJS, Root};\n"),
|
||||||
CGGeneric("use dom::bindings::trace::JSTraceable;\n"),
|
CGGeneric("use dom::bindings::trace::JSTraceable;\n"),
|
||||||
CGGeneric("use dom::bindings::utils::Reflectable;\n"),
|
CGGeneric("use dom::bindings::utils::Reflectable;\n"),
|
||||||
|
@ -5886,18 +5890,11 @@ impl %(name)sCast {
|
||||||
# Define a `FooDerived` trait for superclasses to implement,
|
# Define a `FooDerived` trait for superclasses to implement,
|
||||||
# as well as the `FooCast::to_*` methods that use it.
|
# as well as the `FooCast::to_*` methods that use it.
|
||||||
baseName = descriptor.prototypeChain[0]
|
baseName = descriptor.prototypeChain[0]
|
||||||
typeIdPat = descriptor.prototypeChain[-1]
|
|
||||||
if upcast:
|
|
||||||
typeIdPat += "(_)"
|
|
||||||
for base in reversed(descriptor.prototypeChain[1:-1]):
|
|
||||||
typeIdPat = "%s(%sTypeId::%s)" % (base, base, typeIdPat)
|
|
||||||
typeIdPat = "%sTypeId::%s" % (baseName, typeIdPat)
|
|
||||||
args = {
|
args = {
|
||||||
'baseName': baseName,
|
'baseName': baseName,
|
||||||
'derivedTrait': name + 'Derived',
|
'derivedTrait': name + 'Derived',
|
||||||
'methodName': 'is_' + name.lower(),
|
'methodName': 'is_' + name.lower(),
|
||||||
'name': name,
|
'name': name,
|
||||||
'typeIdPat': typeIdPat,
|
|
||||||
}
|
}
|
||||||
allprotos.append(CGGeneric("""\
|
allprotos.append(CGGeneric("""\
|
||||||
/// Types which `%(name)s` derives from
|
/// Types which `%(name)s` derives from
|
||||||
|
@ -5938,10 +5935,10 @@ impl %(name)sCast {
|
||||||
|
|
||||||
impl %(derivedTrait)s for %(baseName)s {
|
impl %(derivedTrait)s for %(baseName)s {
|
||||||
fn %(methodName)s(&self) -> bool {
|
fn %(methodName)s(&self) -> bool {
|
||||||
match *self.type_id() {
|
let dom_class = unsafe {
|
||||||
%(typeIdPat)s => true,
|
get_dom_class(self.reflector().get_jsobject().get()).unwrap()
|
||||||
_ => false,
|
};
|
||||||
}
|
%(name)s::derives(dom_class)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,6 @@
|
||||||
//! | union types | `T` |
|
//! | union types | `T` |
|
||||||
|
|
||||||
use core::nonzero::NonZero;
|
use core::nonzero::NonZero;
|
||||||
use dom::bindings::codegen::PrototypeList;
|
|
||||||
use dom::bindings::error::throw_type_error;
|
use dom::bindings::error::throw_type_error;
|
||||||
use dom::bindings::js::Root;
|
use dom::bindings::js::Root;
|
||||||
use dom::bindings::num::Finite;
|
use dom::bindings::num::Finite;
|
||||||
|
@ -101,14 +100,10 @@ impl_as!(u32, u32);
|
||||||
impl_as!(i64, i64);
|
impl_as!(i64, i64);
|
||||||
impl_as!(u64, u64);
|
impl_as!(u64, u64);
|
||||||
|
|
||||||
/// A trait to retrieve the constants necessary to check if a `JSObject`
|
/// A trait to check whether a given `JSObject` implements an IDL interface.
|
||||||
/// implements a given interface.
|
|
||||||
pub trait IDLInterface {
|
pub trait IDLInterface {
|
||||||
/// Returns the prototype ID.
|
/// Returns whether the given DOM class derives that interface.
|
||||||
fn get_prototype_id() -> PrototypeList::ID;
|
fn derives(&'static DOMClass) -> bool;
|
||||||
/// Returns the prototype depth, i.e., the number of interfaces this
|
|
||||||
/// interface inherits from.
|
|
||||||
fn get_prototype_depth() -> usize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A trait to convert Rust types to `JSVal`s.
|
/// A trait to convert Rust types to `JSVal`s.
|
||||||
|
@ -696,9 +691,10 @@ pub unsafe fn get_dom_class(obj: *mut JSObject) -> Result<&'static DOMClass, ()>
|
||||||
/// Returns Err(()) if `obj` is an opaque security wrapper or if the object is
|
/// Returns Err(()) if `obj` is an opaque security wrapper or if the object is
|
||||||
/// 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).
|
||||||
pub unsafe fn private_from_proto_chain(mut obj: *mut JSObject,
|
#[inline]
|
||||||
proto_id: u16, proto_depth: u16)
|
pub unsafe fn private_from_proto_check<F>(mut obj: *mut JSObject, proto_check: F)
|
||||||
-> Result<*const libc::c_void, ()> {
|
-> Result<*const libc::c_void, ()>
|
||||||
|
where F: Fn(&'static DOMClass) -> bool {
|
||||||
let dom_class = try!(get_dom_class(obj).or_else(|_| {
|
let dom_class = try!(get_dom_class(obj).or_else(|_| {
|
||||||
if IsWrapper(obj) {
|
if IsWrapper(obj) {
|
||||||
debug!("found wrapper");
|
debug!("found wrapper");
|
||||||
|
@ -717,7 +713,7 @@ pub unsafe fn private_from_proto_chain(mut obj: *mut JSObject,
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if dom_class.interface_chain[proto_depth as usize] as u16 == proto_id {
|
if proto_check(dom_class) {
|
||||||
debug!("good prototype");
|
debug!("good prototype");
|
||||||
Ok(private_from_reflector(obj))
|
Ok(private_from_reflector(obj))
|
||||||
} else {
|
} else {
|
||||||
|
@ -735,10 +731,8 @@ pub unsafe fn private_from_proto_chain(mut obj: *mut JSObject,
|
||||||
pub fn native_from_reflector_jsmanaged<T>(obj: *mut JSObject) -> Result<Root<T>, ()>
|
pub fn native_from_reflector_jsmanaged<T>(obj: *mut JSObject) -> Result<Root<T>, ()>
|
||||||
where T: Reflectable + IDLInterface
|
where T: Reflectable + IDLInterface
|
||||||
{
|
{
|
||||||
let proto_id = <T as IDLInterface>::get_prototype_id() as u16;
|
|
||||||
let proto_depth = <T as IDLInterface>::get_prototype_depth() as u16;
|
|
||||||
unsafe {
|
unsafe {
|
||||||
private_from_proto_chain(obj, proto_id, proto_depth).map(|obj| {
|
private_from_proto_check(obj, T::derives).map(|obj| {
|
||||||
Root::new(NonZero::new(obj as *const T))
|
Root::new(NonZero::new(obj as *const T))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ use dom::bindings::codegen::InheritTypes::TopTypeId;
|
||||||
use dom::bindings::codegen::PrototypeList;
|
use dom::bindings::codegen::PrototypeList;
|
||||||
use dom::bindings::codegen::PrototypeList::MAX_PROTO_CHAIN_LENGTH;
|
use dom::bindings::codegen::PrototypeList::MAX_PROTO_CHAIN_LENGTH;
|
||||||
use dom::bindings::conversions::native_from_handleobject;
|
use dom::bindings::conversions::native_from_handleobject;
|
||||||
use dom::bindings::conversions::private_from_proto_chain;
|
use dom::bindings::conversions::private_from_proto_check;
|
||||||
use dom::bindings::conversions::{is_dom_class, jsstring_to_str};
|
use dom::bindings::conversions::{is_dom_class, jsstring_to_str};
|
||||||
use dom::bindings::error::throw_type_error;
|
use dom::bindings::error::throw_type_error;
|
||||||
use dom::bindings::error::{Error, ErrorResult, Fallible, throw_invalid_this};
|
use dom::bindings::error::{Error, ErrorResult, Fallible, throw_invalid_this};
|
||||||
|
@ -755,7 +755,10 @@ unsafe fn generic_call(cx: *mut JSContext, argc: libc::c_uint, vp: *mut JSVal,
|
||||||
let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));
|
let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));
|
||||||
let proto_id = (*info).protoID;
|
let proto_id = (*info).protoID;
|
||||||
let depth = (*info).depth;
|
let depth = (*info).depth;
|
||||||
let this = match private_from_proto_chain(obj.ptr, proto_id, depth) {
|
let proto_check = |class: &'static DOMClass| {
|
||||||
|
class.interface_chain[depth as usize] as u16 == proto_id
|
||||||
|
};
|
||||||
|
let this = match private_from_proto_check(obj.ptr, proto_check) {
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
Err(()) => {
|
Err(()) => {
|
||||||
if is_lenient {
|
if is_lenient {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue