Bug 1388215: Part 1 - Add defineLazyModuleGetters and defineLazyServiceGetters methods. r?florian
MozReview-Commit-ID: 8sAjBlRzoYS
--- 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.