rhino vs spidermonkey

前端 未结 3 1025
暗喜
暗喜 2021-01-30 14: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 languag

3条回答
  •  深忆病人
    2021-01-30 14:48

    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 :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!

提交回复
热议问题