You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2019/12/02 11:21:22 UTC

[sling-org-apache-sling-scripting-javascript] branch master updated: SLING-8865 - Enhance the HTL runtime and script engine to take advantage of the support for lazy bindings

This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-javascript.git


The following commit(s) were added to refs/heads/master by this push:
     new e2c0d0e  SLING-8865 - Enhance the HTL runtime and script engine to take advantage of the support for lazy bindings
e2c0d0e is described below

commit e2c0d0e678789e14bfd973c12e76daff71f86191
Author: Radu Cotescu <17...@users.noreply.github.com>
AuthorDate: Mon Dec 2 12:21:16 2019 +0100

    SLING-8865 - Enhance the HTL runtime and script engine to take advantage of the support for lazy bindings
    
    * expand LazyBindings.Supplier in the SlingWrapFactory; the HTL JS Use Provider uses
    o.a.s.scripting.javascript for its engine
---
 pom.xml                                            |  3 ++-
 .../javascript/helper/SlingWrapFactory.java        |  9 +++++++--
 .../javascript/wrapper/ScriptableResourceTest.java | 23 +++++++++++++++++++++-
 3 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/pom.xml b/pom.xml
index e582c13..1e1676d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -44,6 +44,7 @@
 
     <properties>
         <rhino.version>1.7.7.1_1</rhino.version>
+        <sling.java.version>8</sling.java.version>
     </properties>
 
     <build>
@@ -102,7 +103,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.1.0</version>
+            <version>2.21.0</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
diff --git a/src/main/java/org/apache/sling/scripting/javascript/helper/SlingWrapFactory.java b/src/main/java/org/apache/sling/scripting/javascript/helper/SlingWrapFactory.java
index f625489..f2231d2 100644
--- a/src/main/java/org/apache/sling/scripting/javascript/helper/SlingWrapFactory.java
+++ b/src/main/java/org/apache/sling/scripting/javascript/helper/SlingWrapFactory.java
@@ -21,6 +21,7 @@ package org.apache.sling.scripting.javascript.helper;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.sling.api.scripting.LazyBindings;
 import org.mozilla.javascript.Context;
 import org.mozilla.javascript.Scriptable;
 import org.mozilla.javascript.WrapFactory;
@@ -66,8 +67,12 @@ public class SlingWrapFactory extends WrapFactory {
             log.warn("Cannot Wrap " + javaObject, e);
         }
 
-        if(result==null) {
-            result = super.wrapAsJavaObject(cx, scope, javaObject, staticType);
+        if (result == null) {
+            if (javaObject instanceof LazyBindings.Supplier) {
+                result = super.wrapAsJavaObject(cx, scope, ((LazyBindings.Supplier) javaObject).get(), staticType);
+            } else {
+                result = super.wrapAsJavaObject(cx, scope, javaObject, staticType);
+            }
         }
 
         return result;
diff --git a/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResourceTest.java b/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResourceTest.java
index 277aee8..5d250b5 100644
--- a/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResourceTest.java
+++ b/src/test/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResourceTest.java
@@ -47,6 +47,7 @@ import org.apache.sling.commons.testing.sling.MockResourceResolver;
 import org.apache.sling.jcr.resource.JcrResourceConstants;
 import org.apache.sling.scripting.javascript.RepositoryScriptingTestBase;
 import org.apache.sling.scripting.javascript.internal.ScriptEngineHelper;
+import org.jetbrains.annotations.NotNull;
 import org.mozilla.javascript.Wrapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -284,6 +285,11 @@ public class ScriptableResourceTest extends RepositoryScriptingTestBase {
             return RESOURCE_RESOLVER;
         }
 
+        @Override
+        public @NotNull ValueMap getValueMap() {
+            return adaptTo(ValueMap.class);
+        }
+
         public String getResourceType() {
             return RESOURCE_TYPE;
         }
@@ -292,6 +298,16 @@ public class ScriptableResourceTest extends RepositoryScriptingTestBase {
             return RESOURCE_SUPER_TYPE;
         }
 
+        @Override
+        public boolean hasChildren() {
+            try {
+                return node.hasNodes();
+            } catch (RepositoryException e) {
+                // do nothing
+            }
+            return false;
+        }
+
         @SuppressWarnings("unchecked")
         public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
             if (type == Node.class || type == Item.class) {
@@ -409,6 +425,11 @@ public class ScriptableResourceTest extends RepositoryScriptingTestBase {
         }
 
         public Iterator<Resource> listChildren() {
+            return getChildren().iterator();
+        }
+
+        @Override
+        public @NotNull Iterable<Resource> getChildren() {
             try
             {
                 List<Resource> childList = new ArrayList<Resource>();
@@ -418,7 +439,7 @@ public class ScriptableResourceTest extends RepositoryScriptingTestBase {
                     Node nextNode = it.nextNode();
                     childList.add( new TestResource( nextNode ) );
                 }
-                return childList.iterator();
+                return childList;
             } catch ( RepositoryException re )
             {
                 return null;