]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/main.jsm
Imported Upstream version 1.1+hg7904
[dactyl.git] / common / modules / main.jsm
index 19e36b46a11574f9bd27e16ec7a69e24c867ee3a..80e64a797bfce4f3e6e4e8fe61b2545fd377dea4 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c) 2009-2013 Kris Maglione <maglione.k@gmail.com>
+// Copyright (c) 2009-2014 Kris Maglione <maglione.k@gmail.com>
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
@@ -13,6 +13,8 @@ defineModule("main", {
 
 var BASE = "resource://dactyl-content/";
 
+var global = this;
+
 /**
  * @class ModuleBase
  * The base class for all modules.
@@ -89,21 +91,89 @@ var Modules = function Modules(window) {
     Module.list = [];
     Module.constructors = {};
 
-    const create = window.Object.create.bind(window.Object);
+    function newContext(proto, normal, name) {
+        if (normal)
+            return create(proto);
+
+        sandbox = Components.utils.Sandbox(window, { sandboxPrototype: proto || modules,
+                                                     sandboxName: name || ("Dactyl Sandbox " + ++_id),
+                                                     wantXrays: true });
+
+        // Hack:
+        // sandbox.Object = jsmodules.Object;
+        sandbox.File = global.File;
+        sandbox.Math = global.Math;
+        sandbox.Set  = global.Set;
+        return sandbox;
+    };
+
 
     const BASES = [BASE, "resource://dactyl-local-content/"];
 
-    jsmodules = Cu.createObjectIn(window);
+    let proxyCache = {};
+    if (config.haveGecko(29))
+        var proxy = new Proxy(window, {
+            get: function window_get(target, prop) {
+                // `in`, not `hasOwnProperty`, because we want to return
+                // unbound methods in `Object.prototype`
+                if (prop in proxyCache)
+                    return proxyCache[prop];
+
+                let p = target[prop];
+                if (callable(p))
+                    return proxyCache[prop] = p.bind(target);
+
+                return p;
+            },
+
+            set: function window_set(target, prop, val) {
+                return target[prop] = val;
+            }
+        });
+    else {
+        // Bug 814892
+        let o = {};
+        // Oh, the brokenness... See bug 793210
+        Object.preventExtensions(o);
+        proxy = new Proxy(o, {
+            get: function window_get(target, prop) {
+                // `in`, not `hasOwnProperty`, because we want to return
+                // unbound methods in `Object.prototype`
+                if (prop in proxyCache)
+                    return proxyCache[prop];
+
+                let p = window[prop];
+                if (callable(p))
+                    return proxyCache[prop] = p.bind(window);
+
+                return p;
+            },
+
+            set: function window_set(target, prop, val) {
+                return window[prop] = val;
+            },
+
+            getOwnPropertyDescriptor: function (target, prop) Object.getOwnPropertyDescriptor(window, prop),
+            getOwnPropertyNames: function (target, prop) Object.getOwnPropertyNames(window),
+            defineProperty: function (target, prop, desc) Object.defineProperty(window, prop, desc),
+            deleteProperty: function (target, prop) { delete window[prop]; },
+            has: function (target, prop) prop in window,
+            hasOwn: function (target, prop) hasOwnProperty(window, prop),
+            enumerate: function (target) (p for (p in window)),
+            iterate: function (target) (p for (p of window))
+        });
+    }
+
+    var jsmodules = newContext(proxy, false, "Dactyl `jsmodules`");
     jsmodules.NAME = "jsmodules";
+
+    const create = bind("create", jsmodules.Object);
+
     const modules = update(create(jsmodules), {
         yes_i_know_i_should_not_report_errors_in_these_branches_thanks: [],
 
         jsmodules: jsmodules,
 
-        get content() this.config.browser.contentWindow || window.content,
-
-        window: window,
-
         Module: Module,
 
         load: function load(script) {
@@ -128,24 +198,7 @@ var Modules = function Modules(window) {
             }
         },
 
-        newContext: function newContext(proto, normal, name) {
-            if (normal)
-                return create(proto);
-
-            if (services.has("dactyl") && services.dactyl.createGlobal)
-                var sandbox = services.dactyl.createGlobal();
-            else
-                sandbox = Components.utils.Sandbox(window, { sandboxPrototype: proto || modules,
-                                                             sandboxName: name || ("Dactyl Sandbox " + ++_id),
-                                                             wantXrays: false });
-
-            // Hack:
-            // sandbox.Object = jsmodules.Object;
-            sandbox.File = jsmodules.File;
-            sandbox.Math = jsmodules.Math;
-            sandbox.__proto__ = proto || modules;
-            return sandbox;
-        },
+        newContext: newContext,
 
         get ownPropertyValues() array.compact(
                 Object.getOwnPropertyNames(this)
@@ -167,6 +220,7 @@ overlay.overlayWindow(Object.keys(config.overlays),
         const modules = Modules(window);
         modules.moduleManager = this;
         this.modules = modules;
+        this.jsmodules = modules.jsmodules;
 
         window.dactyl = { modules: modules };
 
@@ -192,8 +246,8 @@ overlay.overlayWindow(Object.keys(config.overlays),
 
         this.startTime = Date.now();
         this.deferredInit = { load: {} };
-        this.seen = {};
-        this.loaded = {};
+        this.seen = RealSet();
+        this.loaded = RealSet();
         modules.loaded = this.loaded;
 
         this.modules = modules;
@@ -225,11 +279,13 @@ overlay.overlayWindow(Object.keys(config.overlays),
     },
 
     cleanup: function cleanup(window) {
-        overlay.windows = overlay.windows.filter(w => w != window);
+        overlay.windows.delete(window);
+
+        Cu.nukeSandbox(this.jsmodules);
     },
 
     unload: function unload(window) {
-        for each (let mod in this.modules.moduleList.reverse()) {
+        for (let mod of this.modules.moduleList.reverse()) {
             mod.stale = true;
 
             if ("destroy" in mod)
@@ -244,7 +300,7 @@ overlay.overlayWindow(Object.keys(config.overlays),
 
         defineModule.loadLog.push("Loaded in " + (Date.now() - this.startTime) + "ms");
 
-        overlay.windows = array.uniq(overlay.windows.concat(window), true);
+        overlay.windows.add(window);
     },
 
     loadModule: function loadModule(module, prereq, frame) {
@@ -258,10 +314,10 @@ overlay.overlayWindow(Object.keys(config.overlays),
         }
 
         try {
-            if (Set.has(loaded, module.className))
+            if (loaded.has(module.className))
                 return;
 
-            if (Set.add(seen, module.className))
+            if (seen.add(module.className))
                 throw Error("Module dependency loop.");
 
             for (let dep in values(module.requires))
@@ -277,9 +333,9 @@ overlay.overlayWindow(Object.keys(config.overlays),
             let obj = defineModule.time(module.className, "init", module);
             Class.replaceProperty(modules, module.className, obj);
 
-            Set.add(loaded, module.className);
+            loaded.add(module.className);
 
-            if (loaded.dactyl && obj.signals)
+            if (loaded.has("dactyl") && obj.signals)
                 modules.dactyl.registerObservers(obj);
 
             if (!module.lazyDepends)
@@ -300,7 +356,7 @@ overlay.overlayWindow(Object.keys(config.overlays),
 
         let className = mod.className || mod.constructor.className;
 
-        if (!Set.has(init, className)) {
+        if (!hasOwnProperty(init, className)) {
             init[className] = function callee() {
                 function finish() {
                     this.currentDependency = className;
@@ -324,11 +380,12 @@ overlay.overlayWindow(Object.keys(config.overlays),
         let { Module, modules } = this.modules;
 
         defineModule.modules.forEach((mod) => {
-            let names = Set(Object.keys(mod.INIT));
+            let names = RealSet(Object.keys(mod.INIT));
             if ("init" in mod.INIT)
-                Set.add(names, "init");
+                names.add("init");
 
-            keys(names).forEach((name) => { this.deferInit(name, mod.INIT, mod); });
+            for (let name of names)
+                this.deferInit(name, mod.INIT, mod);
         });
 
         Module.list.forEach((mod) => {