Lately, I’ve been working on JavaScript stuff at work. When it comes to JavaScript debugging, Firebug can take you a long way. Firebug is not, however, a very scriptable environment.
SpiderMonkey is “Gecko’s JavaScript engine written in C”. For our purposes, SpiderMonkey is a command-line tool to execute JavaScript.
You can build it from source or look for it in your system’s package manager.
Before I continue, SpiderMonkey defines a few functions which are useful for non-browser environments. In the following examples, I’ll be using the print function which outputs a string to STDOUT.
Interactive mode:
Execute (-e) mode:
File (-f) mode:
File (-f) mode (multiple files):
Combined mode:
Apart from “print”, there are other useful functions:
Command Usage Description ======= ===== =========== load load(['foo.js' ...]) Load files named by string arguments readline readline() Read a single line from stdin print print([exp ...]) Evaluate and print expressions help help([name ...]) Display usage and help messages quit quit() Quit the shell clear clear([obj]) Clear properties of object
This is an abridged version of the available functions, here’s the full one.
The ability to import other files (load), read from STDIN (readline), output messages (print), and quit are exactly what’s needed for scripting. In fact, while I was googling for SpiderMonkey, I found this post which used SpiderMonkey as a primitive CGI script.
More realistically, because SpiderMonkey is MUCH faster to startup than Rhino, you can include it in a command-line workflow to unit test or lint your JavaScript.
[…] 21, 2009 by Jonathan Palardy I’ve talked about SpiderMonkey before. Being able to instantly evaluate JavaScript code is great but you can use FireBug for that. I […]
how do you use firebug for automated unittesting?
too bad readline() implementation sucks. :(
and no fileOpen because of security issues? c`mon. that’s plain idiotic.
Thanks for the useful post. There is a typo near the top: ‘SpideMonkey’ instead of ‘SpiderMonkey’.
Oh, thanks — I fixed it.
can write a introduction about a spider monkeys pleassssssssssssssssssssssssssssssssssssssssseeeeeeee