使用 Rhino 在 Java 中编译 JavaScript

发布于 2024-12-14 13:02:35 字数 914 浏览 1 评论 0原文

我正在我的 Java 应用程序中测试 Rhino JavaScript 脚本引擎。

启动时,我加载所有 .js 文件并在 Java 中编译它们,如下所示:

这一切都很好,但是离线 .js->java .class 编译器:http://www.mozilla.org/rhino/jsc.html 有各种奇特的东西优化:http://www.mozilla.org/rhino/opt.html

如何我是否可以在 Java 内部进行这些优化编译?

他们的 API 文档也有吗?他们主页上的 API 文档链接已损坏; http://www.mozilla.org/rhino/doc.html

编辑:我无法使用离线编译器,因为我希望用户可以轻松拖放脚本并重新启动。

I'm testing out Rhino JavaScript script engine inside my Java application.

At startup, I load all .js files and compile them inside of Java like this:

That is all well and great, but the offline .js->java .class compiler: http://www.mozilla.org/rhino/jsc.html has all sorts of fancy optimizations: http://www.mozilla.org/rhino/opt.html

How do I compile inside of Java with those optimizations?

Also were is their API docs for this? Their API doc link on their main page is broke; http://www.mozilla.org/rhino/doc.html

Edit: I can't use the offline compiler because I want it to be easy for users to drag/drop their scripts in and restart.

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

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

发布评论

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

评论(1

[旋木] 2024-12-21 13:02:35

toolsrc\org\mozilla\javascript\tools\jsc\ 中的 Main.java 应该会给你一些如何做到这一点的想法。

基本上,您可以使用 CompilerEnvirons 类设置优化级别。之后,您应该使用 CompilerEnvirons 作为第一个参数来创建 ClassCompiler 对象的新实例。

从源头看:

compilerEnv = new CompilerEnvirons();
...
compiler = new ClassCompiler(compilerEnv);
...
compilerEnv.setOptimizationLevel(optLevel);

Main.java inside toolsrc\org\mozilla\javascript\tools\jsc\ should give you some idea how to do it.

Basically, you set optimization level using CompilerEnvirons class. After that, you should create a new instance of the ClassCompiler object using CompilerEnvirons as the first parameter.

From the source:

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