Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

rhino

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?

like image 517
bolinfest Avatar asked Aug 09 '11 17:08

bolinfest


2 Answers

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.

like image 151
Ben-Hur Langoni Junior Avatar answered Nov 15 '22 09:11

Ben-Hur Langoni Junior


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.

like image 36
nilskp Avatar answered Nov 15 '22 09:11

nilskp