Bug 1495723 - Add support for importing extra symbols to `defineLazyGlobalGetters`. r=kmag a=pascalc
authorLina Cambridge <lina@yakshaving.ninja>
Tue, 02 Oct 2018 20:10:04 +0000
changeset 490168 6a4d84aadda3b35f8ea9f556e1aae644668ed18e
parent 490167 cfa284805fbff5c80876c469d39e8217d7781ae0
child 490169 0ad842642aa7bca6216b9606a103250f08af0188
push id9930
push userarchaeopteryx@coole-files.de
push dateThu, 04 Oct 2018 09:57:52 +0000
treeherdermozilla-beta@6a4d84aadda3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag, pascalc
bugs1495723
milestone63.0
Bug 1495723 - Add support for importing extra symbols to `defineLazyGlobalGetters`. r=kmag a=pascalc Some global imports, like `fetch`, expose multiple symbols. This patch ensures we can define lazy getters for those symbols, by mapping the extra symbol name to the main import name. Differential Revision: https://phabricator.services.mozilla.com/D7522
js/xpconnect/loader/XPCOMUtils.jsm
services/sync/modules/resource.js
--- a/js/xpconnect/loader/XPCOMUtils.jsm
+++ b/js/xpconnect/loader/XPCOMUtils.jsm
@@ -54,16 +54,27 @@
 
 
 var EXPORTED_SYMBOLS = [ "XPCOMUtils" ];
 
 let global = Cu.getGlobalForObject({});
 
 const nsIFactoryQI = ChromeUtils.generateQI([Ci.nsIFactory]);
 
+// Some global imports expose additional symbols; for example,
+// `Cu.importGlobalProperties(["MessageChannel"])` imports `MessageChannel`
+// and `MessagePort`. This table maps those extra symbols to the main
+// import name.
+const EXTRA_GLOBAL_NAME_TO_IMPORT_NAME = {
+  Headers: "fetch",
+  MessagePort: "MessageChannel",
+  Request: "fetch",
+  Response: "fetch",
+};
+
 /**
  * Redefines the given property on the given object with the given
  * value. This can be used to redefine getter properties which do not
  * implement setters.
  */
 function redefine(object, prop, value) {
   Object.defineProperty(object, prop, {
     configurable: true,
@@ -169,17 +180,18 @@ var XPCOMUtils = {
    *        The object on which to define the properties.
    * @param {string[]} aNames
    *        The list of global properties to define.
    */
   defineLazyGlobalGetters(aObject, aNames) {
     for (let name of aNames) {
       this.defineLazyGetter(aObject, name, () => {
         if (!(name in global)) {
-          Cu.importGlobalProperties([name]);
+          let importName = EXTRA_GLOBAL_NAME_TO_IMPORT_NAME[name] || name;
+          Cu.importGlobalProperties([importName]);
         }
         return global[name];
       });
     }
   },
 
   /**
    * Defines a getter on a specified object for a service.  The service will not
--- a/services/sync/modules/resource.js
+++ b/services/sync/modules/resource.js
@@ -5,17 +5,17 @@
 var EXPORTED_SYMBOLS = ["Resource"];
 
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 ChromeUtils.import("resource://gre/modules/Log.jsm");
 ChromeUtils.import("resource://services-common/observers.js");
 ChromeUtils.import("resource://services-common/utils.js");
 ChromeUtils.import("resource://services-sync/util.js");
 const {setTimeout, clearTimeout} = ChromeUtils.import("resource://gre/modules/Timer.jsm", {});
-XPCOMUtils.defineLazyGlobalGetters(this, ["fetch"]);
+XPCOMUtils.defineLazyGlobalGetters(this, ["fetch", "Headers", "Request"]);
 /* global AbortController */
 
 /*
  * Resource represents a remote network resource, identified by a URI.
  * Create an instance like so:
  *
  *   let resource = new Resource("http://foobar.com/path/to/resource");
  *