mirror of
https://github.com/servo/servo.git
synced 2025-06-13 02:44:29 +00:00
Move more foundational types to script_bindings (#35280)
* script: Move DOMClass to script_bindings. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * script: Move DOMJSClass and get_dom_class to script_bindings. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * script: Move Castable/DerivedFrom/IDLInterface to script_bindings. Signed-off-by: Josh Matthews <josh@joshmatthews.net> --------- Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
parent
eaaad757e8
commit
c0cef69108
16 changed files with 291 additions and 207 deletions
|
@ -6982,7 +6982,7 @@ class CGNonNamespacedEnum(CGThing):
|
|||
|
||||
# Build the enum body.
|
||||
joinedEntries = ',\n'.join(entries)
|
||||
enumstr = f"{comment}pub(crate) enum {enumName} {{\n{joinedEntries}\n}}\n"
|
||||
enumstr = f"{comment}pub enum {enumName} {{\n{joinedEntries}\n}}\n"
|
||||
if repr:
|
||||
enumstr = f"#[repr({repr})]\n{enumstr}"
|
||||
if deriving:
|
||||
|
@ -8211,14 +8211,7 @@ class GlobalGenRoots():
|
|||
"""
|
||||
|
||||
@staticmethod
|
||||
def InterfaceObjectMap(config):
|
||||
mods = [
|
||||
"crate::dom::bindings::codegen",
|
||||
"crate::script_runtime::JSContext",
|
||||
"js::rust::HandleObject",
|
||||
]
|
||||
imports = CGList([CGGeneric(f"use {mod};") for mod in mods], "\n")
|
||||
|
||||
def Globals(config):
|
||||
global_descriptors = config.getDescriptors(isGlobal=True)
|
||||
flags = [("EMPTY", 0)]
|
||||
flags.extend(
|
||||
|
@ -8228,14 +8221,28 @@ class GlobalGenRoots():
|
|||
global_flags = CGWrapper(CGIndenter(CGList([
|
||||
CGGeneric(f"const {args[0]} = {args[1]};")
|
||||
for args in flags
|
||||
], "\n")), pre="#[derive(Clone, Copy)]\npub(crate) struct Globals: u8 {\n", post="\n}")
|
||||
], "\n")), pre="#[derive(Clone, Copy)]\npub struct Globals: u8 {\n", post="\n}")
|
||||
globals_ = CGWrapper(CGIndenter(global_flags), pre="bitflags::bitflags! {\n", post="\n}")
|
||||
|
||||
return CGList([
|
||||
CGGeneric(AUTOGENERATED_WARNING_COMMENT),
|
||||
globals_,
|
||||
])
|
||||
|
||||
@staticmethod
|
||||
def InterfaceObjectMap(config):
|
||||
mods = [
|
||||
"crate::dom::bindings::codegen",
|
||||
"crate::script_runtime::JSContext",
|
||||
"js::rust::HandleObject",
|
||||
]
|
||||
imports = CGList([CGGeneric(f"use {mod};") for mod in mods], "\n")
|
||||
|
||||
phf = CGGeneric("include!(concat!(env!(\"BINDINGS_OUT_DIR\"), \"/InterfaceObjectMapPhf.rs\"));")
|
||||
|
||||
return CGList([
|
||||
CGGeneric(AUTOGENERATED_WARNING_COMMENT),
|
||||
CGList([imports, globals_, phf], "\n\n")
|
||||
CGList([imports, phf], "\n\n")
|
||||
])
|
||||
|
||||
@staticmethod
|
||||
|
@ -8270,8 +8277,8 @@ class GlobalGenRoots():
|
|||
|
||||
return CGList([
|
||||
CGGeneric(AUTOGENERATED_WARNING_COMMENT),
|
||||
CGGeneric(f"pub(crate) const PROTO_OR_IFACE_LENGTH: usize = {len(protos) + len(constructors)};\n"),
|
||||
CGGeneric(f"pub(crate) const MAX_PROTO_CHAIN_LENGTH: usize = {config.maxProtoChainLength};\n\n"),
|
||||
CGGeneric(f"pub const PROTO_OR_IFACE_LENGTH: usize = {len(protos) + len(constructors)};\n"),
|
||||
CGGeneric(f"pub const MAX_PROTO_CHAIN_LENGTH: usize = {config.maxProtoChainLength};\n\n"),
|
||||
CGGeneric("#[allow(clippy::enum_variant_names, dead_code)]"),
|
||||
CGNonNamespacedEnum('ID', protos, 0, deriving="PartialEq, Copy, Clone", repr="u16"),
|
||||
CGNonNamespacedEnum('Constructor', constructors, len(protos),
|
||||
|
@ -8281,7 +8288,7 @@ class GlobalGenRoots():
|
|||
indentLevel=4),
|
||||
pre=f"static INTERFACES: [&str; {len(protos)}] = [\n",
|
||||
post="\n];\n\n"),
|
||||
CGGeneric("pub(crate) fn proto_id_to_name(proto_id: u16) -> &'static str {\n"
|
||||
CGGeneric("pub fn proto_id_to_name(proto_id: u16) -> &'static str {\n"
|
||||
" debug_assert!(proto_id < ID::Last as u16);\n"
|
||||
" INTERFACES[proto_id as usize]\n"
|
||||
"}\n\n"),
|
||||
|
@ -8329,17 +8336,13 @@ class GlobalGenRoots():
|
|||
return curr
|
||||
|
||||
@staticmethod
|
||||
def InheritTypes(config):
|
||||
|
||||
def ConcreteInheritTypes(config):
|
||||
descriptors = config.getDescriptors(register=True, isCallback=False)
|
||||
imports = [CGGeneric("use crate::dom::types::*;\n"),
|
||||
CGGeneric("use script_bindings::codegen::InheritTypes::*;\n"),
|
||||
CGGeneric("use crate::dom::bindings::conversions::{DerivedFrom, get_dom_class};\n"),
|
||||
CGGeneric("use crate::dom::bindings::inheritance::Castable;\n"),
|
||||
CGGeneric("use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom};\n"),
|
||||
CGGeneric("use crate::dom::bindings::trace::JSTraceable;\n"),
|
||||
CGGeneric("use crate::dom::bindings::reflector::DomObject;\n"),
|
||||
CGGeneric("use js::jsapi::JSTracer;\n\n"),
|
||||
CGGeneric("use std::mem;\n\n")]
|
||||
CGGeneric("use crate::dom::bindings::reflector::DomObject;\n\n")]
|
||||
allprotos = []
|
||||
topTypes = []
|
||||
hierarchy = defaultdict(list)
|
||||
|
@ -8368,19 +8371,57 @@ class GlobalGenRoots():
|
|||
if downcast:
|
||||
hierarchy[descriptor.interface.parent.identifier.name].append(name)
|
||||
|
||||
typeIdCode = []
|
||||
|
||||
for base, derived in hierarchy.items():
|
||||
if base in topTypes:
|
||||
typeIdCode.append(CGGeneric(f"""
|
||||
impl {base} {{
|
||||
pub(crate) fn type_id(&self) -> &'static {base}TypeId {{
|
||||
unsafe {{
|
||||
&get_dom_class(self.reflector().get_jsobject().get())
|
||||
.unwrap()
|
||||
.type_id
|
||||
.{base.lower()}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
|
||||
"""))
|
||||
|
||||
curr = CGList(imports + typeIdCode + allprotos)
|
||||
curr = CGWrapper(curr, pre=AUTOGENERATED_WARNING_COMMENT)
|
||||
return curr
|
||||
|
||||
@staticmethod
|
||||
def InheritTypes(config):
|
||||
descriptors = config.getDescriptors(register=True, isCallback=False)
|
||||
topTypes = []
|
||||
hierarchy = defaultdict(list)
|
||||
for descriptor in descriptors:
|
||||
name = descriptor.name
|
||||
upcast = descriptor.hasDescendants()
|
||||
downcast = len(descriptor.prototypeChain) != 1
|
||||
|
||||
if upcast and not downcast:
|
||||
topTypes.append(name)
|
||||
|
||||
if downcast:
|
||||
hierarchy[descriptor.interface.parent.identifier.name].append(name)
|
||||
|
||||
typeIdCode = []
|
||||
topTypeVariants = [
|
||||
("ID used by abstract interfaces.", "pub(crate) abstract_: ()"),
|
||||
("ID used by interfaces that are not castable.", "pub(crate) alone: ()"),
|
||||
("ID used by abstract interfaces.", "pub abstract_: ()"),
|
||||
("ID used by interfaces that are not castable.", "pub alone: ()"),
|
||||
]
|
||||
topTypeVariants += [
|
||||
(f"ID used by interfaces that derive from {typeName}.",
|
||||
f"pub(crate) {typeName.lower()}: {typeName}TypeId")
|
||||
f"pub {typeName.lower()}: {typeName}TypeId")
|
||||
for typeName in topTypes
|
||||
]
|
||||
topTypeVariantsAsStrings = [CGGeneric(f"/// {variant[0]}\n{variant[1]},") for variant in topTypeVariants]
|
||||
typeIdCode.append(CGWrapper(CGIndenter(CGList(topTypeVariantsAsStrings, "\n"), 4),
|
||||
pre="#[derive(Copy)]\npub(crate) union TopTypeId {\n",
|
||||
pre="#[derive(Copy)]\npub union TopTypeId {\n",
|
||||
post="\n}\n\n"))
|
||||
|
||||
typeIdCode.append(CGGeneric("""\
|
||||
|
@ -8403,24 +8444,10 @@ impl Clone for TopTypeId {
|
|||
variants += [CGGeneric(type_id_variant(derivedName)) for derivedName in derived]
|
||||
derives = "Clone, Copy, Debug, PartialEq"
|
||||
typeIdCode.append(CGWrapper(CGIndenter(CGList(variants, ",\n"), 4),
|
||||
pre=f"#[derive({derives})]\npub(crate) enum {base}TypeId {{\n",
|
||||
pre=f"#[derive({derives})]\npub enum {base}TypeId {{\n",
|
||||
post="\n}\n\n"))
|
||||
if base in topTypes:
|
||||
typeIdCode.append(CGGeneric(f"""
|
||||
impl {base} {{
|
||||
pub(crate) fn type_id(&self) -> &'static {base}TypeId {{
|
||||
unsafe {{
|
||||
&get_dom_class(self.reflector().get_jsobject().get())
|
||||
.unwrap()
|
||||
.type_id
|
||||
.{base.lower()}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
|
||||
"""))
|
||||
|
||||
curr = CGList(imports + typeIdCode + allprotos)
|
||||
curr = CGList(typeIdCode)
|
||||
curr = CGWrapper(curr, pre=AUTOGENERATED_WARNING_COMMENT)
|
||||
return curr
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue