From 9d9fdd2f790870b2dd6db07e0dd2840b6fd19467 Mon Sep 17 00:00:00 2001 From: syvb Date: Thu, 22 May 2025 13:50:43 -0700 Subject: [PATCH] Fix duplicate import generation Signed-off-by: Ashwin Naren --- components/script_bindings/codegen/CodegenRust.py | 10 ++++++---- components/script_bindings/codegen/Configuration.py | 1 + components/script_bindings/codegen/run.py | 5 +++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/components/script_bindings/codegen/CodegenRust.py b/components/script_bindings/codegen/CodegenRust.py index 20d6d30681e..a8e150f276c 100644 --- a/components/script_bindings/codegen/CodegenRust.py +++ b/components/script_bindings/codegen/CodegenRust.py @@ -2138,7 +2138,8 @@ class CGImports(CGWrapper): """ Generates the appropriate import/use statements. """ - def __init__(self, child, descriptors, callbacks, dictionaries, enums, typedefs, imports, config): + def __init__(self, child, descriptors, callbacks, dictionaries, enums, typedefs, imports, config, + current_name=None): """ Adds a set of imports. """ @@ -2249,7 +2250,8 @@ class CGImports(CGWrapper): parentName = descriptor.getParentName() while parentName: descriptor = descriptorProvider.getDescriptor(parentName) - extras += [descriptor.bindingPath] + if current_name != descriptor.ifaceName: + extras += [descriptor.path, descriptor.bindingPath] parentName = descriptor.getParentName() elif t.isType() and t.isRecord(): extras += ['crate::record::Record'] @@ -7644,7 +7646,7 @@ class CGBindingRoot(CGThing): Root codegen class for binding generation. Instantiate the class, and call declare or define to generate header or cpp code (respectively). """ - def __init__(self, config, prefix, webIDLFile): + def __init__(self, config, prefix, webIDLFile, name): descriptors = config.getDescriptors(webIDLFile=webIDLFile, hasInterfaceObject=True) # We also want descriptors that have an interface prototype object @@ -7712,7 +7714,7 @@ class CGBindingRoot(CGThing): # These are the global imports (outside of the generated module) curr = CGImports(curr, descriptors=callbackDescriptors, callbacks=mainCallbacks, dictionaries=dictionaries, enums=enums, typedefs=typedefs, - imports=['crate::import::base::*'], config=config) + imports=['crate::import::base::*'], config=config, current_name=name) # Add the auto-generated comment. curr = CGWrapper(curr, pre=f"{AUTOGENERATED_WARNING_COMMENT}{ALLOWED_WARNINGS}") diff --git a/components/script_bindings/codegen/Configuration.py b/components/script_bindings/codegen/Configuration.py index e540b0c3101..fe5a2825578 100644 --- a/components/script_bindings/codegen/Configuration.py +++ b/components/script_bindings/codegen/Configuration.py @@ -248,6 +248,7 @@ class Descriptor(DescriptorProvider): self.inRealmMethods = [name for name in desc.get('inRealms', [])] self.canGcMethods = [name for name in desc.get('canGc', [])] self.additionalTraits = [name for name in desc.get('additionalTraits', [])] + self.ifaceName = ifaceName self.bindingPath = f"{getModuleFromObject(self.interface)}::{ifaceName}_Binding" self.outerObjectHook = desc.get('outerObjectHook', 'None') self.proxy = False diff --git a/components/script_bindings/codegen/run.py b/components/script_bindings/codegen/run.py index 94f75b0153f..541db25a732 100644 --- a/components/script_bindings/codegen/run.py +++ b/components/script_bindings/codegen/run.py @@ -72,8 +72,9 @@ def main(): for webidl in webidls: filename = os.path.join(webidls_dir, webidl) - prefix = "Bindings/%sBinding" % webidl[:-len(".webidl")] - module = CGBindingRoot(config, prefix, filename).define() + name = webidl[:-len(".webidl")] + prefix = "Bindings/%sBinding" % name + module = CGBindingRoot(config, prefix, filename, name).define() if module: with open(os.path.join(out_dir, prefix + ".rs"), "wb") as f: f.write(module.encode("utf-8"))