Tue, 14 Oct 2014 16:16:12 +0530
8050977: Java8 Javascript Nashorn exception: no current Global instance for nashorn
Reviewed-by: attila, lagergren, hannesw
1.1 --- a/src/jdk/nashorn/api/scripting/NashornScriptEngine.java Mon Oct 13 20:10:14 2014 +0200 1.2 +++ b/src/jdk/nashorn/api/scripting/NashornScriptEngine.java Tue Oct 14 16:16:12 2014 +0530 1.3 @@ -229,6 +229,8 @@ 1.4 } 1.5 1.6 private <T> T getInterfaceInner(final Object thiz, final Class<T> clazz) { 1.7 + assert !(thiz instanceof ScriptObject) : "raw ScriptObject not expected here"; 1.8 + 1.9 if (clazz == null || !clazz.isInterface()) { 1.10 throw new IllegalArgumentException(getMessage("interface.class.expected")); 1.11 } 1.12 @@ -254,17 +256,6 @@ 1.13 if (! isOfContext(realGlobal, nashornContext)) { 1.14 throw new IllegalArgumentException(getMessage("script.object.from.another.engine")); 1.15 } 1.16 - } else if (thiz instanceof ScriptObject) { 1.17 - // called from script code. 1.18 - realSelf = (ScriptObject)thiz; 1.19 - realGlobal = Context.getGlobal(); 1.20 - if (realGlobal == null) { 1.21 - throw new IllegalArgumentException(getMessage("no.current.nashorn.global")); 1.22 - } 1.23 - 1.24 - if (! isOfContext(realGlobal, nashornContext)) { 1.25 - throw new IllegalArgumentException(getMessage("script.object.from.another.engine")); 1.26 - } 1.27 } 1.28 1.29 if (realSelf == null) { 1.30 @@ -368,6 +359,7 @@ 1.31 1.32 private Object invokeImpl(final Object selfObject, final String name, final Object... args) throws ScriptException, NoSuchMethodException { 1.33 name.getClass(); // null check 1.34 + assert !(selfObject instanceof ScriptObject) : "raw ScriptObject not expected here"; 1.35 1.36 Global invokeGlobal = null; 1.37 ScriptObjectMirror selfMirror = null; 1.38 @@ -377,20 +369,6 @@ 1.39 throw new IllegalArgumentException(getMessage("script.object.from.another.engine")); 1.40 } 1.41 invokeGlobal = selfMirror.getHomeGlobal(); 1.42 - } else if (selfObject instanceof ScriptObject) { 1.43 - // invokeMethod called from script code - in which case we may get 'naked' ScriptObject 1.44 - // Wrap it with oldGlobal to make a ScriptObjectMirror for the same. 1.45 - final Global oldGlobal = Context.getGlobal(); 1.46 - invokeGlobal = oldGlobal; 1.47 - if (oldGlobal == null) { 1.48 - throw new IllegalArgumentException(getMessage("no.current.nashorn.global")); 1.49 - } 1.50 - 1.51 - if (! isOfContext(oldGlobal, nashornContext)) { 1.52 - throw new IllegalArgumentException(getMessage("script.object.from.another.engine")); 1.53 - } 1.54 - 1.55 - selfMirror = (ScriptObjectMirror)ScriptObjectMirror.wrap(selfObject, oldGlobal); 1.56 } else if (selfObject == null) { 1.57 // selfObject is null => global function call 1.58 final Global ctxtGlobal = getNashornGlobalFrom(context);
2.1 --- a/src/jdk/nashorn/api/scripting/ScriptUtils.java Mon Oct 13 20:10:14 2014 +0200 2.2 +++ b/src/jdk/nashorn/api/scripting/ScriptUtils.java Tue Oct 14 16:16:12 2014 +0530 2.3 @@ -75,11 +75,8 @@ 2.4 * @param sync the object to synchronize on 2.5 * @return a synchronizing wrapper function 2.6 */ 2.7 - public static Object makeSynchronizedFunction(final Object func, final Object sync) { 2.8 - if (func instanceof ScriptFunction) { 2.9 - return ((ScriptFunction)func).makeSynchronizedFunction(sync); 2.10 - } 2.11 - throw typeError("not.a.function", ScriptRuntime.safeToString(func)); 2.12 + public static Object makeSynchronizedFunction(final ScriptFunction func, final Object sync) { 2.13 + return func.makeSynchronizedFunction(unwrap(sync)); 2.14 } 2.15 2.16 /** 2.17 @@ -88,12 +85,8 @@ 2.18 * @param obj object to be wrapped 2.19 * @return wrapped object 2.20 */ 2.21 - public static Object wrap(final Object obj) { 2.22 - if (obj instanceof ScriptObject) { 2.23 - return ScriptObjectMirror.wrap(obj, Context.getGlobal()); 2.24 - } 2.25 - 2.26 - return obj; 2.27 + public static ScriptObjectMirror wrap(final ScriptObject obj) { 2.28 + return (ScriptObjectMirror) ScriptObjectMirror.wrap(obj, Context.getGlobal()); 2.29 } 2.30 2.31 /** 2.32 @@ -160,14 +153,15 @@ 2.33 } 2.34 2.35 final LinkerServices linker = Bootstrap.getLinkerServices(); 2.36 - final MethodHandle converter = linker.getTypeConverter(obj.getClass(), clazz); 2.37 + final Object objToConvert = unwrap(obj); 2.38 + final MethodHandle converter = linker.getTypeConverter(objToConvert.getClass(), clazz); 2.39 if (converter == null) { 2.40 // no supported conversion! 2.41 throw new UnsupportedOperationException("conversion not supported"); 2.42 } 2.43 2.44 try { 2.45 - return converter.invoke(obj); 2.46 + return converter.invoke(objToConvert); 2.47 } catch (final RuntimeException | Error e) { 2.48 throw e; 2.49 } catch (final Throwable t) {
3.1 --- a/src/jdk/nashorn/internal/codegen/MethodEmitter.java Mon Oct 13 20:10:14 2014 +0200 3.2 +++ b/src/jdk/nashorn/internal/codegen/MethodEmitter.java Tue Oct 14 16:16:12 2014 +0530 3.3 @@ -99,6 +99,7 @@ 3.4 import jdk.nashorn.internal.ir.Symbol; 3.5 import jdk.nashorn.internal.ir.TryNode; 3.6 import jdk.nashorn.internal.objects.Global; 3.7 +import jdk.nashorn.internal.objects.NativeArray; 3.8 import jdk.nashorn.internal.runtime.ArgumentSetter; 3.9 import jdk.nashorn.internal.runtime.Context; 3.10 import jdk.nashorn.internal.runtime.Debug; 3.11 @@ -2126,7 +2127,14 @@ 3.12 3.13 int pos = 0; 3.14 for (int i = argCount - 1; i >= 0; i--) { 3.15 - paramTypes[i] = stack.peek(pos++); 3.16 + Type pt = stack.peek(pos++); 3.17 + // "erase" specific ScriptObject subtype info - except for NativeArray. 3.18 + // NativeArray is used for array/List/Deque conversion for Java calls. 3.19 + if (ScriptObject.class.isAssignableFrom(pt.getTypeClass()) && 3.20 + !NativeArray.class.isAssignableFrom(pt.getTypeClass())) { 3.21 + pt = Type.SCRIPT_OBJECT; 3.22 + } 3.23 + paramTypes[i] = pt; 3.24 } 3.25 final String descriptor = Type.getMethodDescriptor(returnType, paramTypes); 3.26 for (int i = 0; i < argCount; i++) {
4.1 --- a/src/jdk/nashorn/internal/objects/NativeJava.java Mon Oct 13 20:10:14 2014 +0200 4.2 +++ b/src/jdk/nashorn/internal/objects/NativeJava.java Tue Oct 14 16:16:12 2014 +0530 4.3 @@ -90,7 +90,11 @@ 4.4 */ 4.5 @Function(name="synchronized", attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) 4.6 public static Object synchronizedFunc(final Object self, final Object func, final Object obj) { 4.7 - return ScriptUtils.makeSynchronizedFunction(func, obj); 4.8 + if (func instanceof ScriptFunction) { 4.9 + return ((ScriptFunction)func).makeSynchronizedFunction(obj); 4.10 + } 4.11 + 4.12 + throw typeError("not.a.function", ScriptRuntime.safeToString(func)); 4.13 } 4.14 4.15 /**
5.1 --- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java Mon Oct 13 20:10:14 2014 +0200 5.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterBytecodeGenerator.java Tue Oct 14 16:16:12 2014 +0530 5.3 @@ -152,6 +152,7 @@ 5.4 static final String SET_GLOBAL_METHOD_DESCRIPTOR = Type.getMethodDescriptor(Type.VOID_TYPE, OBJECT_TYPE); 5.5 static final String VOID_NOARG_METHOD_DESCRIPTOR = Type.getMethodDescriptor(Type.VOID_TYPE); 5.6 5.7 + private static final Type SCRIPT_OBJECT_TYPE = Type.getType(ScriptObject.class); 5.8 private static final Type SCRIPT_FUNCTION_TYPE = Type.getType(ScriptFunction.class); 5.9 private static final Type STRING_TYPE = Type.getType(String.class); 5.10 private static final Type METHOD_TYPE_TYPE = Type.getType(MethodType.class); 5.11 @@ -536,8 +537,8 @@ 5.12 final int argLen = originalArgTypes.length; 5.13 final Type[] newArgTypes = new Type[argLen + 1]; 5.14 5.15 - // Insert ScriptFunction|Object as the last argument to the constructor 5.16 - final Type extraArgumentType = fromFunction ? SCRIPT_FUNCTION_TYPE : OBJECT_TYPE; 5.17 + // Insert ScriptFunction|ScriptObject as the last argument to the constructor 5.18 + final Type extraArgumentType = fromFunction ? SCRIPT_FUNCTION_TYPE : SCRIPT_OBJECT_TYPE; 5.19 newArgTypes[argLen] = extraArgumentType; 5.20 System.arraycopy(originalArgTypes, 0, newArgTypes, 0, argLen); 5.21 5.22 @@ -588,6 +589,34 @@ 5.23 // Initialize converters 5.24 generateConverterInit(mv, fromFunction); 5.25 endInitMethod(mv); 5.26 + 5.27 + if (! fromFunction) { 5.28 + newArgTypes[argLen] = OBJECT_TYPE; 5.29 + final InstructionAdapter mv2 = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, INIT, 5.30 + Type.getMethodDescriptor(originalCtorType.getReturnType(), newArgTypes), null, null)); 5.31 + generateOverridingConstructorWithObjectParam(mv2, ctor, originalCtorType.getDescriptor()); 5.32 + } 5.33 + } 5.34 + 5.35 + // Object additional param accepting constructor - generated to handle null and undefined value 5.36 + // for script adapters. This is effectively to throw TypeError on such script adapters. See 5.37 + // JavaAdapterServices.getHandle as well. 5.38 + private void generateOverridingConstructorWithObjectParam(final InstructionAdapter mv, final Constructor<?> ctor, final String ctorDescriptor) { 5.39 + mv.visitCode(); 5.40 + mv.visitVarInsn(ALOAD, 0); 5.41 + final Class<?>[] argTypes = ctor.getParameterTypes(); 5.42 + int offset = 1; // First arg is at position 1, after this. 5.43 + for (int i = 0; i < argTypes.length; ++i) { 5.44 + final Type argType = Type.getType(argTypes[i]); 5.45 + mv.load(offset, argType); 5.46 + offset += argType.getSize(); 5.47 + } 5.48 + mv.invokespecial(superClassName, INIT, ctorDescriptor, false); 5.49 + mv.visitVarInsn(ALOAD, offset); 5.50 + mv.visitInsn(ACONST_NULL); 5.51 + mv.visitInsn(ACONST_NULL); 5.52 + mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getHandle", GET_HANDLE_OBJECT_DESCRIPTOR, false); 5.53 + endInitMethod(mv); 5.54 } 5.55 5.56 private static void endInitMethod(final InstructionAdapter mv) {
6.1 --- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java Mon Oct 13 20:10:14 2014 +0200 6.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterClassLoader.java Tue Oct 14 16:16:12 2014 +0530 6.3 @@ -39,6 +39,7 @@ 6.4 import jdk.nashorn.internal.runtime.Context; 6.5 import jdk.nashorn.internal.runtime.JSType; 6.6 import jdk.nashorn.internal.runtime.ScriptFunction; 6.7 +import jdk.nashorn.internal.runtime.ScriptObject; 6.8 6.9 /** 6.10 * This class encapsulates the bytecode of the adapter class and can be used to load it into the JVM as an actual Class. 6.11 @@ -51,7 +52,7 @@ 6.12 private static final AccessControlContext CREATE_LOADER_ACC_CTXT = ClassAndLoader.createPermAccCtxt("createClassLoader"); 6.13 private static final AccessControlContext GET_CONTEXT_ACC_CTXT = ClassAndLoader.createPermAccCtxt(Context.NASHORN_GET_CONTEXT); 6.14 private static final Collection<String> VISIBLE_INTERNAL_CLASS_NAMES = Collections.unmodifiableCollection(new HashSet<>( 6.15 - Arrays.asList(JavaAdapterServices.class.getName(), ScriptFunction.class.getName(), JSType.class.getName()))); 6.16 + Arrays.asList(JavaAdapterServices.class.getName(), ScriptObject.class.getName(), ScriptFunction.class.getName(), JSType.class.getName()))); 6.17 6.18 private final String className; 6.19 private final byte[] classBytes;
7.1 --- a/src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java Mon Oct 13 20:10:14 2014 +0200 7.2 +++ b/src/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java Tue Oct 14 16:16:12 2014 +0530 7.3 @@ -47,7 +47,6 @@ 7.4 import jdk.internal.org.objectweb.asm.Opcodes; 7.5 import jdk.internal.org.objectweb.asm.Type; 7.6 import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; 7.7 -import jdk.nashorn.api.scripting.ScriptUtils; 7.8 import jdk.nashorn.internal.runtime.Context; 7.9 import jdk.nashorn.internal.runtime.ScriptFunction; 7.10 import jdk.nashorn.internal.runtime.ScriptObject; 7.11 @@ -220,7 +219,7 @@ 7.12 * @return the filtered return value. 7.13 */ 7.14 public static Object exportReturnValue(final Object obj) { 7.15 - return ScriptUtils.wrap(NashornBeansLinker.exportArgument(obj)); 7.16 + return NashornBeansLinker.exportArgument(obj, true); 7.17 } 7.18 7.19 /**
8.1 --- a/src/jdk/nashorn/internal/runtime/linker/NashornBeansLinker.java Mon Oct 13 20:10:14 2014 +0200 8.2 +++ b/src/jdk/nashorn/internal/runtime/linker/NashornBeansLinker.java Tue Oct 14 16:16:12 2014 +0530 8.3 @@ -35,17 +35,28 @@ 8.4 import jdk.internal.dynalink.linker.LinkRequest; 8.5 import jdk.internal.dynalink.linker.LinkerServices; 8.6 import jdk.internal.dynalink.support.Lookup; 8.7 +import jdk.nashorn.api.scripting.ScriptUtils; 8.8 +import jdk.nashorn.internal.objects.NativeArray; 8.9 import jdk.nashorn.internal.runtime.ConsString; 8.10 +import jdk.nashorn.internal.runtime.ScriptObject; 8.11 +import jdk.nashorn.internal.runtime.options.Options; 8.12 8.13 /** 8.14 * This linker delegates to a {@code BeansLinker} but passes it a special linker services object that has a modified 8.15 * {@code asType} method that will ensure that we never pass internal engine objects that should not be externally 8.16 - * observable (currently only ConsString) to Java APIs, but rather that we flatten it into a String. We can't just add 8.17 + * observable (currently ConsString and ScriptObject) to Java APIs, but rather that we flatten it into a String. We can't just add 8.18 * this functionality as custom converters via {@code GuaardingTypeConverterFactory}, since they are not consulted when 8.19 * the target method handle parameter signature is {@code Object}. 8.20 */ 8.21 public class NashornBeansLinker implements GuardingDynamicLinker { 8.22 + // System property to control whether to wrap ScriptObject->ScriptObjectMirror for 8.23 + // Object type arguments of Java method calls, field set and array set. 8.24 + private static final boolean MIRROR_ALWAYS = Options.getBooleanProperty("nashorn.mirror.always", true); 8.25 + 8.26 private static final MethodHandle EXPORT_ARGUMENT = new Lookup(MethodHandles.lookup()).findOwnStatic("exportArgument", Object.class, Object.class); 8.27 + private static final MethodHandle EXPORT_NATIVE_ARRAY = new Lookup(MethodHandles.lookup()).findOwnStatic("exportNativeArray", Object.class, NativeArray.class); 8.28 + private static final MethodHandle EXPORT_SCRIPT_OBJECT = new Lookup(MethodHandles.lookup()).findOwnStatic("exportScriptObject", Object.class, ScriptObject.class); 8.29 + private static final MethodHandle IMPORT_RESULT = new Lookup(MethodHandles.lookup()).findOwnStatic("importResult", Object.class, Object.class); 8.30 8.31 private final BeansLinker beansLinker = new BeansLinker(); 8.32 8.33 @@ -67,8 +78,39 @@ 8.34 return delegateLinker.getGuardedInvocation(linkRequest, new NashornBeansLinkerServices(linkerServices)); 8.35 } 8.36 8.37 - static Object exportArgument(final Object arg) { 8.38 - return arg instanceof ConsString ? arg.toString() : arg; 8.39 + @SuppressWarnings("unused") 8.40 + private static Object exportArgument(final Object arg) { 8.41 + return exportArgument(arg, MIRROR_ALWAYS); 8.42 + } 8.43 + 8.44 + @SuppressWarnings("unused") 8.45 + private static Object exportNativeArray(final NativeArray arg) { 8.46 + return exportArgument(arg, MIRROR_ALWAYS); 8.47 + } 8.48 + 8.49 + @SuppressWarnings("unused") 8.50 + private static Object exportScriptObject(final ScriptObject arg) { 8.51 + return exportArgument(arg, MIRROR_ALWAYS); 8.52 + } 8.53 + 8.54 + @SuppressWarnings("unused") 8.55 + private static Object exportScriptArray(final NativeArray arg) { 8.56 + return exportArgument(arg, MIRROR_ALWAYS); 8.57 + } 8.58 + 8.59 + static Object exportArgument(final Object arg, final boolean mirrorAlways) { 8.60 + if (arg instanceof ConsString) { 8.61 + return arg.toString(); 8.62 + } else if (mirrorAlways && arg instanceof ScriptObject) { 8.63 + return ScriptUtils.wrap((ScriptObject)arg); 8.64 + } else { 8.65 + return arg; 8.66 + } 8.67 + } 8.68 + 8.69 + @SuppressWarnings("unused") 8.70 + private static Object importResult(final Object arg) { 8.71 + return ScriptUtils.unwrap(arg); 8.72 } 8.73 8.74 private static class NashornBeansLinkerServices implements LinkerServices { 8.75 @@ -80,23 +122,50 @@ 8.76 8.77 @Override 8.78 public MethodHandle asType(final MethodHandle handle, final MethodType fromType) { 8.79 - final MethodHandle typed = linkerServices.asType(handle, fromType); 8.80 - 8.81 final MethodType handleType = handle.type(); 8.82 final int paramCount = handleType.parameterCount(); 8.83 assert fromType.parameterCount() == handleType.parameterCount(); 8.84 8.85 + MethodType newFromType = fromType; 8.86 MethodHandle[] filters = null; 8.87 for(int i = 0; i < paramCount; ++i) { 8.88 - if(shouldConvert(handleType.parameterType(i), fromType.parameterType(i))) { 8.89 - if(filters == null) { 8.90 + final MethodHandle filter = argConversionFilter(handleType.parameterType(i), fromType.parameterType(i)); 8.91 + if (filter != null) { 8.92 + if (filters == null) { 8.93 filters = new MethodHandle[paramCount]; 8.94 } 8.95 - filters[i] = EXPORT_ARGUMENT; 8.96 + // "erase" specific type with Object type or else we'll get filter mismatch 8.97 + newFromType = newFromType.changeParameterType(i, Object.class); 8.98 + filters[i] = filter; 8.99 } 8.100 } 8.101 8.102 - return filters != null ? MethodHandles.filterArguments(typed, 0, filters) : typed; 8.103 + final MethodHandle typed = linkerServices.asType(handle, newFromType); 8.104 + MethodHandle result = filters != null ? MethodHandles.filterArguments(typed, 0, filters) : typed; 8.105 + // Filter Object typed return value for possible ScriptObjectMirror. We convert 8.106 + // ScriptObjectMirror as ScriptObject (if it is mirror from current global). 8.107 + if (MIRROR_ALWAYS && areBothObjects(handleType.returnType(), fromType.returnType())) { 8.108 + result = MethodHandles.filterReturnValue(result, IMPORT_RESULT); 8.109 + } 8.110 + 8.111 + return result; 8.112 + } 8.113 + 8.114 + private static MethodHandle argConversionFilter(final Class<?> handleType, final Class<?> fromType) { 8.115 + if (handleType == Object.class) { 8.116 + if (fromType == Object.class) { 8.117 + return EXPORT_ARGUMENT; 8.118 + } else if (fromType == NativeArray.class) { 8.119 + return EXPORT_NATIVE_ARRAY; 8.120 + } else if (fromType == ScriptObject.class) { 8.121 + return EXPORT_SCRIPT_OBJECT; 8.122 + } 8.123 + } 8.124 + return null; 8.125 + } 8.126 + 8.127 + private static boolean areBothObjects(final Class<?> handleType, final Class<?> fromType) { 8.128 + return handleType == Object.class && fromType == Object.class; 8.129 } 8.130 8.131 @Override 8.132 @@ -104,10 +173,6 @@ 8.133 return Implementation.asTypeLosslessReturn(this, handle, fromType); 8.134 } 8.135 8.136 - private static boolean shouldConvert(final Class<?> handleType, final Class<?> fromType) { 8.137 - return handleType == Object.class && fromType == Object.class; 8.138 - } 8.139 - 8.140 @Override 8.141 public MethodHandle getTypeConverter(final Class<?> sourceType, final Class<?> targetType) { 8.142 return linkerServices.getTypeConverter(sourceType, targetType);
9.1 --- a/src/jdk/nashorn/internal/runtime/linker/NashornLinker.java Mon Oct 13 20:10:14 2014 +0200 9.2 +++ b/src/jdk/nashorn/internal/runtime/linker/NashornLinker.java Tue Oct 14 16:16:12 2014 +0530 9.3 @@ -292,7 +292,7 @@ 9.4 9.5 @SuppressWarnings("unused") 9.6 private static Object createMirror(final Object obj) { 9.7 - return ScriptUtils.wrap(obj); 9.8 + return obj instanceof ScriptObject? ScriptUtils.wrap((ScriptObject)obj) : obj; 9.9 } 9.10 9.11 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
10.1 --- a/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js Mon Oct 13 20:10:14 2014 +0200 10.2 +++ b/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js Tue Oct 14 16:16:12 2014 +0530 10.3 @@ -105,7 +105,7 @@ 10.4 if (arguments.length < 1 || arguments.length > 2 ) { 10.5 throw "sync(function [,object]) parameter count mismatch"; 10.6 } 10.7 - return Packages.jdk.nashorn.api.scripting.ScriptUtils.makeSynchronizedFunction(func, syncobj); 10.8 + return Java.synchronized(func, syncobj); 10.9 } 10.10 }); 10.11 10.12 @@ -160,7 +160,7 @@ 10.13 configurable: true, enumerable: false, writable: true, 10.14 value: function(state) { 10.15 if (! state) { 10.16 - state = java.util.Collections.newSetFromMap(new java.util.IdentityHashMap()); 10.17 + state = java.util.Collections.newSetFromMap(new java.util.HashMap()); 10.18 } 10.19 if (state.contains(this)) { 10.20 return "{}";
11.1 --- a/test/script/basic/convert.js Mon Oct 13 20:10:14 2014 +0200 11.2 +++ b/test/script/basic/convert.js Tue Oct 14 16:16:12 2014 +0530 11.3 @@ -42,7 +42,7 @@ 11.4 11.5 // object to Map 11.6 obj = { foo: 333, bar: 'hello'}; 11.7 -var map = ScriptUtils.convert(obj, java.util.Map.class); 11.8 +var map = ScriptUtils.wrap(obj); 11.9 print(map instanceof java.util.Map); 11.10 for (m in map) { 11.11 print(m + " " + map[m]);
12.1 --- a/test/script/nosecurity/JDK-8044798.js Mon Oct 13 20:10:14 2014 +0200 12.2 +++ b/test/script/nosecurity/JDK-8044798.js Tue Oct 14 16:16:12 2014 +0530 12.3 @@ -25,6 +25,8 @@ 12.4 * JDK-8044798: API for debugging Nashorn 12.5 * 12.6 * @test 12.7 + * @option -Dnashorn.mirror.always=false 12.8 + * @fork 12.9 * @run 12.10 */ 12.11
13.1 --- a/test/src/jdk/nashorn/api/scripting/ScriptEngineSecurityTest.java Mon Oct 13 20:10:14 2014 +0200 13.2 +++ b/test/src/jdk/nashorn/api/scripting/ScriptEngineSecurityTest.java Tue Oct 14 16:16:12 2014 +0530 13.3 @@ -168,42 +168,6 @@ 13.4 } 13.5 } 13.6 13.7 - @Test 13.8 - /** 13.9 - * Check that script can't implement sensitive package interfaces. 13.10 - */ 13.11 - public void checkSensitiveInterfaceImplTest() throws ScriptException { 13.12 - if (System.getSecurityManager() == null) { 13.13 - // pass vacuously 13.14 - return; 13.15 - } 13.16 - 13.17 - final ScriptEngineManager m = new ScriptEngineManager(); 13.18 - final ScriptEngine e = m.getEngineByName("nashorn"); 13.19 - final Object[] holder = new Object[1]; 13.20 - e.put("holder", holder); 13.21 - // put an empty script object into array 13.22 - e.eval("holder[0] = {}"); 13.23 - // holder[0] is an object of some subclass of ScriptObject 13.24 - final Class<?> ScriptObjectClass = holder[0].getClass().getSuperclass(); 13.25 - final Class<?> PropertyAccessClass = ScriptObjectClass.getInterfaces()[0]; 13.26 - // implementation methods for PropertyAccess class 13.27 - e.eval("function set() {}; function get() {}; function getInt(){} " + 13.28 - "function getDouble(){}; function getLong() {}; " + 13.29 - "this.delete = function () {}; function has() {}; " + 13.30 - "function hasOwnProperty() {}"); 13.31 - 13.32 - // get implementation of a restricted package interface 13.33 - try { 13.34 - log(Objects.toString(((Invocable)e).getInterface((Class<?>)PropertyAccessClass))); 13.35 - fail("should have thrown SecurityException"); 13.36 - } catch (final Exception exp) { 13.37 - if (! (exp instanceof SecurityException)) { 13.38 - fail("SecurityException expected, got " + exp); 13.39 - } 13.40 - } 13.41 - } 13.42 - 13.43 // @bug 8032948: Nashorn linkages awry 13.44 public static class FakeProxy extends Proxy { 13.45 public FakeProxy(final InvocationHandler ih) {
14.1 --- a/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Mon Oct 13 20:10:14 2014 +0200 14.2 +++ b/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Tue Oct 14 16:16:12 2014 +0530 14.3 @@ -38,6 +38,7 @@ 14.4 import java.util.concurrent.Callable; 14.5 import javax.script.Compilable; 14.6 import javax.script.CompiledScript; 14.7 +import javax.script.Invocable; 14.8 import javax.script.ScriptContext; 14.9 import javax.script.ScriptEngine; 14.10 import javax.script.ScriptEngineFactory; 14.11 @@ -629,6 +630,40 @@ 14.12 assertEquals(enumerable, Boolean.FALSE); 14.13 } 14.14 14.15 + public static class Context { 14.16 + private Object myobj; 14.17 + 14.18 + public void set(Object o) { 14.19 + myobj = o; 14.20 + } 14.21 + 14.22 + public Object get() { 14.23 + return myobj; 14.24 + } 14.25 + } 14.26 + 14.27 + // @bug 8050977: Java8 Javascript Nashorn exception: 14.28 + // no current Global instance for nashorn 14.29 + @Test 14.30 + public void currentGlobalMissingTest() throws Exception { 14.31 + final ScriptEngineManager manager = new ScriptEngineManager(); 14.32 + final ScriptEngine e = manager.getEngineByName("nashorn"); 14.33 + 14.34 + final Context ctx = new Context(); 14.35 + e.put("ctx", ctx); 14.36 + e.eval("var obj = { foo: function(str) { return str.toUpperCase() } }"); 14.37 + e.eval("ctx.set(obj)"); 14.38 + final Invocable inv = (Invocable)e; 14.39 + assertEquals("HELLO", inv.invokeMethod(ctx.get(), "foo", "hello")); 14.40 + // try object literal 14.41 + e.eval("ctx.set({ bar: function(str) { return str.toLowerCase() } })"); 14.42 + assertEquals("hello", inv.invokeMethod(ctx.get(), "bar", "HELLO")); 14.43 + // try array literal 14.44 + e.eval("var arr = [ 'hello', 'world' ]"); 14.45 + e.eval("ctx.set(arr)"); 14.46 + assertEquals("helloworld", inv.invokeMethod(ctx.get(), "join", "")); 14.47 + } 14.48 + 14.49 private static void checkProperty(final ScriptEngine e, final String name) 14.50 throws ScriptException { 14.51 final String value = System.getProperty(name);