Bug 1186409 - Fix Addon SDK JSM context detection. r=kmag
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
--- 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")
}