From edea2caec148910965899670c4267064679cf4ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BClker?= Date: Sun, 18 May 2025 11:30:20 +0200 Subject: Make `getAllTypes` unwrap IDL `record` types (#37039) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IDL `record` types can themselves contain types that are not described anywhere else. An example is in https://github.com/servo/servo/issues/37038, where the `record` contains a definition of a union. These inner types must be returned from `getAllTypes`, otherwise we won't generate code for them. This PR also adds a few type annotations. I can remove them if requested, but I think they're helpful. Testing: Includes a regression test Fixes: https://github.com/servo/servo/issues/37038 --------- Signed-off-by: Simon Wülker --- components/script_bindings/codegen/CodegenRust.py | 29 +++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'components/script_bindings/codegen/CodegenRust.py') diff --git a/components/script_bindings/codegen/CodegenRust.py b/components/script_bindings/codegen/CodegenRust.py index 458aa7508b0..20d6d30681e 100644 --- a/components/script_bindings/codegen/CodegenRust.py +++ b/components/script_bindings/codegen/CodegenRust.py @@ -6,6 +6,7 @@ from collections import defaultdict from itertools import groupby +from typing import Generator, Tuple, Optional, List import operator import os @@ -18,7 +19,9 @@ from WebIDL import ( BuiltinTypes, IDLArgument, IDLBuiltinType, + IDLCallback, IDLDefaultDictionaryValue, + IDLDictionary, IDLEmptySequenceValue, IDLInterface, IDLInterfaceMember, @@ -27,12 +30,15 @@ from WebIDL import ( IDLObject, IDLPromiseType, IDLType, + IDLTypedef, IDLTypedefType, IDLUndefinedValue, IDLWrapperType, ) from Configuration import ( + Configuration, + Descriptor, MakeNativeName, MemberIsLegacyUnforgeable, getModuleFromObject, @@ -2580,7 +2586,12 @@ class CGCallbackTempRoot(CGGeneric): CGGeneric.__init__(self, f"{name.replace('', '::')}::new(cx, ${{val}}.get().to_object())") -def getAllTypes(descriptors, dictionaries, callbacks, typedefs): +def getAllTypes( + descriptors: List[Descriptor], + dictionaries: List[IDLDictionary], + callbacks: List[IDLCallback], + typedefs: List[IDLTypedef] +) -> Generator[Tuple[IDLType, Optional[Descriptor]], None, None]: """ Generate all the types we're dealing with. For each type, a tuple containing type, descriptor, dictionary is yielded. The @@ -2588,18 +2599,32 @@ def getAllTypes(descriptors, dictionaries, callbacks, typedefs): """ for d in descriptors: for t in getTypesFromDescriptor(d): + if t.isRecord(): + yield (t.inner, d) yield (t, d) for dictionary in dictionaries: for t in getTypesFromDictionary(dictionary): + if t.isRecord(): + yield (t.inner, None) yield (t, None) for callback in callbacks: for t in getTypesFromCallback(callback): + if t.isRecord(): + yield (t.inner, None) yield (t, None) for typedef in typedefs: + if typedef.innerType.isRecord(): + yield (typedef.innerType.inner, None) yield (typedef.innerType, None) -def UnionTypes(descriptors, dictionaries, callbacks, typedefs, config): +def UnionTypes( + descriptors: List[Descriptor], + dictionaries: List[IDLDictionary], + callbacks: List[IDLCallback], + typedefs: List[IDLTypedef], + config: Configuration +): """ Returns a CGList containing CGUnionStructs for every union. """ -- cgit v1.2.3