Author: bdelacretaz
Date: Tue Apr 11 09:30:28 2017
New Revision: 1790947

URL: http://svn.apache.org/viewvc?rev=1790947&view=rev
Log:
SLING-6763 - remove org.apache.sling.commons.json and add sling-babel, 
contributed by Senol Tas, thanks!

Added:
    sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/
    sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/
    sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/index.js
    
sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/sling-sandbox-plugin/
    
sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/sling-sandbox-plugin/index.js
    sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/package.json
    
sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/webpack.config.js
    sling/trunk/contrib/scripting/esx/gulpfile.js
    sling/trunk/contrib/scripting/esx/package.json
    
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/services/ScriptSandboxService.java
    
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/services/impl/ScriptSandboxServiceImpl.java
    sling/trunk/contrib/scripting/esx/src/main/resources/internal/
    sling/trunk/contrib/scripting/esx/src/main/resources/internal/sling-babel.js
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/components/esx_modules/calculator/calculator.js
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/components/esx_modules/calculator/package.json
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/components/esx_modules/pathinfo/
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/components/esx_modules/pathinfo/index.js
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/components/page/templates/listing-small.html
Removed:
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/components/page/helper/pathinfo/
Modified:
    sling/trunk/contrib/scripting/esx/   (props changed)
    sling/trunk/contrib/scripting/esx/.gitignore
    sling/trunk/contrib/scripting/esx/README.md
    sling/trunk/contrib/scripting/esx/pom.xml
    
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/EsxScriptEngineFactory.java
    
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/Module.java
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/components/esx_modules/BlogComponent/index.js
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/components/hbstemplates/layout.html
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/components/page/page.esx
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/components/post/post.esx
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/content/demo.xml
    
sling/trunk/contrib/scripting/esx/src/main/resources/libs/esx/demo/package.json

Propchange: sling/trunk/contrib/scripting/esx/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Tue Apr 11 09:30:28 2017
@@ -1,4 +1,7 @@
+etc
 target
 .settings
 .classpath
 .project
+node
+node_modules

Modified: sling/trunk/contrib/scripting/esx/.gitignore
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/.gitignore?rev=1790947&r1=1790946&r2=1790947&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/esx/.gitignore (original)
+++ sling/trunk/contrib/scripting/esx/.gitignore Tue Apr 11 09:30:28 2017
@@ -4,3 +4,4 @@ target
 .project
 settings.xml
 node_modules
+node

Modified: sling/trunk/contrib/scripting/esx/README.md
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/README.md?rev=1790947&r1=1790946&r2=1790947&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/esx/README.md (original)
+++ sling/trunk/contrib/scripting/esx/README.md Tue Apr 11 09:30:28 2017
@@ -27,7 +27,7 @@ This module implements a Nashorn Apache
 
 It requires a function named `render` in the `esx` script that processes the 
request.
 
