Bug 1186409 - Fix Addon SDK JSM context detection. draft
authorAndrew McCreight <continuation@gmail.com>
Tue, 06 Jun 2017 16:09:10 -0700
changeset 656074 b4484c8d90a086752fad8055bb37e9dc6b287e19
parent 656073 f35f93edc299690bdb2a89438639ac5004898eb3
child 656075 2f7ba785adf7fde13c2a43de1c1109fadb631754
push id77055
push userbmo:continuation@gmail.com
push dateWed, 30 Aug 2017 18:10:37 +0000
bugs1186409
milestone57.0a1
Bug 1186409 - Fix Addon SDK JSM context detection. Two files in the Addon SDK change their behavior depending on who is loading them. The JSM case does not work as written with JSM global sharing. The URI property is on the |this| object with JSM merging, and I think has to be explicitly accessed because of the way that the property is set up. Because |this| isn't the global, there is no Components object on it. We have to rely on regular variable lookup to find it. Hopefully this won't break if run in some way that isn't tested. I'm not entirely sure this is right, but it passes tests. MozReview-Commit-ID: 2n2NICVytxv
addon-sdk/source/lib/sdk/core/promise.js
addon-sdk/source/lib/toolkit/require.js
--- a/addon-sdk/source/lib/sdk/core/promise.js
+++ b/addon-sdk/source/lib/sdk/core/promise.js
@@ -83,20 +83,22 @@ function getEnvironment (callback) {
   let Cu, _exports, _module, _require;
 
   // CommonJS / SDK
   if (typeof(require) === 'function') {
     Cu = require('chrome').Cu;
     _exports = exports;
     _module = module;
     _require = require;
-  }
   // JSM
-  else if (String(this).indexOf('BackstagePass') >= 0) {
-    Cu = this['Components'].utils;
+  } else if (typeof(this.__URI__) === "string") {
+    // Intentionally bypass the scan_for_bad_chrome checker in a way
+    // that works with shared JSM globals.
+    let comp = Components;
+    Cu = comp.utils;
     _exports = this.Promise = {};
     _module = { uri: __URI__, id: 'promise/core' };
     _require = uri => {
       let imports = {};
       Cu.import(uri, imports);
       return imports;
     };
     this.EXPORTED_SYMBOLS = ['Promise'];
--- a/addon-sdk/source/lib/toolkit/require.js
+++ b/addon-sdk/source/lib/toolkit/require.js
@@ -72,20 +72,20 @@ const make = (exports, rootURI, componen
 
 // If loaded in the context of commonjs module, reload as JSM into an
 // exports object.
 if (typeof(require) === "function" && typeof(module) === "object") {
   require("chrome").Cu.import(module.uri, module.exports);
 }
 // If loaded in the context of JSM make a loader & require and define
 // new symbols as exported ones.
-else if (typeof(__URI__) === "string" && this["Components"]) {
+else if (typeof(this.__URI__) === "string" && Components) {
   const builtin = Object.keys(this);
   const uri = __URI__.replace("toolkit/require.js", "");
-  make(this, uri, this["Components"]);
+  make(this, uri, Components);
 
   this.EXPORTED_SYMBOLS = Object.
                             keys(this).
                             filter($ => builtin.indexOf($) < 0);
 }
 else {
   throw Error("Loading require.js in this environment isn't supported")
 }