aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Wülker <simon.wuelker@arcor.de>2025-05-18 11:30:20 +0200
committerGitHub <noreply@github.com>2025-05-18 09:30:20 +0000
commitedea2caec148910965899670c4267064679cf4ed (patch)
tree196d1fded370f462fb313c40065847cedf8a3a50
parent070a8cf937b3a4c07c1affdb3e3960f85cc67976 (diff)
downloadservo-edea2caec148910965899670c4267064679cf4ed.tar.gz
servo-edea2caec148910965899670c4267064679cf4ed.zip
Make `getAllTypes` unwrap IDL `record<K, V>` types (#37039)
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 <simon.wuelker@arcor.de>
-rw-r--r--components/script_bindings/codegen/CodegenRust.py29
-rw-r--r--components/script_bindings/webidls/TestBinding.webidl6
2 files changed, 33 insertions, 2 deletions
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('<D>', '::<D>')}::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.
"""
diff --git a/components/script_bindings/webidls/TestBinding.webidl b/components/script_bindings/webidls/TestBinding.webidl
index f70e73568cc..f2c45313228 100644
--- a/components/script_bindings/webidls/TestBinding.webidl
+++ b/components/script_bindings/webidls/TestBinding.webidl
@@ -621,3 +621,9 @@ namespace TestNS {
};
typedef Promise<undefined> PromiseUndefined;
+
+// https://github.com/servo/servo/issues/37038
+dictionary NotUsedAnyWhereElse {};
+dictionary RecordFieldWithUnionInside {
+ record<USVString, (USVString or NotUsedAnyWhereElse)> recordWithUnionField;
+};