犀牛与蜘蛛猴

发布于 2024-09-15 17:58:33 字数 114 浏览 6 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戴着白色围巾的女孩 2024-09-22 17:58:33

恐怕区别在于它们所用的语言或含义。人们使用 C/C++ 编写各种东西(例如 Firefox),而 Java 在应用程序服务器中最为流行。来自http://en.wikipedia.org/wiki/Rhino_%28JavaScript_engine%29

Rhino 将 JavaScript 脚本转换为
Java 类。 Rhino 可以同时工作
编译模式和解释模式。
它旨在用于
服务器端应用程序,因此有
没有内置的浏览器支持
共同关联的对象
使用 JavaScript。

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

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

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

简而言之,Rhino 允许 JS 与您的代码交互。 SpiderMonkey 更像是一个 DIY 编译器套件,其附加优势是已经构建了很多人都知道的标准语言,您只需向其中添加自定义即可。

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.

溇涏 2024-09-22 17:58:33

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

基本上,我对要安装什么以及安装什么感到有点困惑某种命令行来调用:)

首先,我发现 Bug # rhino (Ubuntu) 中的 705339:“Rhino shell 因 NullPointerException 崩溃” - 并意识到 OpenJDK 实际上安装了 rhino.jar Java 存档。因此,如果你有这个,你可以立即在 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()

 

这对于基本的东西来说已经足够了......但是,如果您想使用 DOM window 对象或 setTimeout() 函数 - 本质上,这些是“浏览器特定的”实现”(对于 setTimeout,另请参阅 SO:7286178),没有浏览器的脚本引擎不会“知道”它们。

 

然而,至少对于 rhino 来说,这是通过 Envjs “浏览器环境”来解决的库,其 rhino 的最新版本 - 1.2env.rhino.js参见SO:6170676 用于 rhino 中的 setTimeout)。

所以我们可以做这样的事情:

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

...并且哎呀 - 它不起作用:)但是,这在 由于 getCurrentContext 错误,无法运行 1.2。 - Env.js | Google 网上论坛

抱歉,Envjs 无法与 java 捆绑的 rhino 一起运行。你需要使用
1.7rc2 可从这里获取:

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

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

sudo apt-get install rhino

... 因为 rhino filelist 指出,此软件包安装 js-1.7R2.jar。该软件包还安装了一个脚本 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

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

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

并尝试在其上调用 rhino

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

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

# 从脚本或命令行运行 env.rhino.js
# 注意优化设置
java -jar lib/js.jar -opt -1 myscript.js

对 - 所以最后,我们只需添加此优化设置,并且:

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

...最终它起作用了:) 编辑:使用 env.rhino.1.2.js,您还可以使用console.log()写入stdout。编辑:要运行rhino Java调试器,请参阅使用less $(which rhino)安装特定的.jar,然后调用java直接(rhino 脚本使用一组不同的命令行开关,因此无法说服调用调试器

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

如果您现在尝试使用 spidermonkey 运行相同的脚本(另请参阅 在Ubuntu 11.04上获取spidermonkey js的最佳方式?),你会得到:

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

...也就是说,spidermonkey无法env.rhino.1.2.js一起使用。

 

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

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!

白首有我共你 2024-09-22 17:58:33

根据他们开发的语言,开发人员需要使用 Spidermonkey 显式处理内存。由于 rhino 是用 java 开发的,其中包括自动垃圾收集,并且从 Java 1.6 版本开始,JVM 的 [ Java 虚拟机 ] 已被调整为比 c 或 c++ 执行速度更快。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文