diff options
author | Josh Matthews <josh@joshmatthews.net> | 2025-02-04 05:36:30 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-04 10:36:30 +0000 |
commit | c0cef69108b199efc0fcb720fe70ffe0dd07d763 (patch) | |
tree | 3186949cbf606efe13a1ba6a06d5d26e8f727d99 /components/script_bindings/codegen | |
parent | eaaad757e81d52c22b8e2b8039a310061e9d2cb3 (diff) | |
download | servo-c0cef69108b199efc0fcb720fe70ffe0dd07d763.tar.gz servo-c0cef69108b199efc0fcb720fe70ffe0dd07d763.zip |
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>
Diffstat (limited to 'components/script_bindings/codegen')
-rw-r--r-- | components/script_bindings/codegen/CodegenRust.py | 109 | ||||
-rw-r--r-- | components/script_bindings/codegen/run.py | 2 |
2 files changed, 70 insertions, 41 deletions
diff --git a/components/script_bindings/codegen/CodegenRust.py b/components/script_bindings/codegen/CodegenRust.py index de4adafe886..66a6b84848a 100644 --- a/components/script_bindings/codegen/CodegenRust.py +++ b/components/script_bindings/codegen/CodegenRust.py @@ -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) @@ -8369,18 +8372,56 @@ class GlobalGenRoots(): 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 diff --git a/components/script_bindings/codegen/run.py b/components/script_bindings/codegen/run.py index dd812cd2d1e..3ef7f296715 100644 --- a/components/script_bindings/codegen/run.py +++ b/components/script_bindings/codegen/run.py @@ -52,10 +52,12 @@ def main(): for name, filename in [ ("PrototypeList", "PrototypeList.rs"), ("RegisterBindings", "RegisterBindings.rs"), + ("Globals", "Globals.rs"), ("InterfaceObjectMap", "InterfaceObjectMap.rs"), ("InterfaceObjectMapData", "InterfaceObjectMapData.json"), ("InterfaceTypes", "InterfaceTypes.rs"), ("InheritTypes", "InheritTypes.rs"), + ("ConcreteInheritTypes", "ConcreteInheritTypes.rs"), ("Bindings", "Bindings/mod.rs"), ("UnionTypes", "UnionTypes.rs"), ("DomTypes", "DomTypes.rs"), |