Monday, March 31, 2008

Better line editing for Rhino shell

Thanks to a patch from Matthieu Riou, we now have line editing in the Rhino shell. The heavy lifting comes from JLine, a nice Java library for handling console input. We get command history (using the up and down arrow keys to bring up previous command lines) for free. And with some additional code in Rhino we have limited support for completion.

We don't ship with JLine by default (perhaps we should?), so you'll have to download it yourself. Rhino automatically detects whether JLine is on the classpath and uses it if so and otherwise maintains the previous simple behavior. So to run your shell with JLine your command will look like

java -cp js.jar:lib/jline-0.9.93.jar org.mozilla.javascript.tools.shell.Main


Completion works by looking at the global scope and attempting to complete variables defined there. For example,

js> var obj = {prop1:{prop2:3}};
js> ob

After typing ob and pressing tab, Rhino will autocomplete to obj. It's also smart enough if it sees a dotted property list it will walk it to find names to autocomplete:

js> obj.prop1.pr

Pressing tab after obj.prop1.pr will autocomplete to obj.prop1.prop2.

Some limitations: it's not possible as far as I know to enumerate all the classes or packages in the Java runtime, so autocompletion works somewhat disappointingly for Java classes. I've also found that JLine doesn't work in the Eclipse console, so I just leave the JLine jar off when I'm running in Eclipse.

For more details, see bug 418034.

Thursday, March 27, 2008

Work on Rhino with the Google Summer of Code

The Mozilla project has been accepted for the Google Summer of Code (GSoC), and that means that if you are a student you can apply to the Summer of Code to work on Rhino and receive a stipend for your work.

The application deadline is very close: Monday, March 31st. See the GSoC website for information on how to apply, and the Rhino Wish List for a number of ideas of projects you might do with Rhino.

Good Luck!

Sunday, March 23, 2008

Doctest in Rhino

Python is a fertile ground of good ideas, and we've seen a number of Python's ideas surface in JavaScript recently. JavaScript 1.7's generators and array comprehensions are two recent examples.

I've just implemented another Python idea: doctest. This is a function that will test snippets of shell sessions. It gets its name from its use testing these snippets that appear in documentation comments, but it turns out to be a very convenient way to write tests more generally.

For example, say you've written a new function hello(). I usually go to the shell and play with it to make sure it works correctly:

js> function hello(greetee) {
> return "hello, " + greetee;
> }
js> hello("world");
hello, world
js> hello(3)
hello, 3
js> hello()
hello, undefined


For Rhino we've used a set of JavaScript language tests developed by mozilla.org over many years. They were run by a Perl driver that was recently ported to Java. Until recently I'd would have tested the above code with a test that would be run by that framework. These tests involved a bunch of setup code and then taking each call to hello() above, saving the result value, and calling a comparison function with the actual and expected values. Doctest does this all for me.

In my Rhino implementation of Doctest in addition to a shell function I've written a JUnit runner that finds files with a .doctest extension and runs them. So now all I need to do is copy the shell session above, paste it into hello.doctest, and put it in the right directory and I have a JUnit test! It's much more convenient to write tests, which greatly increases the chances that tests actually get written.

This is available now in the latest version of Rhino (ftp) if you'd like to try it out.