]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/javascript.jsm
Import 1.0rc1 supporting Firefox up to 11.*
[dactyl.git] / common / modules / javascript.jsm
index 0f78615cc5b204b924716042538f1bae4c41101b..a4d1ab6bf435024c35d1cb68e0ce64bcaf133414 100644 (file)
@@ -2,7 +2,7 @@
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
-"use strict";
+/* use strict */
 
 let { getOwnPropertyNames } = Object;
 
@@ -10,8 +10,7 @@ try {
 
 Components.utils.import("resource://dactyl/bootstrap.jsm");
 defineModule("javascript", {
-    exports: ["JavaScript", "javascript"],
-    use: ["services", "template", "util"]
+    exports: ["JavaScript", "javascript"]
 }, this);
 
 let isPrototypeOf = Object.prototype.isPrototypeOf;
@@ -41,20 +40,20 @@ var JavaScript = Module("javascript", {
             this.window = window;
 
             init.supercall(this);
-        },
+        }
     }),
 
-    globals: Class.memoize(function () [
-       [this.modules.userContext, "Global Variables"],
+    globals: Class.Memoize(function () [
+       [this.modules.userContext, /*L*/"Global Variables"],
        [this.modules, "modules"],
        [this.window, "window"]
     ]),
 
-    toplevel: Class.memoize(function () this.modules.jsmodules),
+    toplevel: Class.Memoize(function () this.modules.jsmodules),
 
     lazyInit: true,
 
-    newContext: function () this.modules.newContext(this.modules.userContext),
+    newContext: function () this.modules.newContext(this.modules.userContext, true),
 
     get completers() JavaScript.completers, // For backward compatibility
 
@@ -71,22 +70,22 @@ var JavaScript = Module("javascript", {
         if (obj == null)
             return;
 
-        let seen = isinstance(obj, ["Sandbox"]) ? set(JavaScript.magicalNames) : {};
+        let seen = isinstance(obj, ["Sandbox"]) ? Set(JavaScript.magicalNames) : {};
         let globals = values(toplevel && this.window === obj ? this.globalNames : []);
 
         if (toplevel && isObject(obj) && "wrappedJSObject" in obj)
-            if (!set.add(seen, "wrappedJSObject"))
+            if (!Set.add(seen, "wrappedJSObject"))
                 yield "wrappedJSObject";
 
         for (let key in iter(globals, properties(obj, !toplevel, true)))
-            if (!set.add(seen, key))
+            if (!Set.add(seen, key))
                 yield key;
 
         // Properties aren't visible in an XPCNativeWrapper until
         // they're accessed.
         for (let key in properties(this.getKey(obj, "wrappedJSObject"), !toplevel, true))
             try {
-                if (key in obj && !set.has(seen, key))
+                if (key in obj && !Set.has(seen, key))
                     yield key;
             }
             catch (e) {}
@@ -103,7 +102,7 @@ var JavaScript = Module("javascript", {
 
         let completions = [k for (k in this.iter(obj, toplevel))];
         if (obj === this.modules) // Hack.
-            completions = completions.concat([k for (k in this.iter(this.modules.jsmodules, toplevel))]);
+            completions = array.uniq(completions.concat([k for (k in this.iter(this.modules.jsmodules, toplevel))]));
         return completions;
     },
 
@@ -117,18 +116,14 @@ var JavaScript = Module("javascript", {
             return cache[key];
 
         context[JavaScript.EVAL_TMP] = tmp;
-        context[JavaScript.EVAL_EXPORT] = function export_(obj) cache[key] = obj;
         try {
-            if (tmp != null) // Temporary hack until bug 609949 is fixed.
-                this.modules.dactyl.userEval(JavaScript.EVAL_EXPORT + "(" + arg + ")", context, "[Command Line Completion]", 1);
-            else
-                cache[key] = this.modules.dactyl.userEval(arg, context, "[Command Line Completion]", 1);
+            cache[key] = this.modules.dactyl.userEval(arg, context, /*L*/"[Command Line Completion]", 1);
 
             return cache[key];
         }
         catch (e) {
             util.reportError(e);
-            this.context.message = "Error: " + e;
+            this.context.message = _("error.error", e);
             return null;
         }
         finally {
@@ -170,7 +165,7 @@ var JavaScript = Module("javascript", {
 
         if (this._top.char != arg) {
             this.context.highlight(this._top.offset, this._i - this._top.offset, "SPELLCHECK");
-            throw Error("Invalid JS");
+            throw Error(/*L*/"Invalid JS");
         }
 
         // The closing character of this stack frame will have pushed a new
@@ -308,7 +303,7 @@ var JavaScript = Module("javascript", {
             if (this._checkFunction(prev, dot, cacheKey))
                 return [];
             if (prev != statement && obj == null) {
-                this.context.message = "Error: " + cacheKey.quote() + " is " + String(obj);
+                this.context.message = /*L*/"Error: " + cacheKey.quote() + " is " + String(obj);
                 return [];
             }
 
@@ -324,7 +319,7 @@ var JavaScript = Module("javascript", {
         let end = (frame == -1 ? this._lastIdx : this._get(frame + 1).offset);
 
         this._cacheKey = null;
-        let obj = [[this.cache.evalContext, "Local Variables"]].concat(this.globals);
+        let obj = [[this.cache.evalContext, /*L*/"Local Variables"]].concat(this.globals);
         // Is this an object dereference?
         if (dot < statement) // No.
             dot = statement - 1;
@@ -339,7 +334,7 @@ var JavaScript = Module("javascript", {
         const self = this;
 
         if (!getOwnPropertyNames && !services.debugger.isOn && !this.context.message)
-            this.context.message = "For better completion data, please enable the JavaScript debugger (:set jsdebugger)";
+            this.context.message = /*L*/"For better completion data, please enable the JavaScript debugger (:set jsdebugger)";
 
         let base = this.context.fork("js", this._top.offset);
         base.forceAnchored = true;
@@ -419,14 +414,14 @@ var JavaScript = Module("javascript", {
         objects.forEach(function (obj) {
             obj.ctxt_p.split(obj[1] + "/anchored", this, function (context) {
                 context.anchored = true;
-                context.title[0] += " (prototypes)";
+                context.title[0] += /*L*/" (prototypes)";
             });
         });
 
         objects.forEach(function (obj) {
             obj.ctxt_t.split(obj[1] + "/unanchored", this, function (context) {
                 context.anchored = false;
-                context.title[0] += " (substrings)";
+                context.title[0] += /*L*/" (substrings)";
                 context.filters.push(unanchored);
             });
         });
@@ -434,7 +429,7 @@ var JavaScript = Module("javascript", {
         objects.forEach(function (obj) {
             obj.ctxt_p.split(obj[1] + "/unanchored", this, function (context) {
                 context.anchored = false;
-                context.title[0] += " (prototype substrings)";
+                context.title[0] += /*L*/" (prototype substrings)";
                 context.filters.push(unanchored);
             });
         });
@@ -597,8 +592,10 @@ var JavaScript = Module("javascript", {
 
         // Wait for a keypress before completing when there's no key
         if (!this.context.tabPressed && key == "" && obj.length > 1) {
+            let message = this.context.message || "";
             this.context.waitingForTab = true;
-            this.context.message = "Waiting for key press";
+            this.context.message = <>{message}
+                                     {_("completion.waitingForKeyPress")}</>;
             return null;
         }
 
@@ -616,29 +613,28 @@ var JavaScript = Module("javascript", {
         return null;
     },
 
-    magicalNames: Class.memoize(function () Object.getOwnPropertyNames(Cu.Sandbox(this.window), true).sort()),
+    magicalNames: Class.Memoize(function () Object.getOwnPropertyNames(Cu.Sandbox(this.window), true).sort()),
 
     /**
      * A list of properties of the global object which are not
      * enumerable by any standard method.
      */
-    globalNames: Class.memoize(function () let (self = this) array.uniq([
-        "Array", "ArrayBuffer", "AttributeName", "Boolean", "Components",
+    globalNames: Class.Memoize(function () let (self = this) array.uniq([
+        "Array", "ArrayBuffer", "AttributeName", "Audio", "Boolean", "Components",
         "CSSFontFaceStyleDecl", "CSSGroupRuleRuleList", "CSSNameSpaceRule",
-        "CSSRGBColor", "CSSRect", "ComputedCSSStyleDeclaration", "Date",
-        "Error", "EvalError", "Float32Array", "Float64Array", "Function",
+        "CSSRGBColor", "CSSRect", "ComputedCSSStyleDeclaration", "Date", "Error",
+        "EvalError", "File", "Float32Array", "Float64Array", "Function",
         "HTMLDelElement", "HTMLInsElement", "HTMLSpanElement", "Infinity",
         "InnerModalContentWindow", "InnerWindow", "Int16Array", "Int32Array",
         "Int8Array", "InternalError", "Iterator", "JSON", "KeyboardEvent",
         "Math", "NaN", "Namespace", "Number", "Object", "Proxy", "QName",
         "ROCSSPrimitiveValue", "RangeError", "ReferenceError", "RegExp",
         "StopIteration", "String", "SyntaxError", "TypeError", "URIError",
-        "Uint16Array", "Uint32Array", "Uint8Array", "XML",
-        "XMLHttpProgressEvent", "XMLList", "XMLSerializer", "XPCNativeWrapper",
-        "XPCSafeJSWrapper", "XULControllers", "constructor", "decodeURI",
-        "decodeURIComponent", "encodeURI", "encodeURIComponent", "escape",
-        "eval", "isFinite", "isNaN", "isXMLName", "parseFloat", "parseInt",
-        "undefined", "unescape", "uneval"
+        "Uint16Array", "Uint32Array", "Uint8Array", "XML", "XMLHttpProgressEvent",
+        "XMLList", "XMLSerializer", "XPCNativeWrapper", "XPCSafeJSWrapper",
+        "XULControllers", "constructor", "decodeURI", "decodeURIComponent",
+        "encodeURI", "encodeURIComponent", "escape", "eval", "isFinite", "isNaN",
+        "isXMLName", "parseFloat", "parseInt", "undefined", "unescape", "uneval"
     ].concat([k.substr(6) for (k in keys(Ci)) if (/^nsIDOM/.test(k))])
      .concat([k.substr(3) for (k in keys(Ci)) if (/^nsI/.test(k))])
      .concat(this.magicalNames)
@@ -646,7 +642,6 @@ var JavaScript = Module("javascript", {
 
 }, {
     EVAL_TMP: "__dactyl_eval_tmp",
-    EVAL_EXPORT: "__dactyl_eval_export",
 
     /**
      * A map of argument completion functions for named methods. The
@@ -670,9 +665,9 @@ var JavaScript = Module("javascript", {
      * time they are accessed, so they should be accessed
      * judiciously.
      *
-     * @param {function|function[]} funcs The functions for which to
+     * @param {function|[function]} funcs The functions for which to
      *      install the completers.
-     * @param {function[]} completers An array of completer
+     * @param {[function]} completers An array of completer
      *      functions.
      */
     setCompleter: function (funcs, completers) {
@@ -705,7 +700,8 @@ var JavaScript = Module("javascript", {
 
         modes.addMode("REPL", {
             description: "JavaScript Read Eval Print Loop",
-            bases: [modes.COMMAND_LINE]
+            bases: [modes.COMMAND_LINE],
+            displayName: Class.Memoize(function () this.name)
         });
     },
     commandline: function initCommandLine(dactyl, modules, window) {
@@ -723,7 +719,7 @@ var JavaScript = Module("javascript", {
 
                 try {
                     var result = dactyl.userEval(js, this.context);
-                    var xml = util.objectToString(result, true);
+                    var xml = result === undefined ? "" : util.objectToString(result, true);
                 }
                 catch (e) {
                     util.reportError(e);
@@ -751,7 +747,7 @@ var JavaScript = Module("javascript", {
 
             count: 0,
 
-            message: Class.memoize(function () {
+            message: Class.Memoize(function () {
                 default xml namespace = XHTML;
                 util.xmlToDom(<div highlight="REPL" key="rootNode"/>,
                               this.document, this);
@@ -767,7 +763,7 @@ var JavaScript = Module("javascript", {
                 init.supercall(this);
 
                 let self = this;
-                let sandbox = isinstance(context, ["Sandbox"]);
+                let sandbox = true || isinstance(context, ["Sandbox"]);
 
                 this.context = modules.newContext(context, !sandbox);
                 this.js = modules.JavaScript();
@@ -775,8 +771,8 @@ var JavaScript = Module("javascript", {
                 this.js.newContext = function newContext() modules.newContext(self.context, !sandbox);
 
                 this.js.globals = [
-                   [this.context, "REPL Variables"],
-                   [context, "REPL Global"]
+                   [this.context, /*L*/"REPL Variables"],
+                   [context, /*L*/"REPL Global"]
                 ].concat(this.js.globals.filter(function ([global]) isPrototypeOf.call(global, context)));
 
                 if (!isPrototypeOf.call(modules.jsmodules, context))
@@ -790,13 +786,14 @@ var JavaScript = Module("javascript", {
 
                 this.repl = REPL(this.context);
             },
+
             open: function open(context) {
-                this.updatePrompt();
 
                 modules.mow.echo(this.repl);
                 this.widgets.message = null;
 
                 open.superapply(this, arguments);
+                this.updatePrompt();
             },
 
             complete: function complete(context) {
@@ -807,8 +804,10 @@ var JavaScript = Module("javascript", {
 
             mode: modes.REPL,
 
+            get completionList() this.widgets.statusbar.commandline.id,
+
             accept: function accept() {
-                dactyl.trapErrors(function () { this.repl.addOutput(this.command) }, this);
+                dactyl.trapErrors(function () { this.repl.addOutput(this.command); }, this);
 
                 this.completions.cleanup();
                 this.history.save();
@@ -836,8 +835,6 @@ var JavaScript = Module("javascript", {
         commands.add(["javas[cript]", "js"],
             "Evaluate a JavaScript string",
             function (args) {
-                modules.commandline;
-
                 if (args[0] && !args.bang)
                     dactyl.userEval(args[0]);
                 else {
@@ -846,6 +843,7 @@ var JavaScript = Module("javascript", {
                            .open();
                 }
             }, {
+                argCount: "?",
                 bang: true,
                 completer: function (context) modules.completion.javascript(context),
                 hereDoc: true,