incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1603776 - in /sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal: RhinoJavaScriptEngine.java RhinoJavaScriptEngineFactory.java
Date Thu, 19 Jun 2014 09:27:51 GMT
Author: bdelacretaz
Date: Thu Jun 19 09:27:51 2014
New Revision: 1603776

URL: http://svn.apache.org/r1603776
Log:
SLING-3635 - configurable optimization level for Rhino, contributed by Marius-Andrei Danila,
thanks!

Modified:
    sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java
    sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactory.java

Modified: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java?rev=1603776&r1=1603775&r2=1603776&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java
(original)
+++ sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java
Thu Jun 19 09:27:51 2014
@@ -80,6 +80,7 @@ public class RhinoJavaScriptEngine exten
         try {
 
             final Context rhinoContext = Context.enter();
+            rhinoContext.setOptimizationLevel(optimizationLevel());
 
             if (ScriptRuntime.hasTopCall(rhinoContext)) {
                 // reuse the top scope if we are included
@@ -226,4 +227,8 @@ public class RhinoJavaScriptEngine exten
             }
         }
     }
+
+    private int optimizationLevel() {
+        return ((RhinoJavaScriptEngineFactory)getFactory()).getOptimizationLevel();
+    }
 }

Modified: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactory.java?rev=1603776&r1=1603775&r2=1603776&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactory.java
(original)
+++ sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactory.java
Thu Jun 19 09:27:51 2014
@@ -25,6 +25,7 @@ import java.util.Set;
 import javax.script.ScriptEngine;
 
 import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.ReferencePolicy;
@@ -60,14 +61,22 @@ import org.slf4j.LoggerFactory;
  * The <code>RhinoJavaScriptEngineFactory</code> TODO
  *
  */
-@Component
+@Component(metatype = true)
 @Service(value=javax.script.ScriptEngineFactory.class)
 @Reference(name="HostObjectProvider", referenceInterface=RhinoHostObjectProvider.class,
            cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC,
            bind="addHostObjectProvider", unbind="removeHostObjectProvider")
+@Property(
+        name = RhinoJavaScriptEngineFactory.OPTIMIZATION_CONFIG,
+        intValue = RhinoJavaScriptEngineFactory.DEFAULT_OPTIMIZATION_LEVEL,
+        description = "The level of optimization for the bytecode generated by Rhino. Provide
values between 0-9, 9 being the most aggressive level of optimization. A value of -1 will
run scripts in interpreted mode")
 public class RhinoJavaScriptEngineFactory extends AbstractScriptEngineFactory
         implements ScopeProvider {
 
+    public final static String OPTIMIZATION_CONFIG = "org.apache.sling.scripting.javascript.rhino.optLevel";
+
+    public final static int DEFAULT_OPTIMIZATION_LEVEL = - 1;
+
     public final static String ECMA_SCRIPT_EXTENSION = "ecma";
 
     public final static String ESP_SCRIPT_EXTENSION = "esp";
@@ -81,6 +90,8 @@ public class RhinoJavaScriptEngineFactor
     /** default log */
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    private int optimizationLevel;
+
     private String languageVersion;
 
     private SlingWrapFactory wrapFactory;
@@ -104,6 +115,16 @@ public class RhinoJavaScriptEngineFactor
         return languageVersion;
     }
 
+    /**
+     * Get the optimization level that should be used when running JS scripts
+     * with Rhino
+     * @return an integer from 0-9 with 9 being the most aggressive optimization, or
+     * -1 if interpreted mode is to be used
+     */
+    public int getOptimizationLevel() {
+        return optimizationLevel;
+    }
+
     public Object getParameter(String name) {
         if ("THREADING".equals(name)) {
             return "MULTITHREADED";
@@ -178,6 +199,8 @@ public class RhinoJavaScriptEngineFactor
             "org.apache.sling.scripting.javascript.debug", props,
             context.getBundleContext(), false);
 
+        optimizationLevel = readOptimizationLevel(props);
+
         // setup the wrap factory
         wrapFactory = new SlingWrapFactory();
 
@@ -204,6 +227,8 @@ public class RhinoJavaScriptEngineFactor
         if ( dclm != null ) {
             contextFactory.initApplicationClassLoader(dynamicClassLoaderManager.getDynamicClassLoader());
         }
+        
+        log.info("Activated with optimization level {}", optimizationLevel);
     }
 
     protected void deactivate(ComponentContext context) {
@@ -306,4 +331,15 @@ public class RhinoJavaScriptEngineFactor
                 ? Boolean.parseBoolean(String.valueOf(value))
                 : defaultValue;
     }
+
+    private int readOptimizationLevel(Dictionary<?, ?> dictionary) {
+        Object optLevelObj = dictionary.get(OPTIMIZATION_CONFIG);
+        int optLevel = optLevelObj == null ?
+                DEFAULT_OPTIMIZATION_LEVEL :
+                Integer.parseInt(String.valueOf(optLevelObj));
+        if (!Context.isValidOptimizationLevel(optLevel)) {
+            optLevel = DEFAULT_OPTIMIZATION_LEVEL;
+        }
+        return optLevel;
+    }
 }



Mime
View raw message