Extract bodies of DefineDOMInterface and GetProtoObject/GetConstructorObject out of generated bindings.

This commit is contained in:
Josh Matthews 2023-05-26 01:02:38 -04:00
parent 4998c65c42
commit 747a99d3f6
2 changed files with 62 additions and 30 deletions

View file

@ -3493,21 +3493,8 @@ class CGGetPerInterfaceObject(CGAbstractMethod):
def definition_body(self): def definition_body(self):
return CGGeneric(""" return CGGeneric("""
unsafe { get_per_interface_object_handle(cx, global, %s as usize, CreateInterfaceObjects, rval)
assert!(((*get_object_class(global.get())).flags & JSCLASS_DOM_GLOBAL) != 0); """ % self.id)
/* Check to see whether the interface objects are already installed */
let proto_or_iface_array = get_proto_or_iface_array(global.get());
rval.set((*proto_or_iface_array)[%(id)s as usize]);
if !rval.get().is_null() {
return;
}
CreateInterfaceObjects(cx, global, proto_or_iface_array);
rval.set((*proto_or_iface_array)[%(id)s as usize]);
assert!(!rval.get().is_null());
}
""" % {"id": self.id})
class CGGetProtoObjectMethod(CGGetPerInterfaceObject): class CGGetProtoObjectMethod(CGGetPerInterfaceObject):
@ -3655,25 +3642,19 @@ class CGDefineDOMInterfaceMethod(CGAbstractMethod):
] ]
CGAbstractMethod.__init__(self, descriptor, 'DefineDOMInterface', CGAbstractMethod.__init__(self, descriptor, 'DefineDOMInterface',
'void', args, pub=True) 'void', args, pub=True)
if self.descriptor.interface.isCallback() or self.descriptor.interface.isNamespace():
idPrefix = "PrototypeList::Constructor"
else:
idPrefix = "PrototypeList::ID"
self.id = idPrefix + "::" + MakeNativeName(self.descriptor.name)
def define(self): def define(self):
return CGAbstractMethod.define(self) return CGAbstractMethod.define(self)
def definition_body(self): def definition_body(self):
if self.descriptor.interface.isCallback() or self.descriptor.interface.isNamespace(): return CGGeneric("""
function = "GetConstructorObject" define_dom_interface(cx, global, %s as usize, CreateInterfaceObjects, ConstructorEnabled)
else: """ % self.id)
function = "GetProtoObject"
return CGGeneric("""\
assert!(!global.get().is_null());
if !ConstructorEnabled(cx, global) {
return;
}
rooted!(in(*cx) let mut proto = ptr::null_mut::<JSObject>());
%s(cx, global, proto.handle_mut());
assert!(!proto.is_null());""" % (function,))
def needCx(returnType, arguments, considerTypes): def needCx(returnType, arguments, considerTypes):
@ -6453,6 +6434,8 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
'crate::dom::bindings::interface::define_guarded_methods', 'crate::dom::bindings::interface::define_guarded_methods',
'crate::dom::bindings::interface::define_guarded_properties', 'crate::dom::bindings::interface::define_guarded_properties',
'crate::dom::bindings::interface::is_exposed_in', 'crate::dom::bindings::interface::is_exposed_in',
'crate::dom::bindings::interface::get_per_interface_object_handle',
'crate::dom::bindings::interface::define_dom_interface',
'crate::dom::bindings::htmlconstructor::pop_current_element_queue', 'crate::dom::bindings::htmlconstructor::pop_current_element_queue',
'crate::dom::bindings::htmlconstructor::push_new_element_queue', 'crate::dom::bindings::htmlconstructor::push_new_element_queue',
'crate::dom::bindings::iterable::Iterable', 'crate::dom::bindings::iterable::Iterable',

View file

@ -10,7 +10,9 @@ use crate::dom::bindings::constant::{define_constants, ConstantSpec};
use crate::dom::bindings::conversions::{get_dom_class, DOM_OBJECT_SLOT}; use crate::dom::bindings::conversions::{get_dom_class, DOM_OBJECT_SLOT};
use crate::dom::bindings::guard::Guard; use crate::dom::bindings::guard::Guard;
use crate::dom::bindings::principals::ServoJSPrincipals; use crate::dom::bindings::principals::ServoJSPrincipals;
use crate::dom::bindings::utils::{ProtoOrIfaceArray, DOM_PROTOTYPE_SLOT}; use crate::dom::bindings::utils::{
get_proto_or_iface_array, ProtoOrIfaceArray, DOM_PROTOTYPE_SLOT, JSCLASS_DOM_GLOBAL
};
use crate::script_runtime::JSContext as SafeJSContext; use crate::script_runtime::JSContext as SafeJSContext;
use js::error::throw_type_error; use js::error::throw_type_error;
use js::glue::UncheckedUnwrapObject; use js::glue::UncheckedUnwrapObject;
@ -534,3 +536,50 @@ unsafe extern "C" fn non_new_constructor(
throw_type_error(cx, "This constructor needs to be called with `new`."); throw_type_error(cx, "This constructor needs to be called with `new`.");
false false
} }
pub fn get_per_interface_object_handle(
cx: SafeJSContext,
global: HandleObject,
id: usize,
creator: unsafe fn(SafeJSContext, HandleObject, *mut ProtoOrIfaceArray),
mut rval: MutableHandleObject,
) {
unsafe {
assert!(((*get_object_class(global.get())).flags & JSCLASS_DOM_GLOBAL) != 0);
/* Check to see whether the interface objects are already installed */
let proto_or_iface_array = get_proto_or_iface_array(global.get());
rval.set((*proto_or_iface_array)[id]);
if !rval.get().is_null() {
return;
}
creator(cx, global, proto_or_iface_array);
rval.set((*proto_or_iface_array)[id]);
assert!(!rval.get().is_null());
}
}
pub fn define_dom_interface(
cx: SafeJSContext,
global: HandleObject,
id: usize,
creator: unsafe fn(SafeJSContext, HandleObject, *mut ProtoOrIfaceArray),
enabled: fn(SafeJSContext, HandleObject) -> bool,
) {
assert!(!global.get().is_null());
if !enabled(cx, global) {
return;
}
rooted!(in(*cx) let mut proto = ptr::null_mut::<JSObject>());
get_per_interface_object_handle(
cx,
global,
id,
creator,
proto.handle_mut(),
);
assert!(!proto.is_null());
}