Bug 1314861: Minor optimization: Define globals for shared sandbox modules on the sandbox rather than each module. r?ochameau
MozReview-Commit-ID: Lre6L2u4Y2r
--- 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.