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 558811 aad8a872168fb97a73cb98358e449a888c07a4d0
parent 558810 69c218163a366ccf491bcf10fd5e1a8df3e1f2ad
child 558812 5f1754e1d3d7407451c191cd4e15243dd0be655a
push id52953
push usermaglione.k@gmail.com
push dateSat, 08 Apr 2017 01:32:07 +0000
reviewersochameau
bugs1314861
milestone55.0a1
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),
     };
     descriptors.lazyRequireModule = {
       configurable: true,
       value: lazyRequireModule.bind(sandbox),
     };
@@ -1132,19 +1127,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.