Bug 1392602 - Remove `descriptor` helper in DevTools loader. r=jdescottes draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Mon, 28 Aug 2017 14:10:18 +0200
changeset 658609 ac2966a54df65038223bd4860e4ab4e359064a06
parent 658608 74e9d76a9f3357033cafdac573a35dcac4144f6e
child 658610 4964ba47021d16ae0b45185195c1b3a5edfb5946
push id77824
push userbmo:poirot.alex@gmail.com
push dateMon, 04 Sep 2017 12:19:59 +0000
reviewersjdescottes
bugs1392602
milestone57.0a1
Bug 1392602 - Remove `descriptor` helper in DevTools loader. r=jdescottes MozReview-Commit-ID: JHC0s0cMyZG
devtools/shared/Loader.jsm
devtools/shared/base-loader.js
--- a/devtools/shared/Loader.jsm
+++ b/devtools/shared/Loader.jsm
@@ -5,17 +5,17 @@
 "use strict";
 
 /**
  * Manages the base loader (base-loader.js) instance used to load the developer tools.
  */
 
 var { utils: Cu } = Components;
 var { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
-var { Loader, Require, descriptor, resolveURI, unload } =
+var { Loader, Require, resolveURI, unload } =
   Cu.import("resource://devtools/shared/base-loader.js", {});
 var { requireRawId } = Cu.import("resource://devtools/shared/loader-plugin-raw.jsm", {});
 
 this.EXPORTED_SYMBOLS = ["DevToolsLoader", "devtools", "BuiltinProvider",
                          "require", "loader"];
 
 /**
  * Providers are different strategies for loading the devtools.
@@ -188,17 +188,17 @@ DevToolsLoader.prototype = {
         get exports() {
           return modules[id];
         }
       };
     }
 
     // Register custom globals to the current loader instance
     globals.loader.id = this.id;
-    Object.defineProperties(loader.globals, descriptor(globals));
+    Object.defineProperties(loader.globals, Object.getOwnPropertyDescriptors(globals));
 
     // Expose lazy helpers on loader
     this.lazyGetter = globals.loader.lazyGetter;
     this.lazyImporter = globals.loader.lazyImporter;
     this.lazyServiceGetter = globals.loader.lazyServiceGetter;
     this.lazyRequireGetter = globals.loader.lazyRequireGetter;
   },
 
--- a/devtools/shared/base-loader.js
+++ b/devtools/shared/base-loader.js
@@ -27,37 +27,16 @@ const { defineLazyGetter } = XPCOMUtils;
 const bind = Function.call.bind(Function.bind);
 const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
 const prototypeOf = Object.getPrototypeOf;
 function* getOwnIdentifiers(x) {
   yield* Object.getOwnPropertyNames(x);
   yield* Object.getOwnPropertySymbols(x);
 }
 
-const COMPONENT_ERROR = '`Components` is not available in this context.\n' +
-  'Functionality provided by Components may be available in an SDK\n' +
-  'module: https://developer.mozilla.org/en-US/Add-ons/SDK \n\n' +
-  'However, if you still need to import Components, you may use the\n' +
-  '`chrome` module\'s properties for shortcuts to Component properties:\n\n' +
-  'Shortcuts: \n' +
-  '    Cc = Components' + '.classes \n' +
-  '    Ci = Components' + '.interfaces \n' +
-  '    Cu = Components' + '.utils \n' +
-  '    CC = Components' + '.Constructor \n' +
-  'Example: \n' +
-  '    let { Cc, Ci } = require(\'chrome\');\n';
-
-// Returns map of given `object`-s own property descriptors.
-const descriptor = function descriptor(object) {
-  let value = {};
-  for (let name of getOwnIdentifiers(object))
-    value[name] = getOwnPropertyDescriptor(object, name)
-  return value;
-};
-
 function sourceURI(uri) { return String(uri).split(" -> ").pop(); }
 
 function isntLoaderFrame(frame) { return frame.fileName !== __URI__ }
 
 function parseURI(uri) { return String(uri).split(" -> ").pop(); }
 
 function parseStack(stack) {
   let lines = String(stack).split("\n");
@@ -179,26 +158,36 @@ const Sandbox = function Sandbox(options
 // of the given `loader` by evaluating code associated with it.
 const load = function load(loader, module) {
   let { sandboxes, globals, loadModuleHook } = loader;
   let require = Require(loader, module);
 
   // We expose set of properties defined by `CommonJS` specification via
   // prototype of the sandbox. Also globals are deeper in the prototype
   // chain so that each module has access to them as well.
-  let descriptors = descriptor({
-    require: require,
-    module: module,
-    exports: module.exports,
-    get Components() {
-      // Expose `Components` property to throw error on usage with
-      // additional information
-      throw new ReferenceError(COMPONENT_ERROR);
-    }
-  });
+  let descriptors = {
+    require: {
+      configurable: true,
+      enumerable: true,
+      writable: true,
+      value: require
+    },
+    module: {
+      configurable: true,
+      enumerable: true,
+      writable: true,
+      value: module
+    },
+    exports: {
+      configurable: true,
+      enumerable: true,
+      writable: true,
+      value: module.exports
+    },
+  };
 
   let sandbox;
   if (loader.useSharedGlobalSandbox || isSystemURI(module.uri)) {
     // Create a new object in this sandbox, that will be used as
     // the scope object for this particular module
     sandbox = new loader.sharedGlobalSandbox.Object();
     descriptors.lazyRequire = {
       configurable: true,
@@ -432,17 +421,17 @@ function lazyRequireModule(obj, moduleId
 
 
 // Creates version of `require` that will be exposed to the given `module`
 // in the context of the given `loader`. Each module gets own limited copy
 // of `require` that is allowed to load only a modules that are associated
 // with it during link time.
 const Require = function Require(loader, requirer) {
   let {
-    modules, mapping, mappingCache, resolve: loaderResolve, load,
+    modules, mapping, mappingCache,
     manifest, rootURI, isNative, requireHook
   } = loader;
 
   function require(id) {
     if (!id) // Throw if `id` is not passed.
       throw Error('You must provide a module name when calling require() from '
                   + requirer.id, requirer.uri);
 
@@ -519,17 +508,17 @@ const Require = function Require(loader,
 
     let requirement, uri;
 
     if (modules[id]) {
       uri = requirement = id;
     }
     else if (requirer) {
       // Resolve `id` to its requirer if it's relative.
-      requirement = loaderResolve(id, requirer.id);
+      requirement = resolve(id, requirer.id);
     }
     else {
       requirement = id;
     }
 
     // Resolves `uri` of module using loaders resolve function.
     if (!uri) {
       if (mappingCache.has(requirement)) {
@@ -605,17 +594,16 @@ const unload = function unload(loader, r
 //   If `resolve` does not returns `uri` string exception will be thrown by
 //   an associated `require` call.
 function Loader(options) {
   function normalizeRootURI(uri) {
     return addTrailingSlash(join(uri));
   }
 
   let { paths, sharedGlobal, globals } = options;
-  let resolve = Loader.resolve;
   if (!globals) {
     globals = {};
   }
 
   // We create an identity object that will be dispatched on an unload
   // event as subject. This way unload listeners will be able to assert
   // which loader is unloaded. Please note that we intentionally don't
   // use `loader` as subject to prevent a loader access leakage through
@@ -688,23 +676,21 @@ function Loader(options) {
     mapping: { enumerable: false, value: mapping },
     mappingCache: { enumerable: false, value: new Map() },
     // Map of module objects indexed by module URIs.
     modules: { enumerable: false, value: modules },
     useSharedGlobalSandbox: { enumerable: false, value: !!sharedGlobal },
     sharedGlobalSandbox: { enumerable: false, value: sharedGlobalSandbox },
     // Map of module sandboxes indexed by module URIs.
     sandboxes: { enumerable: false, value: {} },
-    resolve: { enumerable: false, value: resolve },
     // ID of the addon, if provided.
     id: { enumerable: false, value: options.id },
     // Whether the modules loaded should be ignored by the debugger
     invisibleToDebugger: { enumerable: false,
                            value: options.invisibleToDebugger || false },
-    load: { enumerable: false, value: options.load || load },
     requireHook: { enumerable: false, value: options.requireHook },
     loadModuleHook: { enumerable: false, value: options.loadModuleHook },
   };
 
   return Object.create(null, returnObj);
 };
 
 var SystemRegExp = /^resource:\/\/(gre|devtools|testing-common)\//;