Bug 1495723 - Add support for importing extra symbols to `defineLazyGlobalGetters`. r=kmag
authorLina Cambridge <lina@yakshaving.ninja>
Tue, 02 Oct 2018 20:10:04 +0000
changeset 497735 1fa05a1ba7626bbe7e4144b933c490372d818234
parent 497734 005e374d721ecb778d3a8293e6a1c033c4916e0a
child 497736 0f5faf5435a815f27817a2944e5d0e83dea0c685
push id1864
push userffxbld-merge
push dateMon, 03 Dec 2018 15:51:40 +0000
treeherdermozilla-release@f040763d99ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1495723
milestone64.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
Bug 1495723 - Add support for importing extra symbols to `defineLazyGlobalGetters`. r=kmag 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");
  *