Is it possible to set the optimization level for RhinoScriptEngine in Java 6?
Asked Answered
D

2

7

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?

Doorframe answered 9/8, 2011 at 17:14 Comment(2)
One other option is to copy all of the code from the com.sun.script.javascript package into a new package (or possibly the same one with renamed classes to access package-private members) and change the implementation of makeContext() to set the optimization level, but that seems like overkill.Doorframe
I met same problem. 'Nashorn' in JDK8 is a more advanced JS engine, Nashorn can resolve the 64K problem.Laaspere
O
3

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.

Orphism answered 24/3, 2013 at 20:3 Comment(1)
But isn't that equivalent to an optimization level of -1, which is what the OP wanted? Surely code generation compilation errors should not be occurring under Java 6 if it's always interpreted.Kampmeier
F
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.

Forwardness answered 15/9, 2011 at 18:55 Comment(1)
Hmm, one problem may be that I am using the OpenJDK: $ java -version java version "1.6.0_22" OpenJDK Runtime Environment (IcedTea6 1.10.2) (6b22-1.10.2-0ubuntu1~11.04.1) OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode) The Sun JDK seems to have some differences around Rhino. We had to do some pretty strange things to get things to work on both JDKs for my project, plovr: code.google.com/p/plovr/source/detail?r=7e3cad7f1b9dDoorframe

© 2022 - 2024 — McMap. All rights reserved.