incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r931522 - in /sling/trunk/bundles/scripting/javascript/src: main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngine.java test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java
Date Wed, 07 Apr 2010 12:32:08 GMT
Author: cziegeler
Date: Wed Apr  7 12:32:08 2010
New Revision: 931522

URL: http://svn.apache.org/viewvc?rev=931522&view=rev
Log:
SLING-1111 : RhinoJavaScriptEngine does not preserve scope between eval()s

Added:
    sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java
  (with props)
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=931522&r1=931521&r2=931522&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 Apr  7 12:32:08 2010
@@ -47,6 +47,7 @@ import org.slf4j.Logger;
 public class RhinoJavaScriptEngine extends AbstractSlingScriptEngine {
 
     private Scriptable rootScope;
+    private Scriptable scope;
 
     public RhinoJavaScriptEngine(ScriptEngineFactory factory,
             Scriptable rootScope) {
@@ -72,31 +73,32 @@ public class RhinoJavaScriptEngine exten
 
         // container for replaced properties
         Map<String, Object> replacedProperties = null;
-        Scriptable scope = null;
 
         // create a rhino Context and execute the script
         try {
 
             final Context rhinoContext = Context.enter();
 
-            if (ScriptRuntime.hasTopCall(rhinoContext)) {
+            if (ScriptRuntime.hasTopCall(rhinoContext) && scope == null) {
 
                 // 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
-                scope = new ImporterTopLevel(); // new NativeObject();
-
-                // Set the global scope to be our prototype
-                scope.setPrototype(rootScope);
-
-                // We want "scope" to be a new top-level scope, so set its
-                // parent scope to null. This means that any variables created
-                // by assignments will be properties of "scope".
-                scope.setParentScope(null);
+                if (scope == null) {
+                    // create the request top scope, use the ImporterToplevel here
+                    // to support the importPackage and importClasses functions
+                    scope = new ImporterTopLevel(); // new NativeObject();
+
+                    // Set the global scope to be our prototype
+                    scope.setPrototype(rootScope);
+
+                    // We want "scope" to be a new top-level scope, so set its
+                    // parent scope to null. This means that any variables created
+                    // by assignments will be properties of "scope".
+                    scope.setParentScope(null);
+                }
 
                 // setup the context for use
                 WrapFactory wrapFactory = ((RhinoJavaScriptEngineFactory) getFactory()).getWrapFactory();
@@ -110,7 +112,7 @@ public class RhinoJavaScriptEngine exten
             final Object securityDomain = null;
 
             return rhinoContext.evaluateReader(scope, scriptReader, scriptName,
-                lineNumber, securityDomain);
+                    lineNumber, securityDomain);
 
         } catch (JavaScriptException t) {
 

Added: sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java?rev=931522&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java
(added)
+++ sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java
Wed Apr  7 12:32:08 2010
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.scripting.javascript.internal;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+
+import junit.framework.TestCase;
+
+import org.apache.sling.scripting.javascript.helper.SlingWrapFactory;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ImporterTopLevel;
+import org.mozilla.javascript.Scriptable;
+
+public class RhinoJavaScriptEngineTest extends TestCase {
+
+    public void testPreserveScopeBetweenEvals() throws ScriptException {
+        MockRhinoJavaScriptEngineFactory factory = new MockRhinoJavaScriptEngineFactory();
+        ScriptEngine engine = factory.getScriptEngine();
+        engine.eval("var f = 1");
+        Object result = null;
+        try {
+            result = engine.eval("f += 1");
+        } catch (ScriptException e) {
+            TestCase.fail(e.getMessage());
+        }
+        assertTrue(result instanceof Double);
+        assertEquals(2.0, result);
+    }
+
+    private static class MockRhinoJavaScriptEngineFactory extends RhinoJavaScriptEngineFactory
{
+
+        protected SlingWrapFactory wrapFactory;
+
+        @Override
+        public ScriptEngine getScriptEngine() {
+            final Context rhinoContext = Context.enter();
+            Scriptable scope = rhinoContext.initStandardObjects(new ImporterTopLevel(), false);
+            return new RhinoJavaScriptEngine(this, scope);
+        }
+
+        @Override
+        SlingWrapFactory getWrapFactory() {
+            if (wrapFactory == null) {
+                wrapFactory = new SlingWrapFactory();
+            }
+            return wrapFactory;
+        }
+    }
+
+}

Propchange: sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message