views:

1275

answers:

6

For debugging and testing I'm searching for a JavaScript shell with auto completion and if possible object introspection (like ipython). The online JavaScript Shell is really nice, but I'm looking for something local, without the need for an browser.

So far I have tested the standalone JavaScript interpreter rhino, spidermonkey and google V8. But neither of them has completion. At least Rhino with jline and spidermonkey have some kind of command history via key up/down, but nothing more.

Any suggestions?

This question was asked again here. It might contain an answer that you are looking for.

A: 

@John thanks I know firebug, but I'm looking for a standalone, commandline javascript shell.

Peter Hoffmann
A: 

Isn't Rhino Shell what you are looking for?

Jasper
+1  A: 

This post by John Resig says that there are shells for Tamarin (Firefox 4?) and JavaScriptCore (Safari 3). I'm not sure if they have auto completion though.

Sam Hasler
+6  A: 

Rhino Shell since 1.7R2 has support for completion as well. You can find more information here.

Martin Lazar
+1  A: 

In Windows, you can run this file from the command prompt in cscript.exe, and it provides an simple interactive shell. No completion.

// shell.js
// ------------------------------------------------------------------
//
// implements an interactive javascript shell.
//
// from
// http://kobyk.wordpress.com/2007/09/14/a-jscript-interactive-interpreter-shell-for-the-windows-script-host/
//
// Sat Nov 28 00:09:55 2009
//

var GSHELL = (function () {

    var numberToHexString = function (n) {
        if (n >= 0) {
            return n.toString(16);
        } else {
            n += 0x100000000;
            return n.toString(16);
        }
    };
    var line, scriptText, previousLine, result;

    return function() {
        while(true) {
            WScript.StdOut.Write("js> ");
            if (WScript.StdIn.AtEndOfStream) {
                WScript.Echo("Bye.");
                break;
            }
            line = WScript.StdIn.ReadLine();
            scriptText = line + "\n";
            if (line === "") {
                WScript.Echo(
                    "Enter two consecutive blank lines to terminate multi-line input.");
                do {
                    if (WScript.StdIn.AtEndOfStream) {
                        break;
                    }
                    previousLine = line;
                    line = WScript.StdIn.ReadLine();
                    line += "\n";
                    scriptText += line;
                } while(previousLine != "\n" || line != "\n");
            }
            try {
                result = eval(scriptText);
            } catch (error) {
                WScript.Echo("0x" + numberToHexString(error.number) + " " + error.name + ": " +
                             error.message);
            }
            if (result) {
                try {
                    WScript.Echo(result);
                } catch (error) {
                    WScript.Echo("<<>>");
                }
            }
            result = null;
        }
    };
})();

GSHELL();

If you want, you can augment that with other utility libraries, with a .wsf file. Save the above to "shell.js", and save the following to "shell.wsf":

<job>
    <reference object="Scripting.FileSystemObject" />
    <script language="JavaScript" src="util.js" />
    <script language="JavaScript" src="shell.js" />
</job>

...where util.js is:

var quit = function(x) { WScript.Quit(x);}
var say = function(s) { WScript.Echo(s); };
var echo = say;
var exit = quit;
var sleep = function(n) { WScript.Sleep(n*1000); };

...and then run shell.wsf from the command line.

Cheeso