Bug Bug 1314861: Minor optimization: Define globals for shared sandbox modules on the sandbox rather than each module. r?ochameau draft
authorKris Maglione <maglione.k@gmail.com>
Wed, 02 Nov 2016 12:15:45 -0700
changeset 433086 3663dd9d459571ae95841d4245fecc3886108fda
parent 433085 315fcf2dbe58a869089db1ec233188e24fca18ed
child 433087 fb6b8a936888a56fb07dc60deb6955c7cfb42076
push id34481
push usermaglione.k@gmail.com
push dateThu, 03 Nov 2016 03:45:04 +0000
reviewersochameau
bugs1314861
milestone52.0a1
Bug Bug 1314861: Minor optimization: Define globals for shared sandbox modules on the sandbox rather than each module. r?ochameau MozReview-Commit-ID: Lre6L2u4Y2r
addon-sdk/source/lib/toolkit/loader.js
--- a/addon-sdk/source/lib/toolkit/loader.js
+++ b/addon-sdk/source/lib/toolkit/loader.js
@@ -468,21 +468,16 @@ const load = iced(function load(loader, 
   });
 
   let sandbox;
   if ((loader.useSharedGlobalSandbox || isSystemURI(module.uri)) &&
       loader.sharedGlobalBlocklist.indexOf(module.id) == -1) {
     // Create a new object in this sandbox, that will be used as
     // the scope object for this particular module
     sandbox = new loader.sharedGlobalSandbox.Object();
-    // Inject all expected globals in the scope object
-    getOwnIdentifiers(globals).forEach(function(name) {
-      descriptors[name] = getOwnPropertyDescriptor(globals, name)
-      descriptors[name].configurable = true;
-    });
     descriptors.lazyRequire = {
       configurable: true,
       value: lazyRequire.bind(sandbox),
     };
     Object.defineProperties(sandbox, descriptors);
   }
   else {
     sandbox = Sandbox({
@@ -1092,19 +1087,28 @@ function Loader(options) {
     name: "Addon-SDK",
     wantXrays: false,
     wantGlobalProperties: [],
     invisibleToDebugger: options.invisibleToDebugger || false,
     metadata: {
       addonID: options.id,
       URI: "Addon-SDK"
     },
-    prototype: options.sandboxPrototype || {}
+    prototype: options.sandboxPrototype || globals,
   });
 
+  if (options.sandboxPrototype) {
+    // If we were given a sandboxPrototype, we have to define the globals on
+    // the sandbox directly. Note that this will not work for callers who
+    // depend on being able to add globals after the loader was created.
+    for (let name of getOwnIdentifiers(globals))
+      Object.defineProperty(sharedGlobalSandbox, name,
+                            getOwnPropertyDescriptor(globals, name));
+  }
+
   // Loader object is just a representation of a environment
   // state. We freeze it and mark make it's properties non-enumerable
   // as they are pure implementation detail that no one should rely upon.
   let returnObj = {
     destructor: { enumerable: false, value: destructor },
     globals: { enumerable: false, value: globals },
     mapping: { enumerable: false, value: mapping },
     // Map of module objects indexed by module URIs.