-To activate this script engine you must first **enable Nashorn support** in 
the 
+To activate this script engine you must first **enable Nashorn support** in the
 `sling.properties` file of your Sling instance:
 
 ```
@@ -45,27 +45,27 @@ First create a node with some content:
          -Ftitle="Hello ESX" \
          -Ftext="Here's some example text" \
          http://localhost:8080/apps/foo
-         
+
 Then create an ESX script to render it:
 
     $ cat << EOF > /tmp/foo.esx
     var foo = {
       render: function () {
-        var output  = "<h1>" + currentNode.properties.title + "</h1>";         
    
+        var output  = '<h1>\${currentNode.properties.title}</h1>';
         output += currentNode.properties.text;
         return output;     
       }
     }  
     module.exports = foo;
     EOF
-       
+
     $ curl -u admin:admin -T /tmp/foo.esx 
http://localhost:8080/apps/foo/foo.esx
-   
+
     $ curl http://localhost:8080/apps/foo.html
     <h1>Hello ESX</h1>Here's some example text
-         
 
-An ESX file is a regular java script file. 
+
+An ESX file is a regular java script file.
 
 The NodeJS module resolution (https://nodejs.org/api/modules.html) is 
implemented to give access to the
 rich collection of Node modules.
@@ -97,12 +97,8 @@ We have borrowed the requirejs loader pl
 - json loader  (e.g. ```require("./dict/en.json```)
   - the json as a whole will be exported as a javascript Object
 
-## Installing Demo Application
-Currently the demo application is bundles with the engine bundle. To install 
the engine with the demo application, follow this steps:
-- switch to directory src/main/resources/libs/esx/demo
-- run: npm install
-- go back to package root directory
-- run mvn clean install sling:install´
+##  Demo Application
+Currently the demo application is bundles with the engine bundle.
 
 open http://localhost:8080/libs/esx/demo/content/demo.html
 

Added: sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/index.js
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/index.js?rev=1790947&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/index.js 
(added)
+++ sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/index.js 
Tue Apr 11 09:30:28 2017
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+var SlingSandbox = require("./sling-sandbox-plugin");
+var Babel = require("babel-standalone");
+var template = require("babel-template");
+
+require("babel-types");
+require("babel-plugin-transform-react-jsx");
+Babel.registerPlugin("SlingSandbox". SlingSandbox);
+module.exports = Babel;
\ No newline at end of file

Added: 
sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/sling-sandbox-plugin/index.js
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/sling-sandbox-plugin/index.js?rev=1790947&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/sling-sandbox-plugin/index.js
 (added)
+++ 
sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/lib/sling-sandbox-plugin/index.js
 Tue Apr 11 09:30:28 2017
@@ -0,0 +1,36 @@
+/*
+ * 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.
+ */
+module.exports = function (babel) {
+  const { types: t } = babel;
+ 
+  return {
+    visitor: {
+        BlockStatement(path) {
+            var threadInterrupted =  t.callExpression(
+                    t.memberExpression(
+                        t.identifier('SlingSandbox'),
+                        t.identifier('checkInterrupted'))
+                , []);
+          
+          path.unshiftContainer('body',
+            t.expressionStatement(
+                threadInterrupted
+            )                                                              );
+        }
+    }
+  };
+}
\ No newline at end of file

Added: sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/package.json
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/package.json?rev=1790947&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/package.json 
(added)
+++ sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/package.json 
Tue Apr 11 09:30:28 2017
@@ -0,0 +1,22 @@
+{
+  "name": "babel-plugin-sling-sandbox",
+  "version": "0.0.1",
+  "description": "Babel Sandbox Plugin for Apache Sling ESX scripting",
+  "main": "lib/index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1",
+    "postinstall": "webpack"
+  },
+  "author": "",
+  "license": "Apache-2.0",
+  "dependencies": {
+    "babel-core": "^6.24.1",
+    "babel-loader": "^6.4.1",
+    "babel-plugin-transform-react-jsx": "^6.24.1",
+    "babel-preset-es2015": "^6.24.1",
+    "babel-standalone": "^6.24.0",
+    "babel-template": "^6.24.1",
+    "babel-types": "^6.24.1",
+    "webpack": "^2.3.3"
+  }
+}

Added: 
sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/webpack.config.js
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/webpack.config.js?rev=1790947&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/webpack.config.js 
(added)
+++ 
sling/trunk/contrib/scripting/esx/babel-plugin-sling-sandbox/webpack.config.js 
Tue Apr 11 09:30:28 2017
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+module.exports = {
+    entry: "./lib/index.js",
+    output: {
+        filename: "../src/main/resources/internal/sling-babel.js",
+        library: "SlingBabel",
+        libraryTarget: "this"
+    },
+    module: {
+        loaders: [
+            {
+                test: /\.js$/,
+                exclude: /(node_modules|bower_components)/,
+                loader: "babel-loader",
+                query: {
+                    presets: ["es2015"]
+                }
+            }
+            
+        ]
+    }    
+}
\ No newline at end of file

