Can I set the optimization level for RhinoScriptEngine in Java 6?

I ran into a problem when Rhino throws a "Error generating inline code when compiling a script: generated bytecode for a method exceeds 64K limit" exception when running Rhino through the javax.script.ScriptEngine API. The accepted solution is to call setOptimizationLevel(-1) on sun.org.mozilla.javascript.Context .

Unfortunately, I cannot access the Context that ContextFactory creates. I tried adding ContextFactory.Listener to ContextFactory.getGlobal() , which would change Context after creation, but my listener would never be called. I also looked at the source of Java 7 using the makeContext() method in Java 6 to understand 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 start the CoffeeScript compiler . Although, as you can see, the code is much messy, so I would prefer to use the javax.script.ScriptEngine API, if possible, while continuing to support Java 6. Are there any other options?

+6
source share
2 answers

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

Where does he say:

Several components were excluded due to trace and safety reasons:

  • JavaScript-to-bytecode compilation (also called optimizer). This function depends on the class generation library. Removing this JavaScript function will always be interpreted . deleting this function does not affect the execution of the script, since the optimizer is transparent.

The optimizer class was excluded to associate it with JDK6, so the optimization level cannot be set for java 6.

+2
source

I work with 6, and by default it is also set to -1. Rather, if sun.org.mozilla.javascript.internal.optimizer.Codegen not in the classpath, it sets the value to -1. A.

+1
source

Source: https://habr.com/ru/post/894661/


All Articles