是否可以在 Java 6 中设置 RhinoScriptEngine 的优化级别?

发布于 2024-11-29 05:38:32 字数 1478 浏览 0 评论 0 原文

我遇到了 Rhino 抛出 通过 javax.script.ScriptEngine 运行 Rhino 时出现“编译脚本时遇到代码生成错误:方法生成的字节码超出 64K 限制” 异常API。可接受的解决方案似乎是在 sun.org.mozilla.javascript.Context 上调用 setOptimizationLevel(-1)

不幸的是,我似乎无法访问由 ContextFactory 创建的 Context。我尝试向 ContextFactory.getGlobal() 中添加一个 ContextFactory.Listener 来在创建后修改 Context,但我的监听器似乎永远不会得到叫。我还查看了 来自 Java 6 的 RhinoScriptEngine 源代码,看看是否有一个属性可以设置,ContextFactory 可以从中读取以确定优化级别的值。

据我所知,在 Java 7 中,RhinoScriptEngine 默认将优化级别设置为 -1,并且可以设置优化通过 rhino.opt.level 属性设置级别。比较 makeContext() 方法"noreferrer">Java 7 版本,在 makeContext() 方法href="http://javasourcecode.org/html/open-source/jdk/jdk-6u23/com/sun/script/javascript/RhinoScriptEngine.java.html" rel="noreferrer">Java 6 版本看看我的意思。

据我所知,我相信我最好的选择是直接运行Rhino,如图所示 本例中使用 Rhino 运行 CoffeeScript 编译器。尽管如您所见,代码更加混乱,所以如果可能的话,我更愿意使用 javax.script.ScriptEngine API,同时继续支持 Java 6。还有其他选择吗?

I am running into the issue where Rhino throws the "Encountered code generation error while compiling script: generated bytecode for method exceeds 64K limit" exception when running Rhino via the javax.script.ScriptEngine API. The accepted solution appears to be to invoke setOptimizationLevel(-1) on the sun.org.mozilla.javascript.Context.

Unfortunately, I cannot seem to access the Context that is created by the ContextFactory. I have tried adding a ContextFactory.Listener to ContextFactory.getGlobal() that would modify the Context after creation, but my listener never seems to get called. I also took a look at the RhinoScriptEngine source from Java 6 to see whether there was a property that I could set that the ContextFactory would read from in order to determine the value of the optimization level.

As far as I can tell, in Java 7, RhinoScriptEngine sets the optimization level to -1 by default and makes it possible to set the optimization level via the rhino.opt.level property. Compare the makeContext() method in the Java 7 version with the makeContext() method in the Java 6 version to see what I mean.

As far as I can tell, I believe that my best option is to run Rhino directly, as shown in this example of using Rhino to run the CoffeeScript compiler. Though as you can see, the code is a lot messier, so I would prefer to use the javax.script.ScriptEngine API, if possible, while continuing to support Java 6. Are there any other options?

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

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

发布评论

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

评论(2

错爱 2024-12-06 05:38:32

,根据文档:http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html#jsengine

它说:

由于占用空间和安全原因,一些组件已被排除:

  1. JavaScript 到字节码的编译(也称为“优化器”)。这
    功能取决于类生成库。去除这个
    该功能意味着 JavaScript 将始终被解释。这
    删除此功能不会影响脚本执行,因为
    优化器是透明的。

优化器类已被排除在外,因为它与 JDK6 捆绑在一起,因此无法为 java 6 设置优化级别。

No, according to the documentation: http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html#jsengine

Where it says:

A few components have been excluded due to footprint and security reasons:

  1. JavaScript-to-bytecode compilation (also called "optimizer"). This
    feature depends on a class generation library. The removal of this
    feature means that JavaScript will always be interpreted. The
    removal of this feature does not affect script execution because the
    optimizer is transparent.

The optimizer class has been excluded for bundling it with JDK6 therefore optimization level cannot be set for java 6.

笑咖 2024-12-06 05:38:32

我运行的是 6,默认情况下它似乎也设置为 -1。或者更确切地说,除非 sun.org.mozilla.javascript.internal.optimizer.Codegen 位于类路径上,否则它会设置为 -1。

I'm running with 6 and it also appears to be set to -1 by default. Or rather, unless sun.org.mozilla.javascript.internal.optimizer.Codegen is on the classpath, it's set to -1.

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