Added: sling/trunk/contrib/scripting/esx/gulpfile.js
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/gulpfile.js?rev=1790947&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/esx/gulpfile.js (added)
+++ sling/trunk/contrib/scripting/esx/gulpfile.js Tue Apr 11 09:30:28 2017
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+var gulp = require("gulp");
+var gutil = require("gulp-util");
+var install = require("gulp-install");
+var webpack = require("webpack");
+var webpackConfig = require("./babel-plugin-sling-sandbox/webpack.config.js");
+
+
+gulp.task('default', function() {
+      
gulp.src(["babel-plugin-sling-sandbox/package.json","src/**/package.json"])
+        .pipe(install());
+});
\ No newline at end of file

Added: sling/trunk/contrib/scripting/esx/package.json
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/package.json?rev=1790947&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/esx/package.json (added)
+++ sling/trunk/contrib/scripting/esx/package.json Tue Apr 11 09:30:28 2017
@@ -0,0 +1,17 @@
+{
+  "name": "esx",
+  "version": "1.0.0",
+  "description": "<!-- /*  * 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.  
*/ --> # Apache Sling ESX Scripting Engine",
+  "main": "gulpfile.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "gulp": "^3.9.1",
+    "gulp-install": "^1.1.0",
+    "gulp-util": "^3.0.8",
+    "webpack": "^2.3.3"
+  }
+}

Modified: sling/trunk/contrib/scripting/esx/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/pom.xml?rev=1790947&r1=1790946&r2=1790947&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/esx/pom.xml (original)
+++ sling/trunk/contrib/scripting/esx/pom.xml Tue Apr 11 09:30:28 2017
@@ -62,12 +62,44 @@
                 <groupId>org.apache.rat</groupId>
                 <artifactId>apache-rat-plugin</artifactId>
                 <configuration>
-                    <excludes>
-                        
<exclude>src/main/resources/libs/esx/demo/node_modules/**</exclude>
+                    <excludes>                        
+                        <exclude>**/node_modules/**</exclude>
+                        <exclude>node</exclude>
                     </excludes>
                 </configuration>
             </plugin>
             <plugin>
+               <groupId>com.github.eirslett</groupId>
+               <artifactId>frontend-maven-plugin</artifactId>
+               <version>1.4</version>
+               <executions>
+                <execution>
+                        <id>install node and npm</id>
+                        <goals>
+                            <goal>install-node-and-npm</goal>
+                        </goals>
+                        <phase>generate-resources</phase>
+                 </execution>
+                <execution>
+                    <id>npm install</id>
+                    <goals>
+                        <goal>npm</goal>
+                    </goals>
+                </execution>                 
+                <execution>
+                    <id>gulp build</id>
+                    <goals>
+                        <goal>gulp</goal>
+                    </goals>
+                    <phase>generate-resources</phase>
+                </execution>                 
+                 </executions>
+                    <configuration>
+                        <nodeVersion>v7.8.0</nodeVersion>
+                    </configuration>               
+                
+             </plugin>            
+            <plugin>
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-scr-plugin</artifactId>
                 <version>1.21.0</version>
@@ -163,12 +195,7 @@
             <version>2.1.8</version>
             <scope>provided</scope>
         </dependency>
-        <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.commons.json</artifactId>
-            <version>2.0.19-SNAPSHOT</version>
-            <scope>provided</scope>
-        </dependency>
+
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>

Modified: 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/EsxScriptEngineFactory.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/EsxScriptEngineFactory.java?rev=1790947&r1=1790946&r2=1790947&view=diff
==============================================================================
--- 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/EsxScriptEngineFactory.java
 (original)
+++ 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/EsxScriptEngineFactory.java
 Tue Apr 11 09:30:28 2017
@@ -29,6 +29,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.scripting.esx.services.ScriptSandboxService;
 import org.osgi.framework.Constants;
 import org.osgi.service.component.ComponentContext;
 
@@ -49,6 +50,9 @@ public class EsxScriptEngineFactory exte
     @Reference
     ScriptModuleCache moduleCache;
 
+    @Reference
+    ScriptSandboxService sandboxService;
+
     public EsxScriptEngineFactory() {
         setNames("esx", "ESX");
         setExtensions("esx", "ESX");
@@ -69,6 +73,9 @@ public class EsxScriptEngineFactory exte
         return new EsxScriptEngine(this);
     }
 
+    public ScriptSandboxService getSandboxService() {
+        return sandboxService;
+    }
     /**
      * 
      * @return 

Modified: 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/Module.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/Module.java?rev=1790947&r1=1790946&r2=1790947&view=diff
==============================================================================
--- 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/Module.java
 (original)
+++ 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/Module.java
 Tue Apr 11 09:30:28 2017
@@ -23,9 +23,11 @@ import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.logging.Level;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.nodetype.NodeType;
+import javax.script.Invocable;
 import javax.script.ScriptException;
 import javax.script.SimpleBindings;
 import jdk.nashorn.api.scripting.JSObject;
@@ -38,8 +40,6 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.api.scripting.SlingScriptHelper;
-import org.apache.sling.commons.json.JSONException;
-import org.apache.sling.commons.json.JSONObject;
 import org.apache.sling.scripting.esx.plugins.ConsoleLog;
 import org.apache.sling.scripting.esx.plugins.SimpleResource;
 import org.slf4j.Logger;
@@ -106,25 +106,36 @@ public class Module extends SimpleBindin
         put(CONTEXT_FIELD_CONSOLE, new ConsoleLog((String) 
get(CONTEXT_FIELD_FILENAME)));
     }
 
+    private ScriptObjectMirror decorateScript(String source)
+            throws ScriptException {
+        return decorateScript(source, true);
+    }
+
     /**
      *
      * @param source
      * @return
      * @throws ScriptException
      */
