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?