rhino vs spidermonkey
Asked Answered
M

3

24

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?

Marylyn answered 25/8, 2010 at 7:58 Comment(1)
Related Ubuntu Bug report discussed hereTrujillo
V
18

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.

Viv answered 12/12, 2010 at 14:2 Comment(2)
Thanks - interesting and helpful. But Ubuntu often has more than one package to address a particular problem. So the question remains: "why did they remove spidermonkey"?Mordancy
@Mordancy I'm honestly not sure. Perhaps whoever maintained the package for Ubuntu stopped doing so and they dropped it? I believe a requirement of being in "main" is sufficient regular maintenance and tracking upstream etc. It has been a while since I wrote this, and since I've used Ubuntu, but back when I did, I remember a number of packages being dropped from "main" to "universe" due to maintenance issues. I suspect given the rise of PPA, you can probably get hold of SpiderMonkey if you want to.Viv
H
6

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!

Hyper answered 3/9, 2011 at 10:31 Comment(0)
B
-1

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.

Bushman answered 6/12, 2010 at 12:5 Comment(6)
-1 for false statement: "developer needs explicitly handle memory using spidermonkey". JavaScript is a garbage-collected language, regardless of what language it is implemented in. The Spidermonkey shell in particular has the following function exposed on the global object: gczeal(level) - "How zealous the garbage collector should be". So clearly Spidermonkey also has garbage collection. Also, citation needed on statement: "JVM's have been tuned for faster execution than c or c++ from Java 1.6 Version". Not true in most cases, according to bit.ly/fyyrTXAtavistic
developer.mozilla.org/en/SpiderMonkey_Garbage_Collection_Tips this link clearly gives some tips for handling memory ! Also, I guess question was about integrating with API. Please reviewBushman
@echo-flow - Spidermonkey has garbage collection, but the point by Ratna was about developing at the javascript interpreter level (i.e. extending or integrating Spidermonkey), not in javascript. That said, this answer is very much language based...Zacharia
@ratna, link just tells us that for this particular test the JVM 6 is able to reach C++ speed (not all the time, look at 4x slower tests), and even surpass it sometimes (consuming much more memory, and it was probably not reclaimed). It means that the real performance is dependent on your applicaiton, your algorithm and so on. So it is not true ""JVM's have been tuned for faster execution than c or c++ from Java 1.6 Version". The tuning is done for JVM'S, and also for C++ compilers, the theory states JIT can give better results, but at least today, the C++ compiler still wins in most cases.Minutia
@bartosz I agree. C++ compiler still wins in the most cases. Please review this link, I will also try to provide other links too java.sun.com/performance/reference/whitepapers/…Bushman
@ratna I know this link, I am a Java programmer, and I had to tune JVM settings too many times now :D, anyway, we are probably going too much out of topic right now.Minutia

© 2022 - 2024 — McMap. All rights reserved.