Bug 1388215: Part 1 - Add defineLazyModuleGetters and defineLazyServiceGetters methods. r?florian draft
authorKris Maglione <maglione.k@gmail.com>
Tue, 08 Aug 2017 14:57:53 -0700
changeset 642901 73568e194eeb
parent 642833 92318a417ec1
child 642902 f67ff4d3ef52
push id72914
push usermaglione.k@gmail.com
push dateTue, 08 Aug 2017 23:49:25 +0000
reviewersflorian
bugs1388215
milestone57.0a1
Bug 1388215: Part 1 - Add defineLazyModuleGetters and defineLazyServiceGetters methods. r?florian MozReview-Commit-ID: 8sAjBlRzoYS
js/xpconnect/loader/XPCOMUtils.jsm
--- a/js/xpconnect/loader/XPCOMUtils.jsm
+++ b/js/xpconnect/loader/XPCOMUtils.jsm
@@ -315,16 +315,58 @@ this.XPCOMUtils = {
         Cu.reportError("Failed to load module " + aResource + ".");
         throw ex;
       }
       return temp[aSymbol || aName];
     });
   },
 
   /**
+   * Defines a lazy module getter on a specified object for each
+   * property in the given object.
+   *
+   * @param aObject
+   *        The object to define the lazy getter on.
+   * @param aModules
+   *        An object with a property for each module property to be
+   *        imported, where the property name is the name of the
+   *        imported symbol and the value is the module URI.
+   */
+  defineLazyModuleGetters: function XPCU_defineLazyModuleGetters(
+                                   aObject, aModules)
+  {
+    for (let [name, module] of Object.entries(aModules)) {
+      this.defineLazyModuleGetter(aObject, name, module);
+    }
+  },
+
+  /**
+   * Defines a lazy service getter on a specified object for each
+   * property in the given object.
+   *
+   * @param aObject
+   *        The object to define the lazy getter on.
+   * @param aServices
+   *        An object with a property for each service to be
+   *        imported, where the property name is the name of the
+   *        symbol to define, and the value is an array to be passed as
+   *        additional arguments to defineLazyServiceGetter.
+   */
+  defineLazyServiceGetters: function XPCU_defineLazyServiceGetters(
+                                   aObject, aServices)
+  {
+    for (let [name, service] of Object.entries(aServices)) {
+      // Note: This is hot code, and cross-compartment array wrappers
+      // are not JIT-friendly to destructuring or spread operators, so
+      // we need to use indexed access instead.
+      this.defineLazyServiceGetter(aObject, name, service[0], service[1]);
+    }
+  },
+
+  /**
    * Defines a getter on a specified object for preference value. The
    * preference is read the first time that the property is accessed,
    * and is thereafter kept up-to-date using a preference observer.
    *
    * @param aObject
    *        The object to define the lazy getter on.
    * @param aName
    *        The name of the getter property to define on aObject.