Sunday, July 6, 2008

Run Javascript under Windows using Mozilla's interpreter - SpiderMonkey

Javascript is widely used in Web programming. Microsoft's Internet Explorer and Mozilla's Firefox both include its Javascript interpreter in the browsers. However, they also limit your capability of running Javascript in a web-only environment.

The good news is, Mozilla's interpreter was released as an open source. You can find it from http://www.mozilla.org/js/. It has two versions, one is written in C and is called SpiderMonkey. The other is written in Java and called Rhino.

Today, I installed SpiderMonkey on a windows xp computer. The installation is very simple:
1) Install Mozilla Developer Center - MozillaBuild
2) Go to SpiderMonkey's website to get the latestest version and build it under MozillaBuild.

After installation, you will get the executable interpreter js.exe in a directory whose name depends on the system you're building on (you can find this direcotry under mozilla/js/src/ with a suffix .OBJ).

If you are not accustomed to the MozillaBuild environment (unix-like) and is comfortable with Windows/Dos, you can modify the system environment by adding the path of js.exe into the PATH variable (right click "My computer" in Windows Explorer, and then select Advanced -> Environment Variables). Then under the DOS prompt environment (in Windows, Start -> Run -> cmd) , you can use "js file.js" to run your javascripts.