sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1068874 - /sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java
Date Wed, 09 Feb 2011 13:00:04 GMT
Author: fmeschbe
Date: Wed Feb  9 13:00:04 2011
New Revision: 1068874

URL: http://svn.apache.org/viewvc?rev=1068874&view=rev
Log:
SLING-1972 copy back properties of the top level scope to the Engine scope bindings after
the script ended without errors.

Modified:
    sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.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=1068874&r1=1068873&r2=1068874&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
Wed Feb  9 13:00:04 2011
@@ -73,6 +73,7 @@ public class RhinoJavaScriptEngine exten
         // container for replaced properties
         Map<String, Object> replacedProperties = null;
         Scriptable scope = null;
+        boolean isTopLevelCall = false;
 
         // create a rhino Context and execute the script
         try {
@@ -82,6 +83,7 @@ public class RhinoJavaScriptEngine exten
             if (ScriptRuntime.hasTopCall(rhinoContext)) {
                 // reuse the top scope if we are included
                 scope = ScriptRuntime.getTopCallScope(rhinoContext);
+
             } else {
                 // create the request top scope, use the ImporterToplevel here
                 // to support the importPackage and importClasses functions
@@ -98,6 +100,9 @@ public class RhinoJavaScriptEngine exten
                 // setup the context for use
                 WrapFactory wrapFactory = ((RhinoJavaScriptEngineFactory) getFactory()).getWrapFactory();
                 rhinoContext.setWrapFactory(wrapFactory);
+
+                // this is the top level call
+                isTopLevelCall = true;
             }
 
             // add initial properties to the scope
@@ -111,6 +116,9 @@ public class RhinoJavaScriptEngine exten
 
         } catch (JavaScriptException t) {
 
+            // prevent variables to be pushed back in case of errors
+            isTopLevelCall = false;
+
             final ScriptException se = new ScriptException(t.details(),
                 t.sourceName(), t.lineNumber());
 
@@ -137,6 +145,9 @@ public class RhinoJavaScriptEngine exten
 
         } catch (Throwable t) {
 
+            // prevent variables to be pushed back in case of errors
+            isTopLevelCall = false;
+
             final ScriptException se = new ScriptException(
                 "Failure running script " + scriptName + ": " + t.getMessage());
             se.initCause(t);
@@ -144,6 +155,12 @@ public class RhinoJavaScriptEngine exten
 
         } finally {
 
+            // if we are the top call (the Context is now null) we have to
+            // play back any properties from the scope back to the bindings
+            if (isTopLevelCall) {
+                getBoundProperties(scope, bindings);
+            }
+
             // if properties have been replaced, reset them
             resetBoundProperties(scope, replacedProperties);
 
@@ -174,6 +191,23 @@ public class RhinoJavaScriptEngine exten
         return replacedProperties;
     }
 
+    private void getBoundProperties(Scriptable scope, Bindings bindings) {
+        Object[] ids = scope.getIds();
+        for (Object id : ids) {
+            if (id instanceof String) {
+                String key = (String) id;
+                Object value = scope.get(key, scope);
+                if (value != Scriptable.NOT_FOUND) {
+                    if (value instanceof Wrapper) {
+                        bindings.put(key, ((Wrapper) value).unwrap());
+                    } else {
+                        bindings.put(key, value);
+                    }
+                }
+            }
+        }
+    }
+
     private void resetBoundProperties(Scriptable scope,
             Map<String, Object> properties) {
         if (scope != null && properties != null && properties.size() >
0) {



Mime
View raw message