javascript 犀牛 vs 蜘蛛猴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3563909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:31:48  来源:igfitidea点击:

rhino vs spidermonkey

javascriptrhinoubuntu-10.04spidermonkey

提问by Tyler Gillies

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?

我注意到 ubuntu 10.04 删除了 spidermonkey 包。犀牛看起来它仍然存在。犀牛和蜘蛛猴之间有什么区别(除了它们是用什么语言编写的)。他们为什么要移除蜘蛛猴?

采纳答案by Tyler Gillies

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:

恐怕区别在于它们是用什么语言编写的,或者是什么意思。人们使用 C/C++ 来编写各种各样的东西(比如 Firefox),而 Java 在应用服务器中最为流行。来自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.

Rhino 将 JavaScript 脚本转换为 Java 类。Rhino 可以在编译模式和解释模式下工作。它旨在用于服务器端应用程序,因此没有对通常与 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).

这里有三个重要的部分。首先,没有 DOM(SpiderMonkey 也是如此)。其次,服务器端是预期用途。您应该能够在您的大型企业应用程序中使用 Rhino 来在更临时的基础上自动化操作。最后,Javascript 成为一个类,就像 Java 类层次结构的其余部分一样,您可以与 Java 类交互(请参阅该页面上的代码示例)。

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.

简而言之,您可以很容易地操作您的 POJO/基于 JPA 的对象/消息 Beans/任何您想调用您的“企业”Java 类的东西,所有这些都在通过 Rhino 运行的 javascript 中进行。将此与 Jython 进行比较,在 Jython 中您可以使用 Python 语法和类与 Java 交互。如果你有一些 JavaScript/Python 高手在办公室里闲逛,无所事事,那就很方便了。

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.

相比之下,SpiderMonkey 更像是 LUA。它是一种脚本语言。有什么不同?好吧,我怀疑您是否可以直接访问 printf,一方面。您不能直接访问 Java 类,而不能直接访问 C/C++ 类。相反,您使用 C/C++ 来编程语言的额外功能。

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 允许 JS 与您的代码进行交互。SpiderMonkey 更像是一个自己动手做的编译器工具包,它具有额外的优势,即很多人都知道的标准语言已经构建完成,您只需要向其中添加自定义项即可。

回答by sdaau

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

由于我很难找到有关 JavaScript shell/解释器的基本示例,尤其是rhino在 Ubuntu 上 - 我将在此处发布...

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.jarJava archive. So if you have that, you can immediately do in the bash terminal shell:

首先,我在 rhino (Ubuntu) 中发现了Bug #705339:“Rhino shell crashes with NullPointerException”——并意识到 OpenJDK 实际上安装了一个rhino.jarJava 存档。因此,如果您拥有它,您可以立即在 bash 终端 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 windowobject, 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.

这对于基本的东西来说已经足够了......但是,如果你想使用 DOMwindow对象或setTimeout()函数 - 本质上,那些是“浏览器特定的实现”(对于setTimeout,另见SO:7286178),以及一个没有浏览器不会“知道”它们。

 

 

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

但是,至少对于rhinoEnvjs“浏览器环境”库对此进行了补救,其最新版本 - 1.2forrhinoenv.rhino.js参见SO:6170676for setTimeoutinrhino)。

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:

...和哎呀 - 它不起作用:) 但是,由于 getCurrentContext 错误,无法运行 1.2中对此进行了澄清- Env.js | 谷歌群组

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

抱歉,Envjs 无法与 java 捆绑的 rhino 一起运行。您需要使用此处提供的 1.7rc2:

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

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

值得庆幸的是,在 Ubuntu 中,我们可以直接执行以下操作,而不是从源代码构建:

sudo apt-get install rhino

... since as the rhino fileliststates, 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:

...因为正如犀牛文件列表所述,这个包安装js-1.7R2.jar. 该包还安装了一个 script rhino,它本质上是这些 Java 档案的 shell 包装器(请参阅 参考资料less $(which rhino))——因此我们可以方便地使用它,而不是键入java -jar ...等:

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

但是,现在尝试将这些相同的行包含为脚本,让我们称之为test.js

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

and try calling rhinoon it:

并尝试调用rhino它:

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

哦,天哪 - 又失败了,现在怎么办?:) 好吧,谢天谢地,即使这在Envjs 指南中也有所暗示(注意:请允许该页面使用 javascript,否则代码将几乎不可见) - 特别是:

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

# 从脚本或命令行运行 env.rhino.js
# 注意优化设置
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 rhinoJava debugger, see using less $(which rhino)where are the particular .jars installed, and then call javadirectly (the rhinoscript uses a different set of command line switches, and so cannot be persuaded to call the debugger):

...终于它起作用了:) 编辑:使用env.rhino.1.2.js,您也可以使用console.log()写入标准输出。编辑:要运行rhinoJava调试器,请参阅使用less $(which rhino)在安装了特定.jar文件,然后调用java直接(rhino脚本使用一组不同的命令行开关的,因此不能被说服调用调试器):

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:

如果您现在尝试运行相同的脚本spidermonkey(另请参阅在 Ubuntu 11.04 上获取 Spidermonkey js 的最佳方式中的PPA 说明),您将获得:

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

... that is, spidermonkeywill notwork with env.rhino.1.2.js.

...也就是说,spidermonkey不会一起工作env.rhino.1.2.js

 

 

Well, hope this helps someone,
Cheers!

好吧,希望这对某人
有所帮助,干杯!

回答by Ratna Dinakar

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.

基于他们开发的语言,开发人员需要使用蜘蛛猴显式处理内存。由于 rhino 是在 Java 中开发的,其中包括自动垃圾收集和 JVM 的 [Java 虚拟机] 已被调整为比 Java 1.6 版本的 c 或 c++ 更快的执行。