31

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.

Community
  • 1
  • 1
Peter Hoffmann
  • 48,060
  • 14
  • 71
  • 58

6 Answers6

19

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

Martin Lazar
  • 1,330
  • 16
  • 23
5

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
  • 180,104
  • 92
  • 446
  • 681
5

edit: after using the node REPL a bit more, I've discovered this evaluation to be overly positive. There are some serious problems with its implementation, including an inability to yank killed text, issues with editing lines that are longer than the terminal width, and some other problems. It might be better to just use rhino.

The node.js REPL (node-repl on a system with node installed) is the best terminal-based, system-context shell I've seen so far. I'm comparing it to rhino and the built-in v8 shell. It provides tab-completion and line editing history, as well as syntax-colouring of evaluations. You can also import CommonJS modules, or at least those modules implemented by node.

Downside is that you have to build node. This is not a huge deal, as building apps goes, but might be a challenge if you don't normally do such things.

intuited
  • 20,786
  • 6
  • 57
  • 82
2

Isn't Rhino Shell what you are looking for?

Jasper
  • 161
  • 1
  • 4
1

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
  • 13,310
  • 9
  • 68
  • 101
0

jslibs (a standalone javascript runtime) could also be suitable for this purpose.

Franck Freiburger
  • 21,662
  • 20
  • 60
  • 90