-    private ScriptObjectMirror decoreateScript(String source)
+    private ScriptObjectMirror decorateScript(String source, boolean es6)
             throws ScriptException {
-
-        String polyfill = "";
-
-        if ("polyfill".equals(get(CONTEXT_FIELD_ID))) {
-            //polyfill = 
"require('/libs/esx/esx_modules/polyfills/index.js')(this).bind(this)";
+        String filename = (String) get("filename");
+        if (filename.indexOf("node_modules") == -1 && es6) {
+            try {
+                source = factory.getSandboxService().compileSource(source);
+            } catch (ScriptException e) {
+                log.error("Could not transpile script", e);
+                throw new ScriptException("could not load " + get("filename"));
+            }
         }
+
+        // TODO: refactor polyfill for window, global and make require outside 
the wrapper as function parameter        
         source = "//@sourceURL=" + (String) get("filename") + "\n"
-                + "(function (exports, require, module, __filename,"
+                + "(function (exports, Require, module, __filename,"
                 + " __dirname, currentNode, console, properties, sling, 
simpleResource) { "
-                + "var window = this;"
-                + "var global = this;"
+                + "var window = (this.window == 'undefined' || this.window == 
null) ? this : this.window;"
+                + "var global = (global == 'undefined') ? this : global;"
+                + "function require(id) {  return Require.require(id); } 
require.resolve = function (id) { return Require.resolve(id, 
currentNode.resource, 1);  };"
                 + source
                 + "})";
 
@@ -162,7 +173,7 @@ public class Module extends SimpleBindin
 
         if (function == null) {
             if (moduleScript.isJsFile()) {
-                function = decoreateScript(
+                function = Module.this.decorateScript(
                         //readScript(moduleResource)//  
                         readScript(moduleResource)
                 );
@@ -170,9 +181,9 @@ public class Module extends SimpleBindin
 
             if (moduleScript.isJsonFile()) {
                 String jsonfile = readScript(moduleResource);
-                function = decoreateScript(
+                function = decorateScript(
                         "module.exports = " + jsonfile
-                );
+                , false);
             }
 
             if (moduleScript.isResourceFile()) {
@@ -182,21 +193,25 @@ public class Module extends SimpleBindin
 
                 put("children", children);
 
-                ValueMap map = moduleResource.adaptTo(ValueMap.class);
-
-                JSONObject values = new JSONObject(map);
-
-                String jsonprop = values.toString();
+                /*ValueMap map = moduleResource.adaptTo(ValueMap.class);
 
+                 Invocable invocable = (Invocable) factory.getNashornEngine();
+                 Object jsonprop = null;
+                 try {
+                 jsonprop = 
invocable.invokeMethod(factory.getNashornEngine().eval("JSON"), "stringify", 
map);
+                 } catch (NoSuchMethodException ex) {
+                 throw new ScriptException(ex);
+                 }
+                 */
                 SimpleResource simpleResource = 
moduleResource.adaptTo(SimpleResource.class);
                 put("simpleResource", simpleResource);
+                /*   String source = "exports.properties =  " + jsonprop + ";"
+                 + "exports.path = currentNode.resource.path;"
+                 + "exports.simpleResource = this.simpleResource;"
+                 + "exports.children = this.children;";*/
+                String source = "module.exports = this.simpleResource;";
 
-                String source = "exports.properties =  " + jsonprop + ";"
-                        + "exports.path = currentNode.resource.path;"
-                        + "exports.simpleResource = this.simpleResource;"
-                        + "exports.children = this.children;";
-
-                function = decoreateScript(source);
+                function = decorateScript(source, false);
             }
             if (!moduleScript.isResourceFile()) {
                 
factory.getModuleCache().put(moduleScript.getResource().getPath(), function);
@@ -211,7 +226,7 @@ public class Module extends SimpleBindin
             log.debug("sourcE: ");
             source = "module.exports = \"" + source + "\";";
             log.debug(source);
-            function = decoreateScript(source);
+            function = Module.this.decorateScript(source);
             factory.getModuleCache().put(moduleScript.getResource().getPath(), 
function);
         }
         JSObject process = (JSObject) 
factory.getNashornEngine().eval("Object");
@@ -226,7 +241,9 @@ public class Module extends SimpleBindin
                 log.debug("module id {} resource is null", 
get(CONTEXT_FIELD_ID));
             }
 
-            function.call(this, get(CONTEXT_FIELD_EXPORTS), (Require) 
this::require, this, get(CONTEXT_FIELD_FILENAME),
+            // changed require to be generated within the wrapper
+            // TODO: need to be refactored
+            function.call(this, get(CONTEXT_FIELD_EXPORTS), this, this, 
get(CONTEXT_FIELD_FILENAME),
                     ((Resource) 
get(CONTEXT_FIELD_MODULE_RESOURCE)).getParent().getPath(), currentNode,
                     (ConsoleLog) get(CONTEXT_FIELD_CONSOLE),
                     null,
@@ -418,24 +435,20 @@ public class Module extends SimpleBindin
                     try {
                         String jsonData = IOUtils.toString(is);
 
-                        JSONObject json;
+                        Invocable invocable = (Invocable) 
factory.getNashornEngine();
+                        JSObject jsonprop = null;
                         try {
-                            json = new JSONObject(jsonData);
-                        } catch (JSONException ex) {
+                            jsonprop = (JSObject) 
invocable.invokeMethod(factory.getNashornEngine().eval("JSON"), "parse", 
jsonData);
+                        } catch (NoSuchMethodException ex) {
                             throw new ScriptException(ex);
                         }
 
-                        if (json.has("main")) {
-                            String packageModule;
-                            try {
-                                packageModule = json.getString("main");
-                            } catch (JSONException ex) {
-                                throw new ScriptException(ex);
-                            }
+                        Object main = jsonprop.getMember("main");
+                        if (main != null) {
+                            String packageModule = (String) main;
 
                             String mainpath = normalizePath(packageModule,
                                     path);
-
                             return loadAsFile(packageModule, mainpath, 
currentResource, loader);
                         }
 

Added: 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/services/ScriptSandboxService.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/services/ScriptSandboxService.java?rev=1790947&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/services/ScriptSandboxService.java
 (added)
+++ 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/services/ScriptSandboxService.java
 Tue Apr 11 09:30:28 2017
@@ -0,0 +1,27 @@
+/*
+ * 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.esx.services;
+
+import javax.script.ScriptException;
+
+/**
+ *
+ * @author senol
+ */
+public interface ScriptSandboxService {
+    public String compileSource(String source) throws ScriptException;
+}

Added: 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/services/impl/ScriptSandboxServiceImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/services/impl/ScriptSandboxServiceImpl.java?rev=1790947&view=auto
==============================================================================
--- 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/services/impl/ScriptSandboxServiceImpl.java
 (added)
+++ 
sling/trunk/contrib/scripting/esx/src/main/java/org/apache/sling/scripting/esx/services/impl/ScriptSandboxServiceImpl.java
 Tue Apr 11 09:30:28 2017
@@ -0,0 +1,104 @@
+/*
+ * 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.esx.services.impl;
+
+import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.script.Invocable;
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+import jdk.nashorn.api.scripting.JSObject;
+import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingConstants;
+import org.apache.sling.scripting.esx.ScriptModuleCache;
+import org.apache.sling.scripting.esx.services.ScriptSandboxService;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.event.EventHandler;
+import org.slf4j.LoggerFactory;
+
+@Component
+@Service(value = {ScriptSandboxService.class})
+@Property(name = org.osgi.service.event.EventConstants.EVENT_TOPIC,
+        value = {SlingConstants.TOPIC_RESOURCE_CHANGED, 
SlingConstants.TOPIC_RESOURCE_REMOVED})
+public class ScriptSandboxServiceImpl implements ScriptSandboxService {
+
+    private final org.slf4j.Logger log = LoggerFactory.getLogger(getClass());
+    private final String SLING_BABEL_SOURCE_CODE = 
"/resources/internal/sling-babel.js";
+
+    private String slingBabelSource = null;
+    private ScriptEngine scriptEngine;
+    private Object babelOptions = null;
+    private Object babel = null;
+
+    @Override
+    public String compileSource(String source) throws ScriptException {      
+        Invocable inv = (Invocable) scriptEngine;
+        JSObject rs;
+        try {       
+            rs = (JSObject) inv.invokeMethod(babel, "transform", source, 
babelOptions);
+            return rs.getMember("code").toString(); 
+        } catch (ScriptException ex) {
+            throw new ScriptException(ex);
+        } catch (NoSuchMethodException ex) {
+            throw new ScriptException(ex);
+        }
+    }
+
+    /**
+     *
+     * @param context
+     */
+    @Activate
+    protected void activate(ComponentContext context) {
+        try {
+            this.slingBabelSource = "//@sourceURL=" + 
getClass().getCanonicalName() + "\n load( { name : \"" + 
getClass().getCanonicalName() + "\", script: \""
+                    + StringEscapeUtils.escapeEcmaScript(new 
String(IOUtils.toByteArray(
+                                            
context.getBundleContext().getBundle()
+                                            
.getEntry(this.SLING_BABEL_SOURCE_CODE).openStream())))
+                    + "\"} )";
+        } catch (Exception ex) {
+           log.error("failed to load babel source", ex);
+        }
+        
+        String options = "var config = { "
+                + "    presets: [\"es2015\"], "
+                + "    compact: 'true',"
+                + "    plugins: [[\"transform-react-jsx\", { pragma : 
\"SlingEsx.createElement\"}]] "
+                //+ // disabled sandbox plugin for 
+                //"    \"plugins\": [\"SlingSandbox, [\"transform-react-jsx\", 
{ \"pragma\" : \"SlingEsx.createElement\"}]] \n" +        
+                + "    }"; 
+        NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
+        this.scriptEngine = factory.getScriptEngine();
+        try {
+            log.info("trying to load babel into the system");
+            this.scriptEngine.eval(options);
+            this.babelOptions = this.scriptEngine.get("config");
+            scriptEngine.eval(slingBabelSource);
+            this.babel = this.scriptEngine.get("SlingBabel");
+            log.info("Babel loaded");
+        } catch (ScriptException ex) {
+            log.error("coudlnt load babel options", ex);
+        }
+    }
+}


Reply via email to