Question

I noticed ubuntu 10.04 removed the spidermonkey package. Rhino looks like it's still there though. What are the differences between rhino and spidermonkey (besides what language they're written in). And why did they remove spidermonkey?

Was it helpful?

Solution

I'm afraid the difference is the language they are written in, or what it means. People use C/C++ to write all manner of things (like Firefox) whereas Java is most prevalent in Application Servers. From http://en.wikipedia.org/wiki/Rhino_%28JavaScript_engine%29:

Rhino converts JavaScript scripts into Java classes. Rhino works in both compiled as well as interpreted mode. It is intended to be used in server-side applications, hence there is no built-in support for the browser objects that are commonly associated with JavaScript.

There are three important parts here. Firstly, there's no DOM (also true of SpiderMonkey). Secondly, server side is the intended usage. You're supposed to be able to use Rhino in your big enterprise-y application for automating stuff on a more ad-hoc basis. Finally, the Javascript becomes a class just like the rest of the Java class hierarchy and you can interact with Java classes (see the code sample on that page).

In short, you could quite easily manipulate your POJOs/JPA-based objects/Message Beans/whatever you want to call your "enterprise" Java class, all from within a javascript run through Rhino. Compare this to Jython, where you can use Python syntax and classes to interact with Java. Handy if you have some JavaScript/Python whizzes kicking around the office with nothing to do.

SpiderMonkey by contrast is more like LUA. It's a scripting language. What's the difference? Well, I doubt you get access to printf directly, for one. Rather than being able to access Java classes straight off, you don't get to access C/C++ classes straight off. Rather, you use C/C++ to program extra features of the language.

In short, Rhino allows JS to interact with your code. SpiderMonkey is more like a do-it-yourself compiler kit with the added advantage that a standard language a lot of people know has already been built and you just need to add your customisations to it.

OTHER TIPS

Since I have great difficulties finding basic examples about JavaScript shells/interpreters, especially rhino, on Ubuntu - I'll post this here...

Basically, I was a bit confused on what to install, and what sort of command line to call :)

First of all, I found Bug #705339 in rhino (Ubuntu): “Rhino shell crashes with NullPointerException” - and realized that OpenJDK actually installs a rhino.jar Java archive. So if you have that, you can immediately do in the bash terminal shell:

$ java -jar /usr/lib/jvm/java-6-openjdk/jre/lib/rhino.jar 
Rhino 1.7 release 2 2010 11 17
js> print("answer " + 42.0); 
answer 42
js> quit()

 

And this is all good enough for basic stuff... However, if you want to use DOM window object, or setTimeout() function - essentially, those are "browser specific implementations" (for setTimeout, see also SO:7286178), and a scripting engine without a browser wouldn't "know" about them.

 

However, at least for rhino, that is remedied by the Envjs 'browser environment' library, whose Latest release - 1.2 for rhino is env.rhino.js (see SO:6170676 for setTimeout in rhino).

So we can do something like this:

wget http://www.envjs.com/dist/env.rhino.1.2.js
$ java -jar /usr/lib/jvm/java-6-openjdk/jre/lib/rhino.jar 
Rhino 1.7 release 2 2010 11 17
js> load('env.rhino.1.2.js');
js: "env.rhino.1.2.js", line 1247: uncaught JavaScript runtime exception: TypeError: Cannot call property getCurrentContext in object [JavaPackage org.mozilla.javascript.Context]. It is not a function, it is "object".
    at env.rhino.1.2.js:1247
    at <stdin>:2

js> ^C

... and ooops - it doesn't work :) However, that is clarified in Can't run 1.2 due to getCurrentContext error. - Env.js | Google Groups:

Sorry, Envjs can't run with the rhino bundled with java. you'll need to use 1.7rc2 available from here:

http://www.mozilla.org/rhino/download.html

Thankfully, instead of building from source, in Ubuntu we can directly do:

sudo apt-get install rhino

... since as the rhino filelist states, this package installs js-1.7R2.jar. The package also installs a script rhino, which essentially is a shell wrapper for these Java archives (see less $(which rhino)) - so we can conveniently use that, instead of typing java -jar ... etc:

$ rhino
Rhino 1.7 release 2 2010 11 17
js> load('env.rhino.1.2.js'); // takes a while to load
[  Envjs/1.6 (Rhino; U; Linux i386 2.6.38-11-generic; en-US; rv:1.7.0.rc2) Resig/20070309 PilotFish/1.2.13  ]
js> print("loaded " + 1.2); 
loaded 1.2
js> window;
[Window]
js> ^C

 

However, now try including these same lines as a script, let's call it test.js:

load('env.rhino.1.2.js'); // takes a while to load
print("loaded " + 1.2); 
print(window);

and try calling rhino on it:

$ rhino test.js
loaded 1.2
js: uncaught JavaScript runtime exception: ReferenceError: "window" is not defined.

 

Oh dear - fails again, now what ? :) Well, thankfully, even this is somewhat hinted at in Envjs Guide (note: do allow javascript for that page, otherwise the code will be barely visible) - in particular:

# Running env.rhino.js from a script or the command line
# Note the optimization setting
java -jar lib/js.jar -opt -1 myscript.js

Right - so finally, we simply add this optimization setting, and:

$ rhino -opt -1 test.js
[  Envjs/1.6 (Rhino; U; Linux i386 2.6.38-11-generic; en-US; rv:1.7.0.rc2) Resig/20070309 PilotFish/1.2.13  ]
loaded 1.2
[Window]

... finally it works :) EDIT: with env.rhino.1.2.js, you can also use console.log() to write to stdout. EDIT: To run the rhino Java debugger, see using less $(which rhino) where are the particular .jars installed, and then call java directly (the rhino script uses a different set of command line switches, and so cannot be persuaded to call the debugger):

java -cp /usr/share/java/js.jar org.mozilla.javascript.tools.debugger.Main test.js

 

If you try to run the same script now with spidermonkey (see also PPA instructions in Best way to get spidermonkey js on Ubuntu 11.04?), you will get:

$ js test.js
env.rhino.1.2.js:1247: ReferenceError: Packages is not defined

... that is, spidermonkey will not work with env.rhino.1.2.js.

 

Well, hope this helps someone,
Cheers!

Based on the languages they have been developed, developer needs explicitly handle memory using spidermonkey. As rhino has been developed in java which includes automatic garbage collection and JVM's [ Java Virtual Machine ] have been tuned for faster execution than c or c++ from Java 1.6 Version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top