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?
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.
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.
So we can do something like this:
... 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:
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
load('env.rhino.1.2.js'); // takes a while to load print("loaded " + 1.2); print(window);
and try calling
rhino on it:
# 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):
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
Well, hope this helps someone,
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.
©2020 All rights reserved.