aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/JavaScriptingProgrammersGuide.html8
-rw-r--r--docs/source/javaarray.js3
-rw-r--r--make/build.xml4
-rw-r--r--make/java.security.override4
-rw-r--r--src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java10
-rw-r--r--src/jdk/nashorn/api/scripting/ScriptUtils.java48
-rw-r--r--src/jdk/nashorn/internal/objects/Global.java24
-rw-r--r--src/jdk/nashorn/internal/objects/NativeDebug.java2
-rw-r--r--src/jdk/nashorn/internal/objects/NativeJava.java17
-rw-r--r--src/jdk/nashorn/internal/runtime/Context.java25
-rw-r--r--src/jdk/nashorn/internal/runtime/NashornLoader.java47
-rw-r--r--src/jdk/nashorn/internal/runtime/ScriptLoader.java7
-rw-r--r--src/jdk/nashorn/internal/runtime/StructureLoader.java3
-rw-r--r--src/jdk/nashorn/internal/runtime/linker/Bootstrap.java2
-rw-r--r--src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java28
-rw-r--r--src/jdk/nashorn/internal/runtime/linker/ReflectionCheckLinker.java60
-rw-r--r--src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js18
-rw-r--r--src/jdk/nashorn/internal/runtime/resources/parser.js10
-rw-r--r--test/script/basic/JDK-8008448.js4
-rw-r--r--test/script/basic/NASHORN-401.js2
-rw-r--r--test/script/basic/consstring.js2
-rw-r--r--test/script/basic/fileline.js4
-rw-r--r--test/script/basic/javainnerclasses.js18
-rw-r--r--test/script/basic/list.js2
-rw-r--r--test/script/basic/map.js2
-rw-r--r--test/script/basic/stdin.js6
-rw-r--r--test/script/sandbox/javaextend.js2
-rw-r--r--test/script/sandbox/javaextend.js.EXPECTED6
-rw-r--r--test/script/sandbox/reflection.js4
-rw-r--r--test/script/sandbox/reflection.js.EXPECTED1
-rw-r--r--test/script/sandbox/unsafe.js4
-rw-r--r--test/script/sandbox/unsafe.js.EXPECTED4
-rw-r--r--test/script/trusted/urlreader.js6
-rw-r--r--test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java52
-rw-r--r--test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java31
-rw-r--r--test/src/jdk/nashorn/test/models/ConstructorWithArgument.java (renamed from test/src/jdk/nashorn/internal/test/models/ConstructorWithArgument.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/DessertTopping.java (renamed from test/src/jdk/nashorn/internal/test/models/DessertTopping.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/DessertToppingFloorWaxDriver.java (renamed from test/src/jdk/nashorn/internal/test/models/DessertToppingFloorWaxDriver.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/FinalClass.java (renamed from test/src/jdk/nashorn/internal/test/models/FinalClass.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/FloorWax.java (renamed from test/src/jdk/nashorn/internal/test/models/FloorWax.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/Nashorn401TestSubject.java (renamed from test/src/jdk/nashorn/internal/runtime/Nashorn401TestSubject.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/NoAccessibleConstructorClass.java (renamed from test/src/jdk/nashorn/internal/test/models/NoAccessibleConstructorClass.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/NonPublicClass.java (renamed from test/src/jdk/nashorn/internal/test/models/NonPublicClass.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/OuterClass.java (renamed from test/src/jdk/nashorn/internal/test/models/OuterClass.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/OverloadedSam.java (renamed from test/src/jdk/nashorn/internal/test/models/OverloadedSam.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/OverrideObject.java (renamed from test/src/jdk/nashorn/internal/test/models/OverrideObject.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/SourceHelper.java55
-rw-r--r--test/src/jdk/nashorn/test/models/StringArgs.java (renamed from test/src/jdk/nashorn/internal/test/models/StringArgs.java)2
-rw-r--r--test/src/jdk/nashorn/test/models/Toothpaste.java (renamed from test/src/jdk/nashorn/internal/test/models/Toothpaste.java)2
49 files changed, 409 insertions, 142 deletions
diff --git a/docs/JavaScriptingProgrammersGuide.html b/docs/JavaScriptingProgrammersGuide.html
index cf248140..dd243d3a 100644
--- a/docs/JavaScriptingProgrammersGuide.html
+++ b/docs/JavaScriptingProgrammersGuide.html
@@ -533,9 +533,8 @@ with (SwingGui) {
<hr>
<a name="jsarrays" id="jsarrays"></a>
<h3>Creating, Converting and Using Java Arrays</h3>
-<p>While creating a Java object is the same as in Java, to create
-Java arrays in JavaScript we can use Java reflection
-explicitly. But once created the element access or length access is
+<p>
+Array element access or length access is
the same as in Java. Also, a script array can be used when a Java
method expects a Java array (auto conversion). So in most cases we
don't have to create Java arrays explicitly.</p>
@@ -543,7 +542,8 @@ don't have to create Java arrays explicitly.</p>
// <a href="source/javaarray.js">javaarray.js</a>
// create Java String array of 5 elements
-var a = java.lang.reflect.Array.newInstance(java.lang.String.class, 5);
+var StringArray = Java.type("java.lang.String[]");
+var a = new StringArray(5);
// Accessing elements and length access is by usual Java syntax
a[0] = "scripting is great!";
diff --git a/docs/source/javaarray.js b/docs/source/javaarray.js
index b9d93f0d..a02aa3ca 100644
--- a/docs/source/javaarray.js
+++ b/docs/source/javaarray.js
@@ -30,7 +30,8 @@
*/
// create Java String array of 5 elements
-var a = java.lang.reflect.Array.newInstance(java.lang.String.class, 5);
+var StringArray = Java.type("java.lang.String[]");
+var a = new StringArray(5);
// Accessing elements and length access is by usual Java syntax
a[0] = "scripting is great!";
diff --git a/make/build.xml b/make/build.xml
index 34f56e46..e2d8f0cc 100644
--- a/make/build.xml
+++ b/make/build.xml
@@ -191,12 +191,12 @@
<!-- tests that check nashorn internals and internal API -->
<jar jarfile="${nashorn.internal.tests.jar}">
- <fileset dir="${build.test.classes.dir}" excludes="**/api/*"/>
+ <fileset dir="${build.test.classes.dir}" excludes="**/api/**"/>
</jar>
<!-- tests that check nashorn script engine (jsr-223) API -->
<jar jarfile="${nashorn.api.tests.jar}">
- <fileset dir="${build.test.classes.dir}" includes="**/api/*"/>
+ <fileset dir="${build.test.classes.dir}" includes="**/api/**"/>
</jar>
</target>
diff --git a/make/java.security.override b/make/java.security.override
index 00219852..a7edf33b 100644
--- a/make/java.security.override
+++ b/make/java.security.override
@@ -3,7 +3,7 @@
# We ensure that by overriding "package.access" security property.
# The following "package.access" value was copied from default java.security
-# of jre/lib/security and appended with nashorn IR, Codegen and Parser packages.
+# of jre/lib/security and appended with nashorn sensitive packages.
#
# List of comma-separated packages that start with or equal this string
@@ -11,4 +11,4 @@
# passed to checkPackageAccess unless the
# corresponding RuntimePermission ("accessClassInPackage."+package) has
# been granted.
-package.access=sun.,com.sun.xml.internal.ws.,com.sun.xml.internal.bind.,com.sun.imageio.,com.sun.org.apache.xerces.internal.utils.,com.sun.org.apache.xalan.internal.utils.,com.sun.org.glassfish.external.,com.sun.org.glassfish.gmbal.,jdk.internal.,jdk.nashorn.internal.ir., jdk.nashorn.internal.codegen., jdk.nashorn.internal.lookup., jdk.nashorn.internal.parser.
+package.access=sun.,com.sun.xml.internal.ws.,com.sun.xml.internal.bind.,com.sun.imageio.,com.sun.org.apache.xerces.internal.utils.,com.sun.org.apache.xalan.internal.utils.,com.sun.org.glassfish.external.,com.sun.org.glassfish.gmbal.,jdk.internal.,jdk.nashorn.internal.,jdk.nashorn.tools.
diff --git a/src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java b/src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java
index 47a0c595..4672f6a0 100644
--- a/src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java
+++ b/src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java
@@ -147,6 +147,7 @@ public final class NashornScriptEngineFactory implements ScriptEngineFactory {
* @return newly created script engine.
*/
public ScriptEngine getScriptEngine(final ClassLoader appLoader) {
+ checkConfigPermission();
return new NashornScriptEngine(this, appLoader);
}
@@ -157,6 +158,7 @@ public final class NashornScriptEngineFactory implements ScriptEngineFactory {
* @return newly created script engine.
*/
public ScriptEngine getScriptEngine(final String[] args) {
+ checkConfigPermission();
return new NashornScriptEngine(this, args, getAppClassLoader());
}
@@ -168,11 +170,19 @@ public final class NashornScriptEngineFactory implements ScriptEngineFactory {
* @return newly created script engine.
*/
public ScriptEngine getScriptEngine(final String[] args, final ClassLoader appLoader) {
+ checkConfigPermission();
return new NashornScriptEngine(this, args, appLoader);
}
// -- Internals only below this point
+ private void checkConfigPermission() {
+ final SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ sm.checkPermission(new RuntimePermission("nashorn.setConfig"));
+ }
+ }
+
private static final List<String> names;
private static final List<String> mimeTypes;
private static final List<String> extensions;
diff --git a/src/jdk/nashorn/api/scripting/ScriptUtils.java b/src/jdk/nashorn/api/scripting/ScriptUtils.java
new file mode 100644
index 00000000..63358bd7
--- /dev/null
+++ b/src/jdk/nashorn/api/scripting/ScriptUtils.java
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+
+package jdk.nashorn.api.scripting;
+
+import jdk.nashorn.internal.runtime.ScriptRuntime;
+
+/**
+ * Utilities that are to be called from script code
+ */
+public final class ScriptUtils {
+ private ScriptUtils() {}
+
+ /**
+ * Returns AST as JSON compatible string. This is used to
+ * implement "parse" function in resources/parse.js script.
+ *
+ * @param code code to be parsed
+ * @param name name of the code source (used for location)
+ * @param includeLoc tells whether to include location information for nodes or not
+ * @return JSON string representation of AST of the supplied code
+ */
+ public static String parse(final String code, final String name, final boolean includeLoc) {
+ return ScriptRuntime.parse(code, name, includeLoc);
+ }
+}
diff --git a/src/jdk/nashorn/internal/objects/Global.java b/src/jdk/nashorn/internal/objects/Global.java
index b89d2073..a9835f6f 100644
--- a/src/jdk/nashorn/internal/objects/Global.java
+++ b/src/jdk/nashorn/internal/objects/Global.java
@@ -34,6 +34,9 @@ import java.io.PrintWriter;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.ref.SoftReference;
+import java.lang.reflect.Field;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -1503,8 +1506,10 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);
// Nashorn extension: global.$OPTIONS (scripting-mode-only)
- value = context.getEnv();
- addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, value);
+ final ScriptObject options = newEmptyInstance();
+ final ScriptEnvironment scriptEnv = context.getEnv();
+ copyOptions(options, scriptEnv);
+ addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);
// Nashorn extension: global.$ENV (scripting-mode-only)
if (System.getSecurityManager() == null) {
@@ -1523,6 +1528,21 @@ public final class Global extends ScriptObject implements GlobalObject, Scope {
addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
}
+ private void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
+ AccessController.doPrivileged(new PrivilegedAction<Void>() {
+ public Void run() {
+ for (Field f : scriptEnv.getClass().getFields()) {
+ try {
+ options.set(f.getName(), f.get(scriptEnv), false);
+ } catch (final IllegalArgumentException | IllegalAccessException exp) {
+ throw new RuntimeException(exp);
+ }
+ }
+ return null;
+ }
+ });
+ }
+
private void initTypedArray() {
this.builtinArrayBuffer = initConstructor("ArrayBuffer");
this.builtinInt8Array = initConstructor("Int8Array");
diff --git a/src/jdk/nashorn/internal/objects/NativeDebug.java b/src/jdk/nashorn/internal/objects/NativeDebug.java
index 3d6fcbc8..8188f426 100644
--- a/src/jdk/nashorn/internal/objects/NativeDebug.java
+++ b/src/jdk/nashorn/internal/objects/NativeDebug.java
@@ -66,7 +66,7 @@ public final class NativeDebug extends ScriptObject {
public static Object getContext(final Object self) {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
- sm.checkPermission(new RuntimePermission("getNashornContext"));
+ sm.checkPermission(new RuntimePermission("nashorn.getContext"));
}
return Global.getThisContext();
}
diff --git a/src/jdk/nashorn/internal/objects/NativeJava.java b/src/jdk/nashorn/internal/objects/NativeJava.java
index fc16962e..2519c028 100644
--- a/src/jdk/nashorn/internal/objects/NativeJava.java
+++ b/src/jdk/nashorn/internal/objects/NativeJava.java
@@ -223,6 +223,23 @@ public final class NativeJava {
}
/**
+ * Returns name of a java type {@link StaticClass}.
+ * @param self not used
+ * @param type the type whose name is returned
+ * @return name of the given type
+ */
+ @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
+ public static Object typeName(final Object self, final Object type) {
+ if (type instanceof StaticClass) {
+ return ((StaticClass)type).getRepresentedClass().getName();
+ } else if (type instanceof Class) {
+ return ((Class<?>)type).getName();
+ } else {
+ return UNDEFINED;
+ }
+ }
+
+ /**
* Given a JavaScript array and a Java type, returns a Java array with the same initial contents, and with the
* specified component type. Example:
* <pre>
diff --git a/src/jdk/nashorn/internal/runtime/Context.java b/src/jdk/nashorn/internal/runtime/Context.java
index 1c4a08c8..516c0a79 100644
--- a/src/jdk/nashorn/internal/runtime/Context.java
+++ b/src/jdk/nashorn/internal/runtime/Context.java
@@ -39,10 +39,13 @@ import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.net.MalformedURLException;
import java.net.URL;
+import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.CodeSigner;
import java.security.CodeSource;
+import java.security.Permissions;
import java.security.PrivilegedAction;
+import java.security.ProtectionDomain;
import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
import jdk.nashorn.internal.codegen.Compiler;
@@ -123,7 +126,7 @@ public final class Context {
if (callerLoader != myLoader &&
!(callerLoader instanceof StructureLoader) &&
!(JavaAdapterFactory.isAdapterClass(caller))) {
- sm.checkPermission(new RuntimePermission("getNashornGlobal"));
+ sm.checkPermission(new RuntimePermission("nashorn.getGlobal"));
}
}
@@ -137,7 +140,7 @@ public final class Context {
public static void setGlobal(final ScriptObject global) {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
- sm.checkPermission(new RuntimePermission("setNashornGlobal"));
+ sm.checkPermission(new RuntimePermission("nashorn.setGlobal"));
}
if (global != null && !(global instanceof GlobalObject)) {
@@ -154,7 +157,7 @@ public final class Context {
public static Context getContext() {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
- sm.checkPermission(new RuntimePermission("getNashornContext"));
+ sm.checkPermission(new RuntimePermission("nashorn.getContext"));
}
return getContextTrusted();
}
@@ -267,7 +270,7 @@ public final class Context {
public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader) {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
- sm.checkPermission(new RuntimePermission("createNashornContext"));
+ sm.checkPermission(new RuntimePermission("nashorn.createContext"));
}
this.env = new ScriptEnvironment(options, out, err);
@@ -533,7 +536,13 @@ public final class Context {
if (index != -1) {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
- sm.checkPackageAccess(fullName.substring(0, index));
+ AccessController.doPrivileged(new PrivilegedAction<Void>() {
+ @Override
+ public Void run() {
+ sm.checkPackageAccess(fullName.substring(0, index));
+ return null;
+ }
+ }, createNoPermissionsContext());
}
}
@@ -599,7 +608,7 @@ public final class Context {
public ScriptObject newGlobal() {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
- sm.checkPermission(new RuntimePermission("createNashornGlobal"));
+ sm.checkPermission(new RuntimePermission("nashorn.newGlobal"));
}
return newGlobalTrusted();
@@ -676,6 +685,10 @@ public final class Context {
return (context != null) ? context : Context.getContextTrusted();
}
+ private static AccessControlContext createNoPermissionsContext() {
+ return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
+ }
+
private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
ScriptFunction script = null;
diff --git a/src/jdk/nashorn/internal/runtime/NashornLoader.java b/src/jdk/nashorn/internal/runtime/NashornLoader.java
index 80a0b8e6..5ce31008 100644
--- a/src/jdk/nashorn/internal/runtime/NashornLoader.java
+++ b/src/jdk/nashorn/internal/runtime/NashornLoader.java
@@ -30,6 +30,10 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
+import java.security.CodeSource;
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.security.Permissions;
import java.security.SecureClassLoader;
import jdk.nashorn.tools.Shell;
@@ -40,6 +44,28 @@ import jdk.nashorn.tools.Shell;
*
*/
abstract class NashornLoader extends SecureClassLoader {
+ private static final String OBJECTS_PKG = "jdk.nashorn.internal.objects";
+ private static final String RUNTIME_PKG = "jdk.nashorn.internal.runtime";
+ private static final String RUNTIME_LINKER_PKG = "jdk.nashorn.internal.runtime.linker";
+ private static final String SCRIPTS_PKG = "jdk.nashorn.internal.scripts";
+
+ private static final Permission[] SCRIPT_PERMISSIONS;
+ static {
+ SCRIPT_PERMISSIONS = new Permission[4];
+
+ /*
+ * Generated classes get access to runtime, runtime.linker, objects, scripts packages.
+ * Note that the actual scripts can not access these because Java.type, Packages
+ * prevent these restricted packages. And Java reflection and JSR292 access is prevented
+ * for scripts. In other words, nashorn generated portions of script classes can access
+ * clases in these implementation packages.
+ */
+ SCRIPT_PERMISSIONS[0] = new RuntimePermission("accessClassInPackage." + RUNTIME_PKG);
+ SCRIPT_PERMISSIONS[1] = new RuntimePermission("accessClassInPackage." + RUNTIME_LINKER_PKG);
+ SCRIPT_PERMISSIONS[2] = new RuntimePermission("accessClassInPackage." + OBJECTS_PKG);
+ SCRIPT_PERMISSIONS[3] = new RuntimePermission("accessClassInPackage." + SCRIPTS_PKG);
+ }
+
private final Context context;
final Context getContext() {
@@ -68,11 +94,30 @@ abstract class NashornLoader extends SecureClassLoader {
if (i != -1) {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
- sm.checkPackageAccess(name.substring(0, i));
+ final String pkgName = name.substring(0, i);
+ switch (pkgName) {
+ case RUNTIME_PKG:
+ case RUNTIME_LINKER_PKG:
+ case OBJECTS_PKG:
+ case SCRIPTS_PKG:
+ // allow it.
+ break;
+ default:
+ sm.checkPackageAccess(pkgName);
+ }
}
}
}
+ @Override
+ protected PermissionCollection getPermissions(CodeSource codesource) {
+ final Permissions permCollection = new Permissions();
+ for (final Permission perm : SCRIPT_PERMISSIONS) {
+ permCollection.add(perm);
+ }
+ return permCollection;
+ }
+
/**
* Create a secure URL class loader for the given classpath
* @param classPath classpath for the loader to search from
diff --git a/src/jdk/nashorn/internal/runtime/ScriptLoader.java b/src/jdk/nashorn/internal/runtime/ScriptLoader.java
index d6321662..adecd8a3 100644
--- a/src/jdk/nashorn/internal/runtime/ScriptLoader.java
+++ b/src/jdk/nashorn/internal/runtime/ScriptLoader.java
@@ -26,6 +26,7 @@
package jdk.nashorn.internal.runtime;
import java.security.CodeSource;
+import java.security.ProtectionDomain;
/**
* Responsible for loading script generated classes.
@@ -57,6 +58,10 @@ final class ScriptLoader extends NashornLoader {
* @return Installed class.
*/
synchronized Class<?> installClass(final String name, final byte[] data, final CodeSource cs) {
- return defineClass(name, data, 0, data.length, cs);
+ if (cs == null) {
+ return defineClass(name, data, 0, data.length, new ProtectionDomain(null, getPermissions(null)));
+ } else {
+ return defineClass(name, data, 0, data.length, cs);
+ }
}
}
diff --git a/src/jdk/nashorn/internal/runtime/StructureLoader.java b/src/jdk/nashorn/internal/runtime/StructureLoader.java
index da64655f..35786b0b 100644
--- a/src/jdk/nashorn/internal/runtime/StructureLoader.java
+++ b/src/jdk/nashorn/internal/runtime/StructureLoader.java
@@ -38,6 +38,7 @@ import java.security.CodeSigner;
import java.security.CodeSource;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
+import java.security.ProtectionDomain;
import jdk.nashorn.internal.codegen.ObjectClassGenerator;
/**
@@ -129,6 +130,6 @@ final class StructureLoader extends NashornLoader {
}
final byte[] code = new ObjectClassGenerator(context).generate(descriptor);
- return defineClass(name, code, 0, code.length);
+ return defineClass(name, code, 0, code.length, new ProtectionDomain(null, getPermissions(null)));
}
}
diff --git a/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java b/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java
index 3c5e2d81..6b55656b 100644
--- a/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java
+++ b/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java
@@ -57,7 +57,7 @@ public final class Bootstrap {
static {
final DynamicLinkerFactory factory = new DynamicLinkerFactory();
factory.setPrioritizedLinkers(new NashornLinker(), new NashornPrimitiveLinker(), new NashornStaticClassLinker(),
- new JSObjectLinker());
+ new JSObjectLinker(), new ReflectionCheckLinker());
factory.setFallbackLinkers(new BeansLinker(), new NashornBottomLinker());
factory.setSyncOnRelink(true);
final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", -1);
diff --git a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java
index a9e4bd01..ba599da1 100644
--- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java
+++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterFactory.java
@@ -54,6 +54,7 @@ import java.security.CodeSigner;
import java.security.CodeSource;
import java.security.Permissions;
import java.security.PrivilegedAction;
+import java.security.PrivilegedExceptionAction;
import java.security.ProtectionDomain;
import java.security.SecureClassLoader;
import java.security.SecureRandom;
@@ -410,9 +411,13 @@ public final class JavaAdapterFactory {
*/
public static MethodHandle getConstructor(final Class<?> sourceType, final Class<?> targetType) throws Exception {
final StaticClass adapterClass = getAdapterClassFor(new Class<?>[] { targetType });
- return MH.bindTo(Bootstrap.getLinkerServices().getGuardedInvocation(new LinkRequestImpl(NashornCallSiteDescriptor.get(
- "dyn:new", MethodType.methodType(targetType, StaticClass.class, sourceType), 0), false,
- adapterClass, null)).getInvocation(), adapterClass);
+ return AccessController.doPrivileged(new PrivilegedExceptionAction<MethodHandle>() {
+ public MethodHandle run() throws Exception {
+ return MH.bindTo(Bootstrap.getLinkerServices().getGuardedInvocation(new LinkRequestImpl(NashornCallSiteDescriptor.get(
+ "dyn:new", MethodType.methodType(targetType, StaticClass.class, sourceType), 0), false,
+ adapterClass, null)).getInvocation(), adapterClass);
+ }
+ });
}
/**
@@ -456,9 +461,26 @@ public final class JavaAdapterFactory {
private static ClassLoader createClassLoader(final ClassLoader parentLoader, final String className,
final byte[] classBytes, final String privilegedActionClassName) {
return new AdapterLoader(parentLoader) {
+ private final ClassLoader myLoader = getClass().getClassLoader();
private final ProtectionDomain myProtectionDomain = getClass().getProtectionDomain();
@Override
+ public Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
+ try {
+ return super.loadClass(name, resolve);
+ } catch (final SecurityException se) {
+ // we may be implementing an interface or extending a class that was
+ // loaded by a loader that prevents package.access. If so, it'd throw
+ // SecurityException for nashorn's classes!. For adapter's to work, we
+ // should be able to refer to nashorn classes.
+ if (name.startsWith("jdk.nashorn.internal.")) {
+ return myLoader.loadClass(name);
+ }
+ throw se;
+ }
+ }
+
+ @Override
protected Class<?> findClass(final String name) throws ClassNotFoundException {
if(name.equals(className)) {
final byte[] bytes = classBytes;
diff --git a/src/jdk/nashorn/internal/runtime/linker/ReflectionCheckLinker.java b/src/jdk/nashorn/internal/runtime/linker/ReflectionCheckLinker.java
new file mode 100644
index 00000000..eb8837a8
--- /dev/null
+++ b/src/jdk/nashorn/internal/runtime/linker/ReflectionCheckLinker.java
@@ -0,0 +1,60 @@
+/*
+ * 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.
+ */
+
+package jdk.nashorn.internal.runtime.linker;
+
+import jdk.internal.dynalink.linker.GuardedInvocation;
+import jdk.internal.dynalink.linker.LinkRequest;
+import jdk.internal.dynalink.linker.LinkerServices;
+import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
+
+/**
+ * Check java reflection permission for java reflective and java.lang.invoke access from scripts
+ */
+final class ReflectionCheckLinker implements TypeBasedGuardingDynamicLinker{
+ @Override
+ public boolean canLinkType(final Class<?> type) {
+ return canLinkTypeStatic(type);
+ }
+
+ private static boolean canLinkTypeStatic(final Class<?> type) {
+ if (type == Class.class || ClassLoader.class.isAssignableFrom(type)) {
+ return true;
+ }
+ final String name = type.getName();
+ return name.startsWith("java.lang.reflect.") || name.startsWith("java.lang.invoke.");
+ }
+
+ @Override
+ public GuardedInvocation getGuardedInvocation(final LinkRequest origRequest, final LinkerServices linkerServices)
+ throws Exception {
+ final SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ sm.checkPermission(new RuntimePermission("nashorn.JavaReflection"));
+ }
+ // let the next linker deal with actual linking
+ return null;
+ }
+}
diff --git a/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js b/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js
index 15c67f94..0b967d69 100644
--- a/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js
+++ b/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js
@@ -1,21 +1,21 @@
/*
* 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.
@@ -34,7 +34,7 @@ Object.defineProperty(this, "JavaAdapter", {
if (arguments.length < 2) {
throw new TypeError("JavaAdapter requires atleast two arguments");
}
-
+
var types = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
var NewType = Java.extend.apply(Java, types);
return new NewType(arguments[arguments.length - 1]);
@@ -56,10 +56,10 @@ Object.defineProperty(this, "importPackage", {
return type;
} catch (e) {}
}
-
+
return oldNoSuchProperty? oldNoSuchProperty(name) : undefined;
}
-
+
var prefix = "[JavaPackage ";
return function() {
for (var i in arguments) {
@@ -343,7 +343,9 @@ Object.defineProperty(this, "importClass", {
configurable: true, enumerable: false, writable: true,
value: function(clazz) {
if (Java.isType(clazz)) {
- this[clazz.class.getSimpleName()] = clazz;
+ var className = Java.typeName(clazz);
+ var simpleName = className.substring(className.lastIndexOf('.') + 1);
+ this[simpleName] = clazz;
} else {
throw new TypeError(clazz + " is not a Java class");
}
diff --git a/src/jdk/nashorn/internal/runtime/resources/parser.js b/src/jdk/nashorn/internal/runtime/resources/parser.js
index b89f7e12..8671d365 100644
--- a/src/jdk/nashorn/internal/runtime/resources/parser.js
+++ b/src/jdk/nashorn/internal/runtime/resources/parser.js
@@ -1,21 +1,21 @@
/*
* 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.
@@ -47,7 +47,7 @@ function parse(/*code, [name], [location]*/) {
code = arguments[0];
}
- var jsonStr = Packages.jdk.nashorn.internal.runtime.ScriptRuntime.parse(code, name, location);
+ var jsonStr = Packages.jdk.nashorn.api.scripting.ScriptUtils.parse(code, name, location);
return JSON.parse(jsonStr,
function (prop, value) {
if (typeof(value) == 'string' && prop == "value") {
diff --git a/test/script/basic/JDK-8008448.js b/test/script/basic/JDK-8008448.js
index d5ffbd4c..b30e3417 100644
--- a/test/script/basic/JDK-8008448.js
+++ b/test/script/basic/JDK-8008448.js
@@ -32,7 +32,7 @@
var File = Java.type("java.io.File");
var FilenameFilter = Java.type("java.io.FilenameFilter");
-var Source = Java.type("jdk.nashorn.internal.runtime.Source")
+var SourceHelper = Java.type("jdk.nashorn.test.models.SourceHelper")
// Filter out non .js files
var files = new File(__DIR__).listFiles(new FilenameFilter() {
@@ -44,5 +44,5 @@ load("nashorn:parser.js");
// parse each file to make sure it does not result in exception
for each (var f in files) {
- parse(new Source(f.toString(), f).getString());
+ parse(SourceHelper.readFully(f));
}
diff --git a/test/script/basic/NASHORN-401.js b/test/script/basic/NASHORN-401.js
index b6058a9b..6a663e1a 100644
--- a/test/script/basic/NASHORN-401.js
+++ b/test/script/basic/NASHORN-401.js
@@ -28,7 +28,7 @@
* @run
*/
-var t = new Packages.jdk.nashorn.internal.runtime.Nashorn401TestSubject();
+var t = new Packages.jdk.nashorn.test.models.Nashorn401TestSubject();
print(t.method2(10));
print(t.method2(10.2));
diff --git a/test/script/basic/consstring.js b/test/script/basic/consstring.js
index b8dc51d8..0cc8b1f8 100644
--- a/test/script/basic/consstring.js
+++ b/test/script/basic/consstring.js
@@ -37,4 +37,4 @@ list.add(String(new String(str + "2"))); // String() called as function with
list.add((str + "3").toString()); // toString() called on primitive string
list.add(new String(str + "4").toString()); // toString() called on String object
-Packages.jdk.nashorn.internal.test.models.StringArgs.checkString(list);
+Packages.jdk.nashorn.test.models.StringArgs.checkString(list);
diff --git a/test/script/basic/fileline.js b/test/script/basic/fileline.js
index 3323a126..ccf879b2 100644
--- a/test/script/basic/fileline.js
+++ b/test/script/basic/fileline.js
@@ -41,8 +41,8 @@ print(file + " : " + __LINE__);
load(__DIR__ + "loadedfile.js");
// Add check for base part of a URL. We can't test __DIR__ inside
-// a script that is downloaded from a URL. check for Source.baseURL
+// a script that is downloaded from a URL. check for SourceHelper.baseURL
// which is exposed as __DIR__ for URL case.
var url = new java.net.URL("http://www.acme.com:8080/foo/bar.js");
-print(Packages.jdk.nashorn.internal.runtime.Source.baseURL(url));
+print(Packages.jdk.nashorn.test.models.SourceHelper.baseURL(url));
diff --git a/test/script/basic/javainnerclasses.js b/test/script/basic/javainnerclasses.js
index df1e74df..c84571d7 100644
--- a/test/script/basic/javainnerclasses.js
+++ b/test/script/basic/javainnerclasses.js
@@ -29,25 +29,25 @@
*/
// Do it with Java.type()
-var outer = new (Java.type("jdk.nashorn.internal.test.models.OuterClass"))("apple")
+var outer = new (Java.type("jdk.nashorn.test.models.OuterClass"))("apple")
print(outer)
-var innerStatic = new (Java.type("jdk.nashorn.internal.test.models.OuterClass$InnerStaticClass"))("orange")
+var innerStatic = new (Java.type("jdk.nashorn.test.models.OuterClass$InnerStaticClass"))("orange")
print(innerStatic)
-var innerNonStatic = new (Java.type("jdk.nashorn.internal.test.models.OuterClass$InnerNonStaticClass"))(outer, "pear")
+var innerNonStatic = new (Java.type("jdk.nashorn.test.models.OuterClass$InnerNonStaticClass"))(outer, "pear")
print(innerNonStatic)
// Now do it with Packages and explicit $ names
-var outer = new Packages.jdk.nashorn.internal.test.models.OuterClass("red")
+var outer = new Packages.jdk.nashorn.test.models.OuterClass("red")
print(outer)
-var innerStatic = new Packages.jdk.nashorn.internal.test.models.OuterClass$InnerStaticClass("green")
+var innerStatic = new Packages.jdk.nashorn.test.models.OuterClass$InnerStaticClass("green")
print(innerStatic)
-var innerNonStatic = new Packages.jdk.nashorn.internal.test.models.OuterClass$InnerNonStaticClass(outer, "blue")
+var innerNonStatic = new Packages.jdk.nashorn.test.models.OuterClass$InnerNonStaticClass(outer, "blue")
print(innerNonStatic)
// Now do it with Packages and nested properties
-var outer = new Packages.jdk.nashorn.internal.test.models.OuterClass("sweet")
+var outer = new Packages.jdk.nashorn.test.models.OuterClass("sweet")
print(outer)
-var innerStatic = new Packages.jdk.nashorn.internal.test.models.OuterClass.InnerStaticClass("sour")
+var innerStatic = new Packages.jdk.nashorn.test.models.OuterClass.InnerStaticClass("sour")
print(innerStatic)
-var innerNonStatic = new Packages.jdk.nashorn.internal.test.models.OuterClass.InnerNonStaticClass(outer, "bitter")
+var innerNonStatic = new Packages.jdk.nashorn.test.models.OuterClass.InnerNonStaticClass(outer, "bitter")
print(innerNonStatic)
diff --git a/test/script/basic/list.js b/test/script/basic/list.js
index 12e4071b..72ae0be7 100644
--- a/test/script/basic/list.js
+++ b/test/script/basic/list.js
@@ -28,7 +28,7 @@
* @run
*/
var l = new java.util.ArrayList();
-print("l.class.name=" + l.class.name) // Has "class" property like any POJO
+print("l.class.name=" + Java.typeName(l.class)) // Has "class" property like any POJO
l.add("foo")
l.add("bar")
diff --git a/test/script/basic/map.js b/test/script/basic/map.js
index 2ab27aba..c024f6eb 100644
--- a/test/script/basic/map.js
+++ b/test/script/basic/map.js
@@ -28,7 +28,7 @@
* @run
*/
var m = new (Java.type("java.util.LinkedHashMap"));
-print("m.class.name=" + m.class.name) // Has "class" property like any POJO
+print("m.class.name=" + Java.typeName(m.class)) // Has "class" property like any POJO
var empty_key = "empty"
diff --git a/test/script/basic/stdin.js b/test/script/basic/stdin.js
index ba546d59..a3a93324 100644
--- a/test/script/basic/stdin.js
+++ b/test/script/basic/stdin.js
@@ -28,8 +28,8 @@
* @run
*/
-print(java.lang.System.in.class.name);
+print(Java.typeName(java.lang.System.in.class));
var prop = "in";
-print(java.lang.System[prop].class.name);
-print(java.lang.System["in"].class.name);
+print(Java.typeName(java.lang.System[prop].class));
+print(Java.typeName(java.lang.System["in"].class));
diff --git a/test/script/sandbox/javaextend.js b/test/script/sandbox/javaextend.js
index 318a6794..33cc6b01 100644
--- a/test/script/sandbox/javaextend.js
+++ b/test/script/sandbox/javaextend.js
@@ -27,7 +27,7 @@
*/
function model(n) {
- return Java.type("jdk.nashorn.internal.test.models." + n)
+ return Java.type("jdk.nashorn.test.models." + n)
}
// Can't extend a final class
diff --git a/test/script/sandbox/javaextend.js.EXPECTED b/test/script/sandbox/javaextend.js.EXPECTED
index 2a5ad63a..69c78189 100644
--- a/test/script/sandbox/javaextend.js.EXPECTED
+++ b/test/script/sandbox/javaextend.js.EXPECTED
@@ -1,6 +1,6 @@
-TypeError: Can not extend final class jdk.nashorn.internal.test.models.FinalClass.
-TypeError: Can not extend class jdk.nashorn.internal.test.models.NoAccessibleConstructorClass as it has no public or protected constructors.
-TypeError: Can not extend/implement non-public class/interface jdk.nashorn.internal.test.models.NonPublicClass.
+TypeError: Can not extend final class jdk.nashorn.test.models.FinalClass.
+TypeError: Can not extend class jdk.nashorn.test.models.NoAccessibleConstructorClass as it has no public or protected constructors.
+TypeError: Can not extend/implement non-public class/interface jdk.nashorn.test.models.NonPublicClass.
TypeError: Can not extend multiple classes java.lang.Number and java.lang.Thread. At most one of the specified types can be a class, the rest must all be interfaces.
abcdabcd
run-object
diff --git a/test/script/sandbox/reflection.js b/test/script/sandbox/reflection.js
index 7364879d..e892c646 100644
--- a/test/script/sandbox/reflection.js
+++ b/test/script/sandbox/reflection.js
@@ -30,9 +30,7 @@
*/
function check(e) {
- if (e instanceof java.lang.SecurityException) {
- print(e);
- } else {
+ if (! (e instanceof java.lang.SecurityException)) {
fail("expected SecurityException, got " + e);
}
}
diff --git a/test/script/sandbox/reflection.js.EXPECTED b/test/script/sandbox/reflection.js.EXPECTED
deleted file mode 100644
index 202333ea..00000000
--- a/test/script/sandbox/reflection.js.EXPECTED
+++ /dev/null
@@ -1 +0,0 @@
-java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "accessDeclaredMembers")
diff --git a/test/script/sandbox/unsafe.js b/test/script/sandbox/unsafe.js
index b273cd0b..6b3f43af 100644
--- a/test/script/sandbox/unsafe.js
+++ b/test/script/sandbox/unsafe.js
@@ -30,9 +30,7 @@
*/
function check(e) {
- if (e instanceof java.lang.SecurityException) {
- print(e);
- } else {
+ if (! (e instanceof java.lang.SecurityException)) {
fail("expected SecurityException, got " + e);
}
}
diff --git a/test/script/sandbox/unsafe.js.EXPECTED b/test/script/sandbox/unsafe.js.EXPECTED
deleted file mode 100644
index 3f1cbec0..00000000
--- a/test/script/sandbox/unsafe.js.EXPECTED
+++ /dev/null
@@ -1,4 +0,0 @@
-java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "accessClassInPackage.sun.misc")
-java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "accessClassInPackage.sun.misc")
-java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "accessClassInPackage.sun")
-java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "getClassLoader")
diff --git a/test/script/trusted/urlreader.js b/test/script/trusted/urlreader.js
index a5e06ee5..ca037306 100644
--- a/test/script/trusted/urlreader.js
+++ b/test/script/trusted/urlreader.js
@@ -9,7 +9,7 @@ var URLReader = Java.type("jdk.nashorn.api.scripting.URLReader");
var URL = Java.type("java.net.URL");
var File = Java.type("java.io.File");
var JString = Java.type("java.lang.String");
-var Source = Java.type("jdk.nashorn.internal.runtime.Source");
+var SourceHelper = Java.type("jdk.nashorn.test.models.SourceHelper");
var url = new File(__FILE__).toURI().toURL();
var reader = new URLReader(url);
@@ -19,9 +19,9 @@ var reader = new URLReader(url);
// check URL read
// read URL content by directly reading from URL
-var str = new Source(url.toString(), url).getString();
+var str = SourceHelper.readFully(url);
// read URL content via URLReader
-var content = new JString(Source.readFully(reader));
+var content = new JString(SourceHelper.readFully(reader));
// assert that the content is same
Assert.assertEquals(str, content);
diff --git a/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java b/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java
index f4fd114f..0994b0d2 100644
--- a/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java
+++ b/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java
@@ -47,7 +47,6 @@ import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
-import jdk.nashorn.internal.runtime.Version;
import netscape.javascript.JSObject;
import org.testng.Assert;
import org.testng.annotations.Test;
@@ -129,7 +128,6 @@ public class ScriptEngineTest {
assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
assertEquals(fac.getEngineName(), "Oracle Nashorn");
- assertEquals(fac.getEngineVersion(), Version.version());
assertEquals(fac.getOutputStatement("context"), "print(context)");
assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
@@ -313,27 +311,6 @@ public class ScriptEngineTest {
}
}
- public static void alert(final Object msg) {
- System.out.println(msg);
- }
-
- @Test
- public void exposeMethodTest() {
- final ScriptEngineManager m = new ScriptEngineManager();
- final ScriptEngine e = m.getEngineByName("nashorn");
-
- try {
- final Method alert = ScriptEngineTest.class.getMethod("alert", Object.class);
- // expose a Method object as global var.
- e.put("alert", alert);
- // call the global var.
- e.eval("alert.invoke(null, 'alert! alert!!')");
- } catch (final NoSuchMethodException | SecurityException | ScriptException exp) {
- exp.printStackTrace();
- fail(exp.getMessage());
- }
- }
-
@Test
public void putGlobalFunctionTest() {
final ScriptEngineManager m = new ScriptEngineManager();
@@ -593,13 +570,6 @@ public class ScriptEngineTest {
}
@Test
- public void versionTest() {
- final ScriptEngineManager m = new ScriptEngineManager();
- final ScriptEngine e = m.getEngineByName("nashorn");
- assertEquals(e.getFactory().getEngineVersion(), Version.version());
- }
-
- @Test
public void noEnumerablePropertiesTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
@@ -874,26 +844,4 @@ public class ScriptEngineTest {
fail(se.getMessage());
}
}
-
- @Test
- public void factoryOptionsTest() {
- final ScriptEngineManager sm = new ScriptEngineManager();
- for (ScriptEngineFactory fac : sm.getEngineFactories()) {
- if (fac instanceof NashornScriptEngineFactory) {
- final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
- // specify --no-syntax-extensions flag
- final String[] options = new String[] { "--no-syntax-extensions" };
- final ScriptEngine e = nfac.getScriptEngine(options);
- try {
- // try nashorn specific extension
- e.eval("var f = funtion(x) 2*x;");
- fail("should have thrown exception!");
- } catch (final ScriptException se) {
- }
- return;
- }
- }
-
- fail("Cannot find nashorn factory!");
- }
}
diff --git a/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java b/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java
index 0f147562..0f740a48 100644
--- a/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java
+++ b/test/src/jdk/nashorn/internal/runtime/TrustedScriptEngineTest.java
@@ -25,7 +25,7 @@
package jdk.nashorn.internal.runtime;
-
+import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
@@ -40,6 +40,13 @@ import org.testng.annotations.Test;
* Tests for trusted client usage of nashorn script engine factory extension API
*/
public class TrustedScriptEngineTest {
+ @Test
+ public void versionTest() {
+ final ScriptEngineManager m = new ScriptEngineManager();
+ final ScriptEngine e = m.getEngineByName("nashorn");
+ assertEquals(e.getFactory().getEngineVersion(), Version.version());
+ }
+
private static class MyClassLoader extends ClassLoader {
// to check if script engine uses the specified class loader
private final boolean[] reached = new boolean[1];
@@ -116,4 +123,26 @@ public class TrustedScriptEngineTest {
fail("Cannot find nashorn factory!");
}
+
+ @Test
+ public void factoryOptionsTest() {
+ final ScriptEngineManager sm = new ScriptEngineManager();
+ for (ScriptEngineFactory fac : sm.getEngineFactories()) {
+ if (fac instanceof NashornScriptEngineFactory) {
+ final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
+ // specify --no-syntax-extensions flag
+ final String[] options = new String[] { "--no-syntax-extensions" };
+ final ScriptEngine e = nfac.getScriptEngine(options);
+ try {
+ // try nashorn specific extension
+ e.eval("var f = funtion(x) 2*x;");
+ fail("should have thrown exception!");
+ } catch (final ScriptException se) {
+ }
+ return;
+ }
+ }
+
+ fail("Cannot find nashorn factory!");
+ }
}
diff --git a/test/src/jdk/nashorn/internal/test/models/ConstructorWithArgument.java b/test/src/jdk/nashorn/test/models/ConstructorWithArgument.java
index ba4a2163..9a201575 100644
--- a/test/src/jdk/nashorn/internal/test/models/ConstructorWithArgument.java
+++ b/test/src/jdk/nashorn/test/models/ConstructorWithArgument.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
public abstract class ConstructorWithArgument {
private final String token;
diff --git a/test/src/jdk/nashorn/internal/test/models/DessertTopping.java b/test/src/jdk/nashorn/test/models/DessertTopping.java
index 8427c837..591e032d 100644
--- a/test/src/jdk/nashorn/internal/test/models/DessertTopping.java
+++ b/test/src/jdk/nashorn/test/models/DessertTopping.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
public interface DessertTopping {
public String pourOnDessert();
diff --git a/test/src/jdk/nashorn/internal/test/models/DessertToppingFloorWaxDriver.java b/test/src/jdk/nashorn/test/models/DessertToppingFloorWaxDriver.java
index dc04a0d1..856029ad 100644
--- a/test/src/jdk/nashorn/internal/test/models/DessertToppingFloorWaxDriver.java
+++ b/test/src/jdk/nashorn/test/models/DessertToppingFloorWaxDriver.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
public class DessertToppingFloorWaxDriver {
public void decorateDessert(DessertTopping dt) {
diff --git a/test/src/jdk/nashorn/internal/test/models/FinalClass.java b/test/src/jdk/nashorn/test/models/FinalClass.java
index b20257cb..8a3e8432 100644
--- a/test/src/jdk/nashorn/internal/test/models/FinalClass.java
+++ b/test/src/jdk/nashorn/test/models/FinalClass.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
public final class FinalClass {
//empty
diff --git a/test/src/jdk/nashorn/internal/test/models/FloorWax.java b/test/src/jdk/nashorn/test/models/FloorWax.java
index c094ccf6..44ac96e9 100644
--- a/test/src/jdk/nashorn/internal/test/models/FloorWax.java
+++ b/test/src/jdk/nashorn/test/models/FloorWax.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
public interface FloorWax {
public String shineUpTheFloor();
diff --git a/test/src/jdk/nashorn/internal/runtime/Nashorn401TestSubject.java b/test/src/jdk/nashorn/test/models/Nashorn401TestSubject.java
index ed6475de..2e7d9c6c 100644
--- a/test/src/jdk/nashorn/internal/runtime/Nashorn401TestSubject.java
+++ b/test/src/jdk/nashorn/test/models/Nashorn401TestSubject.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.runtime;
+package jdk.nashorn.test.models;
public class Nashorn401TestSubject {
public String method2(int arg) {
diff --git a/test/src/jdk/nashorn/internal/test/models/NoAccessibleConstructorClass.java b/test/src/jdk/nashorn/test/models/NoAccessibleConstructorClass.java
index c79edfbb..f0ddb1aa 100644
--- a/test/src/jdk/nashorn/internal/test/models/NoAccessibleConstructorClass.java
+++ b/test/src/jdk/nashorn/test/models/NoAccessibleConstructorClass.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
public class NoAccessibleConstructorClass {
NoAccessibleConstructorClass() { }
diff --git a/test/src/jdk/nashorn/internal/test/models/NonPublicClass.java b/test/src/jdk/nashorn/test/models/NonPublicClass.java
index 046cdf24..d0a61856 100644
--- a/test/src/jdk/nashorn/internal/test/models/NonPublicClass.java
+++ b/test/src/jdk/nashorn/test/models/NonPublicClass.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
class NonPublicClass {
public NonPublicClass() { }
diff --git a/test/src/jdk/nashorn/internal/test/models/OuterClass.java b/test/src/jdk/nashorn/test/models/OuterClass.java
index cb9484b9..5db86f28 100644
--- a/test/src/jdk/nashorn/internal/test/models/OuterClass.java
+++ b/test/src/jdk/nashorn/test/models/OuterClass.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
public class OuterClass {
private final String value;
diff --git a/test/src/jdk/nashorn/internal/test/models/OverloadedSam.java b/test/src/jdk/nashorn/test/models/OverloadedSam.java
index c63da54c..05736bca 100644
--- a/test/src/jdk/nashorn/internal/test/models/OverloadedSam.java
+++ b/test/src/jdk/nashorn/test/models/OverloadedSam.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
public interface OverloadedSam {
public void sam(String s);
diff --git a/test/src/jdk/nashorn/internal/test/models/OverrideObject.java b/test/src/jdk/nashorn/test/models/OverrideObject.java
index 40cf7f97..5312ffbb 100644
--- a/test/src/jdk/nashorn/internal/test/models/OverrideObject.java
+++ b/test/src/jdk/nashorn/test/models/OverrideObject.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
public class OverrideObject {
@Override
diff --git a/test/src/jdk/nashorn/test/models/SourceHelper.java b/test/src/jdk/nashorn/test/models/SourceHelper.java
new file mode 100644
index 00000000..46b1e488
--- /dev/null
+++ b/test/src/jdk/nashorn/test/models/SourceHelper.java
@@ -0,0 +1,55 @@
+/*
+ * 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.
+ */
+
+package jdk.nashorn.test.models;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.Reader;
+import java.net.URL;
+import jdk.nashorn.internal.runtime.Source;
+
+/**
+ * Helper class to facilitate script access of nashorn Source class.
+ */
+public final class SourceHelper {
+ private SourceHelper() {}
+
+ public static String baseURL(final URL url) {
+ return Source.baseURL(url);
+ }
+
+ public static String readFully(final File file) throws IOException {
+ return new String(Source.readFully(file));
+ }
+
+ public static String readFully(final URL url) throws IOException {
+ return new Source(url.toString(), url).getString();
+ }
+
+ public static String readFully(final Reader reader) throws IOException {
+ return new String(Source.readFully(reader));
+ }
+}
diff --git a/test/src/jdk/nashorn/internal/test/models/StringArgs.java b/test/src/jdk/nashorn/test/models/StringArgs.java
index 8ecdbfd3..1fdcd5de 100644
--- a/test/src/jdk/nashorn/internal/test/models/StringArgs.java
+++ b/test/src/jdk/nashorn/test/models/StringArgs.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
import java.util.List;
diff --git a/test/src/jdk/nashorn/internal/test/models/Toothpaste.java b/test/src/jdk/nashorn/test/models/Toothpaste.java
index 0946eb35..7eae95f3 100644
--- a/test/src/jdk/nashorn/internal/test/models/Toothpaste.java
+++ b/test/src/jdk/nashorn/test/models/Toothpaste.java
@@ -23,7 +23,7 @@
* questions.
*/
-package jdk.nashorn.internal.test.models;
+package jdk.nashorn.test.models;
public abstract class Toothpaste {
public void applyToBrush() {