我遇到了 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?
发布评论
评论(2)
否,根据文档:http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html#jsengine
它说:
优化器类已被排除在外,因为它与 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:
The optimizer class has been excluded for bundling it with JDK6 therefore optimization level cannot be set for java 6.
我运行的是 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.