servo: Merge #17945 - Generate DOM bindings imports for webidl typedefs (from ferjm:typedef.dom.binding); r=jdm
authorFernando Jiménez Moreno <ferjmoreno@gmail.com>
Wed, 02 Aug 2017 13:22:32 -0500
changeset 421980 a514cbd99922972426392b6a6e1d38a124a28922
parent 421979 189eb3e434bd0c69ec3ad5c366b12d523e6b0eeb
child 421981 18071f8e2714ececdf32ed7b59b8ccf10a40d65e
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
bugs17945
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
servo: Merge #17945 - Generate DOM bindings imports for webidl typedefs (from ferjm:typedef.dom.binding); r=jdm - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors Source-Repo: https://github.com/servo/servo Source-Revision: 3ef182ca36f0067c0e431addc8ee7f1343b575c4
servo/components/script/dom/bindings/codegen/CodegenRust.py
--- a/servo/components/script/dom/bindings/codegen/CodegenRust.py
+++ b/servo/components/script/dom/bindings/codegen/CodegenRust.py
@@ -1843,17 +1843,18 @@ class CGWrapper(CGThing):
                 defn.replace("\n", "\n" + (" " * len(self.pre))))
         return self.pre + defn + self.post
 
 
 class CGImports(CGWrapper):
     """
     Generates the appropriate import/use statements.
     """
-    def __init__(self, child, descriptors, callbacks, dictionaries, enums, imports, config, ignored_warnings=None):
+    def __init__(self, child, descriptors, callbacks, dictionaries, enums, typedefs, imports, config,
+                 ignored_warnings=None):
         """
         Adds a set of imports.
         """
         if ignored_warnings is None:
             ignored_warnings = [
                 'non_camel_case_types',
                 'non_upper_case_globals',
                 'unused_imports',
@@ -1932,16 +1933,21 @@ class CGImports(CGWrapper):
         # Import the type names used in the callbacks that are being defined.
         for c in callbacks:
             types += relatedTypesForSignatures(c)
 
         # Import the type names used in the dictionaries that are being defined.
         for d in dictionaries:
             types += componentTypes(d)
 
+        # Import the type names used in the typedefs that are being defined.
+        for t in typedefs:
+            if not t.innerType.isCallback():
+                types += componentTypes(t.innerType)
+
         # Normalize the types we've collected and remove any ones which can't be imported.
         types = removeWrapperAndNullableTypes(types)
 
         descriptorProvider = config.getDescriptorProvider()
         extras = []
         for t in types:
             # Importing these types in the same module that defines them is an error.
             if t in dictionaries or t in enums:
@@ -2287,16 +2293,17 @@ def UnionTypes(descriptors, dictionaries
     # Sort unionStructs by key, retrieve value
     unionStructs = (i[1] for i in sorted(unionStructs.items(), key=operator.itemgetter(0)))
 
     return CGImports(CGList(unionStructs, "\n\n"),
                      descriptors=[],
                      callbacks=[],
                      dictionaries=[],
                      enums=[],
+                     typedefs=[],
                      imports=imports,
                      config=config,
                      ignored_warnings=[])
 
 
 class Argument():
     """
     A class for outputting the type and name of an argument
@@ -5502,25 +5509,27 @@ class CGWeakReferenceableTrait(CGThing):
         CGThing.__init__(self)
         assert descriptor.weakReferenceable
         self.code = "impl WeakReferenceable for %s {}" % descriptor.interface.identifier.name
 
     def define(self):
         return self.code
 
 
-def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries=None, enums=None):
+def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries=None, enums=None, typedefs=None):
     if not callbacks:
         callbacks = []
     if not dictionaries:
         dictionaries = []
     if not enums:
         enums = []
-
-    return CGImports(cgthings, descriptors, callbacks, dictionaries, enums, [
+    if not typedefs:
+        typedefs = []
+
+    return CGImports(cgthings, descriptors, callbacks, dictionaries, enums, typedefs, [
         'core::nonzero::NonZero',
         'js',
         'js::JSCLASS_GLOBAL_SLOT_COUNT',
         'js::JSCLASS_IS_DOMJSCLASS',
         'js::JSCLASS_IS_GLOBAL',
         'js::JSCLASS_RESERVED_SLOTS_MASK',
         'js::JS_CALLEE',
         'js::error::throw_type_error',
@@ -6215,17 +6224,17 @@ class CGBindingRoot(CGThing):
 
         if not (descriptors or dictionaries or mainCallbacks or callbackDescriptors or enums):
             self.root = None
             return
 
         # Do codegen for all the enums.
         cgthings = [CGEnum(e) for e in enums]
 
-        # Do codegen for all the typdefs
+        # Do codegen for all the typedefs
         for t in typedefs:
             typeName = getRetvalDeclarationForType(t.innerType, config.getDescriptorProvider())
             substs = {
                 "name": t.identifier.name,
                 "type": typeName.define(),
             }
 
             if t.innerType.isUnion() and not t.innerType.nullable():
@@ -6253,17 +6262,17 @@ class CGBindingRoot(CGThing):
                                 CGCallbackFunctionImpl(x.interface)], "\n")
                         for x in callbackDescriptors)
 
         # And make sure we have the right number of newlines at the end
         curr = CGWrapper(CGList(cgthings, "\n\n"), post="\n\n")
 
         # Add imports
         curr = generate_imports(config, curr, callbackDescriptors, mainCallbacks,
-                                dictionaries, enums)
+                                dictionaries, enums, typedefs)
 
         # Add the auto-generated comment.
         curr = CGWrapper(curr, pre=AUTOGENERATED_WARNING_COMMENT)
 
         # Store the final result.
         self.root = curr
 
     def define(self):
@@ -7055,17 +7064,17 @@ class GlobalGenRoots():
 
     @staticmethod
     def RegisterBindings(config):
         # TODO - Generate the methods we want
         code = CGList([
             CGRegisterProxyHandlers(config),
         ], "\n")
 
-        return CGImports(code, descriptors=[], callbacks=[], dictionaries=[], enums=[], imports=[
+        return CGImports(code, descriptors=[], callbacks=[], dictionaries=[], enums=[], typedefs=[], imports=[
             'dom::bindings::codegen::Bindings',
             'dom::bindings::codegen::PrototypeList::Proxies',
             'libc',
         ], config=config, ignored_warnings=[])
 
     @staticmethod
     def InterfaceTypes(config):
         descriptors = sorted([MakeNativeName(d.name)