changeset 600:7272ec90f2c6

8025629: load function should support a way to load scripts from classpath Reviewed-by: lagergren, hannesw, attila
author sundar
date Mon, 30 Sep 2013 21:33:38 +0530
parents 787e36fdf69a
children f5aefbe76cec
files make/build.xml src/jdk/nashorn/internal/runtime/Context.java test/script/trusted/JDK-8025629.js test/src/jdk/nashorn/internal/runtime/resources/load_test.js
diffstat 4 files changed, 101 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/make/build.xml	Mon Sep 30 12:06:43 2013 -0300
+++ b/make/build.xml	Mon Sep 30 21:33:38 2013 +0530
@@ -236,6 +236,10 @@
        <fileset dir="${test.src.dir}/META-INF/services/"/>
     </copy>
 
+    <copy todir="${build.test.classes.dir}/jdk/nashorn/internal/runtime/resources">
+       <fileset dir="${test.src.dir}/jdk/nashorn/internal/runtime/resources"/>
+    </copy>
+
     <!-- tests that check nashorn internals and internal API -->
     <jar jarfile="${nashorn.internal.tests.jar}">
       <fileset dir="${build.test.classes.dir}" excludes="**/api/**"/>
@@ -245,6 +249,7 @@
     <jar jarfile="${nashorn.api.tests.jar}">
       <fileset dir="${build.test.classes.dir}" includes="**/api/**"/>
       <fileset dir="${build.test.classes.dir}" includes="**/META-INF/**"/>
+      <fileset dir="${build.test.classes.dir}" includes="**/resources/*.js"/>
     </jar>
 
   </target>
--- a/src/jdk/nashorn/internal/runtime/Context.java	Mon Sep 30 12:06:43 2013 -0300
+++ b/src/jdk/nashorn/internal/runtime/Context.java	Mon Sep 30 21:33:38 2013 +0530
@@ -91,6 +91,11 @@
      */
     public static final String NASHORN_JAVA_REFLECTION = "nashorn.JavaReflection";
 
+    // nashorn load psuedo URL prefixes
+    private static final String LOAD_CLASSPATH = "classpath:";
+    private static final String LOAD_FX = "fx:";
+    private static final String LOAD_NASHORN = "nashorn:";
+
     /* Force DebuggerSupport to be loaded. */
     static {
         DebuggerSupport.FORCELOAD = true;
@@ -501,21 +506,26 @@
         // or a ScriptObject that has "name" and "source" (string valued) properties.
         if (src instanceof String) {
             final String srcStr = (String)src;
-            final File file = new File(srcStr);
-            if (srcStr.indexOf(':') != -1) {
-                if ((source = loadInternal(srcStr, "nashorn:", "resources/")) == null &&
-                    (source = loadInternal(srcStr, "fx:", "resources/fx/")) == null) {
-                    URL url;
-                    try {
-                        //check for malformed url. if malformed, it may still be a valid file
-                        url = new URL(srcStr);
-                    } catch (final MalformedURLException e) {
-                        url = file.toURI().toURL();
+            if (srcStr.startsWith(LOAD_CLASSPATH)) {
+                URL url = getResourceURL(srcStr.substring(LOAD_CLASSPATH.length()));
+                source = (url != null)? new Source(url.toString(), url) : null;
+            } else {
+                final File file = new File(srcStr);
+                if (srcStr.indexOf(':') != -1) {
+                    if ((source = loadInternal(srcStr, LOAD_NASHORN, "resources/")) == null &&
+                        (source = loadInternal(srcStr, LOAD_FX, "resources/fx/")) == null) {
+                        URL url;
+                        try {
+                            //check for malformed url. if malformed, it may still be a valid file
+                            url = new URL(srcStr);
+                        } catch (final MalformedURLException e) {
+                            url = file.toURI().toURL();
+                        }
+                        source = new Source(url.toString(), url);
                     }
-                    source = new Source(url.toString(), url);
+                } else if (file.isFile()) {
+                    source = new Source(srcStr, file);
                 }
-            } else if (file.isFile()) {
-                source = new Source(srcStr, file);
             }
         } else if (src instanceof File && ((File)src).isFile()) {
             final File file = (File)src;
@@ -803,6 +813,18 @@
         return Context.getContextTrusted();
     }
 
+    private URL getResourceURL(final String resName) throws IOException {
+        // try the classPathLoader if we have and then
+        // try the appLoader if non-null.
+        if (classPathLoader != null) {
+            return classPathLoader.getResource(resName);
+        } else if (appLoader != null) {
+            return appLoader.getResource(resName);
+        }
+
+        return null;
+    }
+
     private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
         ScriptFunction script = null;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/trusted/JDK-8025629.js	Mon Sep 30 21:33:38 2013 +0530
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8025629: load function should support a way to load scripts from classpath
+ *
+ * @test
+ * @run
+ */
+
+load("classpath:jdk/nashorn/internal/runtime/resources/load_test.js")
+
+Assert.assertEquals(loadedFunc("hello"), "HELLO");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/src/jdk/nashorn/internal/runtime/resources/load_test.js	Mon Sep 30 21:33:38 2013 +0530
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+function loadedFunc(arg) {
+    return arg.toUpperCase();
+}