Bug 1320793 - Fix lazy load of module globals in devtools. r=jryans
authorAlexandre Poirot <poirot.alex@gmail.com>
Mon, 28 Nov 2016 07:44:15 -0800
changeset 324782 5c3271cb35c218a5976aa87873099b45719178ea
parent 324781 b5bb7341ad8a75892822ddf527f45fbeddfdf1e4
child 324783 cffe1cd111f65c9045cc8e1fe34d845a9a6b5c28
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersjryans
bugs1320793
milestone53.0a1
Bug 1320793 - Fix lazy load of module globals in devtools. r=jryans MozReview-Commit-ID: HqfnxAcPK9v
devtools/shared/builtin-modules.js
--- a/devtools/shared/builtin-modules.js
+++ b/devtools/shared/builtin-modules.js
@@ -208,17 +208,17 @@ defineLazyGetter(exports.modules, "FileR
   let sandbox
     = Cu.Sandbox(CC("@mozilla.org/systemprincipal;1", "nsIPrincipal")(),
                  {wantGlobalProperties: ["FileReader"]});
   return sandbox.FileReader;
 });
 
 // List of all custom globals exposed to devtools modules.
 // Changes here should be mirrored to devtools/.eslintrc.
-const globals = exports.globals = {
+exports.globals = {
   isWorker: false,
   reportError: Cu.reportError,
   atob: atob,
   btoa: btoa,
   URL,
   loader: {
     lazyGetter: defineLazyGetter,
     lazyImporter: defineLazyModuleGetter,
@@ -249,40 +249,58 @@ const globals = exports.globals = {
   //   });
   //
   // Bug 1248830 will work out a better plan here for our content module
   // loading needs, especially as we head towards devtools.html.
   define(factory) {
     factory(this.require, this.exports, this.module);
   },
 };
+// SDK loader copy globals property descriptors on each module global object
+// so that we have to memoize them from here in order to instanciate each
+// global only once.
+// `globals` is a cache object on which we put all global values
+// and we set getters on `exports.globals` returning `globals` values.
+let globals = {};
+function lazyGlobal(name, getter) {
+  defineLazyGetter(globals, name, getter);
+  Object.defineProperty(exports.globals, name, {
+    get: function () {
+      return globals[name];
+    },
+    configurable: true,
+    enumerable: true
+  });
+}
 
 // Lazily define a few things so that the corresponding jsms are only loaded
 // when used.
-defineLazyGetter(globals, "console", () => {
+lazyGlobal("console", () => {
   return Cu.import("resource://gre/modules/Console.jsm", {}).console;
 });
-defineLazyGetter(globals, "clearTimeout", () => {
+lazyGlobal("clearTimeout", () => {
   return Cu.import("resource://gre/modules/Timer.jsm", {}).clearTimeout;
 });
-defineLazyGetter(globals, "setTimeout", () => {
+lazyGlobal("setTimeout", () => {
   return Cu.import("resource://gre/modules/Timer.jsm", {}).setTimeout;
 });
-defineLazyGetter(globals, "clearInterval", () => {
+lazyGlobal("clearInterval", () => {
   return Cu.import("resource://gre/modules/Timer.jsm", {}).clearInterval;
 });
-defineLazyGetter(globals, "setInterval", () => {
+lazyGlobal("setInterval", () => {
   return Cu.import("resource://gre/modules/Timer.jsm", {}).setInterval;
 });
-defineLazyGetter(globals, "CSSRule", () => Ci.nsIDOMCSSRule);
-defineLazyGetter(globals, "DOMParser", () => {
+lazyGlobal("CSSRule", () => Ci.nsIDOMCSSRule);
+lazyGlobal("DOMParser", () => {
   return CC("@mozilla.org/xmlextras/domparser;1", "nsIDOMParser");
 });
-defineLazyGetter(globals, "CSS", () => {
+lazyGlobal("CSS", () => {
   let sandbox
     = Cu.Sandbox(CC("@mozilla.org/systemprincipal;1", "nsIPrincipal")(),
                  {wantGlobalProperties: ["CSS"]});
   return sandbox.CSS;
 });
-defineLazyGetter(globals, "WebSocket", () => {
+lazyGlobal("WebSocket", () => {
   return Services.appShell.hiddenDOMWindow.WebSocket;
 });
-lazyRequireGetter(globals, "indexedDB", "sdk/indexed-db", true);
+lazyGlobal("indexedDB", () => {
+  return require("sdk/indexed-db").indexedDB;
+});