Merge
This commit is contained in:
commit
d9699bc260
@ -106,7 +106,12 @@ public final class NativeArrayBuffer extends ScriptObject {
|
||||
return new NativeArrayBuffer(0);
|
||||
}
|
||||
|
||||
return new NativeArrayBuffer(JSType.toInt32(args[0]));
|
||||
final Object arg0 = args[0];
|
||||
if (arg0 instanceof ByteBuffer) {
|
||||
return new NativeArrayBuffer((ByteBuffer)arg0);
|
||||
} else {
|
||||
return new NativeArrayBuffer(JSType.toInt32(arg0));
|
||||
}
|
||||
}
|
||||
|
||||
private static ByteBuffer cloneBuffer(final ByteBuffer original, final int begin, final int end) {
|
||||
|
@ -27,11 +27,13 @@ package jdk.nashorn.internal.objects;
|
||||
|
||||
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
|
||||
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collection;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import jdk.internal.dynalink.beans.StaticClass;
|
||||
import jdk.internal.dynalink.support.TypeUtilities;
|
||||
import jdk.nashorn.api.scripting.JSObject;
|
||||
@ -337,7 +339,8 @@ public final class NativeJava {
|
||||
|
||||
/**
|
||||
* Given a script object and a Java type, converts the script object into the desired Java type. Currently it
|
||||
* performs shallow creation of Java arrays, as well as wrapping of objects in Lists and Dequeues. Example:
|
||||
* performs shallow creation of Java arrays, as well as wrapping of objects in Lists, Dequeues, Queues,
|
||||
* and Collections. Example:
|
||||
* <pre>
|
||||
* var anArray = [1, "13", false]
|
||||
* var javaIntArray = Java.to(anArray, "int[]")
|
||||
@ -351,9 +354,10 @@ public final class NativeJava {
|
||||
* object to create. Can not be null. If undefined, a "default" conversion is presumed (allowing the argument to be
|
||||
* omitted).
|
||||
* @return a Java object whose value corresponds to the original script object's value. Specifically, for array
|
||||
* target types, returns a Java array of the same type with contents converted to the array's component type. Does
|
||||
* not recursively convert for multidimensional arrays. For {@link List} or {@link Deque}, returns a live wrapper
|
||||
* around the object, see {@link ListAdapter} for details. Returns null if obj is null.
|
||||
* target types, returns a Java array of the same type with contents converted to the array's component type.
|
||||
* Converts recursively when the target type is multidimensional array. For {@link List}, {@link Deque},
|
||||
* {@link Queue}, or {@link Collection}, returns a live wrapper around the object, see {@link ListAdapter} for
|
||||
* details. Returns null if obj is null.
|
||||
* @throws ClassNotFoundException if the class described by objType is not found
|
||||
*/
|
||||
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
|
||||
@ -383,7 +387,7 @@ public final class NativeJava {
|
||||
return JSType.toJavaArray(obj, targetClass.getComponentType());
|
||||
}
|
||||
|
||||
if(targetClass == List.class || targetClass == Deque.class) {
|
||||
if (targetClass == List.class || targetClass == Deque.class || targetClass == Queue.class || targetClass == Collection.class) {
|
||||
return ListAdapter.create(obj);
|
||||
}
|
||||
|
||||
|
@ -1,56 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import jdk.nashorn.api.scripting.JSObject;
|
||||
|
||||
/**
|
||||
* A ListAdapter that can wraps a JSObject.
|
||||
*/
|
||||
public final class JSObjectListAdapter extends ListAdapter {
|
||||
/**
|
||||
* Creates a new list wrapper for the specified JSObject.
|
||||
* @param obj JSOcript the object to wrap
|
||||
*/
|
||||
public JSObjectListAdapter(final JSObject obj) {
|
||||
super(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return JSType.toInt32(((JSObject)obj).getMember("length"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getAt(final int index) {
|
||||
return ((JSObject)obj).getSlot(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setAt(final int index, final Object element) {
|
||||
((JSObject)obj).setSlot(index, element);
|
||||
}
|
||||
}
|
@ -34,7 +34,6 @@ import java.lang.invoke.MethodHandles;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
import jdk.internal.dynalink.beans.StaticClass;
|
||||
import jdk.nashorn.api.scripting.JSObject;
|
||||
@ -201,12 +200,6 @@ public enum JSType {
|
||||
/** Method handle to convert a JS Object to a Java array. */
|
||||
public static final Call TO_JAVA_ARRAY = staticCall(JSTYPE_LOOKUP, JSType.class, "toJavaArray", Object.class, Object.class, Class.class);
|
||||
|
||||
/** Method handle to convert a JS Object to a Java List. */
|
||||
public static final Call TO_JAVA_LIST = staticCall(JSTYPE_LOOKUP, JSType.class, "toJavaList", List.class, Object.class);
|
||||
|
||||
/** Method handle to convert a JS Object to a Java deque. */
|
||||
public static final Call TO_JAVA_DEQUE = staticCall(JSTYPE_LOOKUP, JSType.class, "toJavaDeque", Deque.class, Object.class);
|
||||
|
||||
/** Method handle for void returns. */
|
||||
public static final Call VOID_RETURN = staticCall(JSTYPE_LOOKUP, JSType.class, "voidReturn", void.class);
|
||||
|
||||
@ -1350,24 +1343,6 @@ public enum JSType {
|
||||
return dst;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a JavaScript object to a Java List. See {@link ListAdapter} for details.
|
||||
* @param obj the object to convert. Can be any array-like object.
|
||||
* @return a List that is live-backed by the JavaScript object.
|
||||
*/
|
||||
public static List<?> toJavaList(final Object obj) {
|
||||
return ListAdapter.create(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a JavaScript object to a Java Deque. See {@link ListAdapter} for details.
|
||||
* @param obj the object to convert. Can be any array-like object.
|
||||
* @return a Deque that is live-backed by the JavaScript object.
|
||||
*/
|
||||
public static Deque<?> toJavaDeque(final Object obj) {
|
||||
return ListAdapter.create(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an object is null or undefined
|
||||
*
|
||||
|
@ -25,17 +25,19 @@
|
||||
|
||||
package jdk.nashorn.internal.runtime;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.util.AbstractList;
|
||||
import java.util.Deque;
|
||||
import java.util.Iterator;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
import java.util.RandomAccess;
|
||||
import java.util.concurrent.Callable;
|
||||
import jdk.nashorn.api.scripting.JSObject;
|
||||
import jdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||
import jdk.nashorn.internal.objects.Global;
|
||||
import jdk.nashorn.internal.runtime.linker.Bootstrap;
|
||||
import jdk.nashorn.internal.runtime.linker.InvokeByName;
|
||||
|
||||
/**
|
||||
* An adapter that can wrap any ECMAScript Array-like object (that adheres to the array rules for the property
|
||||
@ -50,82 +52,47 @@ import jdk.nashorn.internal.runtime.linker.InvokeByName;
|
||||
* operations respectively, while {@link #addLast(Object)} and {@link #removeLast()} will translate to {@code push} and
|
||||
* {@code pop}.
|
||||
*/
|
||||
public abstract class ListAdapter extends AbstractList<Object> implements RandomAccess, Deque<Object> {
|
||||
// These add to the back and front of the list
|
||||
private static final Object PUSH = new Object();
|
||||
private static InvokeByName getPUSH() {
|
||||
return Context.getGlobal().getInvokeByName(PUSH,
|
||||
new Callable<InvokeByName>() {
|
||||
@Override
|
||||
public InvokeByName call() {
|
||||
return new InvokeByName("push", Object.class, void.class, Object.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
public final class ListAdapter extends AbstractList<Object> implements RandomAccess, Deque<Object> {
|
||||
// Invoker creator for methods that add to the start or end of the list: PUSH and UNSHIFT. Takes fn, this, and value, returns void.
|
||||
private static final Callable<MethodHandle> ADD_INVOKER_CREATOR = invokerCreator(void.class, Object.class, JSObject.class, Object.class);
|
||||
|
||||
// PUSH adds to the start of the list
|
||||
private static final Object PUSH = new Object();
|
||||
// UNSHIFT adds to the end of the list
|
||||
private static final Object UNSHIFT = new Object();
|
||||
private static InvokeByName getUNSHIFT() {
|
||||
return Context.getGlobal().getInvokeByName(UNSHIFT,
|
||||
new Callable<InvokeByName>() {
|
||||
@Override
|
||||
public InvokeByName call() {
|
||||
return new InvokeByName("unshift", Object.class, void.class, Object.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// These remove from the back and front of the list
|
||||
// Invoker creator for methods that remove from the tail or head of the list: POP and SHIFT. Takes fn, this, returns Object.
|
||||
private static final Callable<MethodHandle> REMOVE_INVOKER_CREATOR = invokerCreator(Object.class, Object.class, JSObject.class);
|
||||
|
||||
// POP removes from the start of the list
|
||||
private static final Object POP = new Object();
|
||||
private static InvokeByName getPOP() {
|
||||
return Context.getGlobal().getInvokeByName(POP,
|
||||
new Callable<InvokeByName>() {
|
||||
@Override
|
||||
public InvokeByName call() {
|
||||
return new InvokeByName("pop", Object.class, Object.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// SHIFT removes from the end of the list
|
||||
private static final Object SHIFT = new Object();
|
||||
private static InvokeByName getSHIFT() {
|
||||
return Context.getGlobal().getInvokeByName(SHIFT,
|
||||
new Callable<InvokeByName>() {
|
||||
@Override
|
||||
public InvokeByName call() {
|
||||
return new InvokeByName("shift", Object.class, Object.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// These insert and remove in the middle of the list
|
||||
// SPLICE can be used to add a value in the middle of the list.
|
||||
private static final Object SPLICE_ADD = new Object();
|
||||
private static InvokeByName getSPLICE_ADD() {
|
||||
return Context.getGlobal().getInvokeByName(SPLICE_ADD,
|
||||
new Callable<InvokeByName>() {
|
||||
@Override
|
||||
public InvokeByName call() {
|
||||
return new InvokeByName("splice", Object.class, void.class, int.class, int.class, Object.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
private static final Callable<MethodHandle> SPLICE_ADD_INVOKER_CREATOR = invokerCreator(void.class, Object.class, JSObject.class, int.class, int.class, Object.class);
|
||||
|
||||
// SPLICE can also be used to remove values from the middle of the list.
|
||||
private static final Object SPLICE_REMOVE = new Object();
|
||||
private static InvokeByName getSPLICE_REMOVE() {
|
||||
return Context.getGlobal().getInvokeByName(SPLICE_REMOVE,
|
||||
new Callable<InvokeByName>() {
|
||||
@Override
|
||||
public InvokeByName call() {
|
||||
return new InvokeByName("splice", Object.class, void.class, int.class, int.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
private static final Callable<MethodHandle> SPLICE_REMOVE_INVOKER_CREATOR = invokerCreator(void.class, Object.class, JSObject.class, int.class, int.class);
|
||||
|
||||
/** wrapped object */
|
||||
protected final Object obj;
|
||||
private final JSObject obj;
|
||||
private final Global global;
|
||||
|
||||
// allow subclasses only in this package
|
||||
ListAdapter(final Object obj) {
|
||||
ListAdapter(final JSObject obj) {
|
||||
this.obj = obj;
|
||||
this.global = getGlobalNonNull();
|
||||
}
|
||||
|
||||
private static Global getGlobalNonNull() {
|
||||
final Global global = Context.getGlobal();
|
||||
if (global != null) {
|
||||
return global;
|
||||
}
|
||||
throw new IllegalStateException(ECMAErrors.getMessage("list.adapter.null.global"));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -135,14 +102,16 @@ public abstract class ListAdapter extends AbstractList<Object> implements Random
|
||||
* @return A ListAdapter wrapper object
|
||||
*/
|
||||
public static ListAdapter create(final Object obj) {
|
||||
return new ListAdapter(getJSObject(obj));
|
||||
}
|
||||
|
||||
private static JSObject getJSObject(final Object obj) {
|
||||
if (obj instanceof ScriptObject) {
|
||||
final Object mirror = ScriptObjectMirror.wrap(obj, Context.getGlobal());
|
||||
return new JSObjectListAdapter((JSObject)mirror);
|
||||
return (JSObject)ScriptObjectMirror.wrap(obj, Context.getGlobal());
|
||||
} else if (obj instanceof JSObject) {
|
||||
return new JSObjectListAdapter((JSObject)obj);
|
||||
} else {
|
||||
throw new IllegalArgumentException("ScriptObject or JSObject expected");
|
||||
return (JSObject)obj;
|
||||
}
|
||||
throw new IllegalArgumentException("ScriptObject or JSObject expected");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -151,34 +120,29 @@ public abstract class ListAdapter extends AbstractList<Object> implements Random
|
||||
return getAt(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get object at an index
|
||||
* @param index index in list
|
||||
* @return object
|
||||
*/
|
||||
protected abstract Object getAt(final int index);
|
||||
private Object getAt(final int index) {
|
||||
return obj.getSlot(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object set(final int index, final Object element) {
|
||||
checkRange(index);
|
||||
final Object prevValue = getAt(index);
|
||||
setAt(index, element);
|
||||
obj.setSlot(index, element);
|
||||
return prevValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set object at an index
|
||||
* @param index index in list
|
||||
* @param element element
|
||||
*/
|
||||
protected abstract void setAt(final int index, final Object element);
|
||||
|
||||
private void checkRange(final int index) {
|
||||
if(index < 0 || index >= size()) {
|
||||
throw invalidIndex(index);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return JSType.toInt32(obj.getMember("length"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void push(final Object e) {
|
||||
addFirst(e);
|
||||
@ -193,10 +157,7 @@ public abstract class ListAdapter extends AbstractList<Object> implements Random
|
||||
@Override
|
||||
public final void addFirst(final Object e) {
|
||||
try {
|
||||
final InvokeByName unshiftInvoker = getUNSHIFT();
|
||||
final Object fn = unshiftInvoker.getGetter().invokeExact(obj);
|
||||
checkFunction(fn, unshiftInvoker);
|
||||
unshiftInvoker.getInvoker().invokeExact(fn, obj, e);
|
||||
getDynamicInvoker(UNSHIFT, ADD_INVOKER_CREATOR).invokeExact(getFunction("unshift"), obj, e);
|
||||
} catch(RuntimeException | Error ex) {
|
||||
throw ex;
|
||||
} catch(final Throwable t) {
|
||||
@ -207,10 +168,7 @@ public abstract class ListAdapter extends AbstractList<Object> implements Random
|
||||
@Override
|
||||
public final void addLast(final Object e) {
|
||||
try {
|
||||
final InvokeByName pushInvoker = getPUSH();
|
||||
final Object fn = pushInvoker.getGetter().invokeExact(obj);
|
||||
checkFunction(fn, pushInvoker);
|
||||
pushInvoker.getInvoker().invokeExact(fn, obj, e);
|
||||
getDynamicInvoker(PUSH, ADD_INVOKER_CREATOR).invokeExact(getFunction("push"), obj, e);
|
||||
} catch(RuntimeException | Error ex) {
|
||||
throw ex;
|
||||
} catch(final Throwable t) {
|
||||
@ -228,10 +186,7 @@ public abstract class ListAdapter extends AbstractList<Object> implements Random
|
||||
} else {
|
||||
final int size = size();
|
||||
if(index < size) {
|
||||
final InvokeByName spliceAddInvoker = getSPLICE_ADD();
|
||||
final Object fn = spliceAddInvoker.getGetter().invokeExact(obj);
|
||||
checkFunction(fn, spliceAddInvoker);
|
||||
spliceAddInvoker.getInvoker().invokeExact(fn, obj, index, 0, e);
|
||||
getDynamicInvoker(SPLICE_ADD, SPLICE_ADD_INVOKER_CREATOR).invokeExact(obj.getMember("splice"), obj, index, 0, e);
|
||||
} else if(index == size) {
|
||||
addLast(e);
|
||||
} else {
|
||||
@ -244,10 +199,12 @@ public abstract class ListAdapter extends AbstractList<Object> implements Random
|
||||
throw new RuntimeException(t);
|
||||
}
|
||||
}
|
||||
private static void checkFunction(final Object fn, final InvokeByName invoke) {
|
||||
private Object getFunction(final String name) {
|
||||
final Object fn = obj.getMember(name);
|
||||
if(!(Bootstrap.isCallable(fn))) {
|
||||
throw new UnsupportedOperationException("The script object doesn't have a function named " + invoke.getName());
|
||||
throw new UnsupportedOperationException("The script object doesn't have a function named " + name);
|
||||
}
|
||||
return fn;
|
||||
}
|
||||
|
||||
private static IndexOutOfBoundsException invalidIndex(final int index) {
|
||||
@ -321,10 +278,7 @@ public abstract class ListAdapter extends AbstractList<Object> implements Random
|
||||
|
||||
private Object invokeShift() {
|
||||
try {
|
||||
final InvokeByName shiftInvoker = getSHIFT();
|
||||
final Object fn = shiftInvoker.getGetter().invokeExact(obj);
|
||||
checkFunction(fn, shiftInvoker);
|
||||
return shiftInvoker.getInvoker().invokeExact(fn, obj);
|
||||
return getDynamicInvoker(SHIFT, REMOVE_INVOKER_CREATOR).invokeExact(getFunction("shift"), obj);
|
||||
} catch(RuntimeException | Error ex) {
|
||||
throw ex;
|
||||
} catch(final Throwable t) {
|
||||
@ -334,10 +288,7 @@ public abstract class ListAdapter extends AbstractList<Object> implements Random
|
||||
|
||||
private Object invokePop() {
|
||||
try {
|
||||
final InvokeByName popInvoker = getPOP();
|
||||
final Object fn = popInvoker.getGetter().invokeExact(obj);
|
||||
checkFunction(fn, popInvoker);
|
||||
return popInvoker.getInvoker().invokeExact(fn, obj);
|
||||
return getDynamicInvoker(POP, REMOVE_INVOKER_CREATOR).invokeExact(getFunction("pop"), obj);
|
||||
} catch(RuntimeException | Error ex) {
|
||||
throw ex;
|
||||
} catch(final Throwable t) {
|
||||
@ -352,10 +303,7 @@ public abstract class ListAdapter extends AbstractList<Object> implements Random
|
||||
|
||||
private void invokeSpliceRemove(final int fromIndex, final int count) {
|
||||
try {
|
||||
final InvokeByName spliceRemoveInvoker = getSPLICE_REMOVE();
|
||||
final Object fn = spliceRemoveInvoker.getGetter().invokeExact(obj);
|
||||
checkFunction(fn, spliceRemoveInvoker);
|
||||
spliceRemoveInvoker.getInvoker().invokeExact(fn, obj, fromIndex, count);
|
||||
getDynamicInvoker(SPLICE_REMOVE, SPLICE_REMOVE_INVOKER_CREATOR).invokeExact(getFunction("splice"), obj, fromIndex, count);
|
||||
} catch(RuntimeException | Error ex) {
|
||||
throw ex;
|
||||
} catch(final Throwable t) {
|
||||
@ -443,12 +391,24 @@ public abstract class ListAdapter extends AbstractList<Object> implements Random
|
||||
|
||||
private static boolean removeOccurrence(final Object o, final Iterator<Object> it) {
|
||||
while(it.hasNext()) {
|
||||
final Object e = it.next();
|
||||
if(o == null ? e == null : o.equals(e)) {
|
||||
if(Objects.equals(o, it.next())) {
|
||||
it.remove();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static Callable<MethodHandle> invokerCreator(final Class<?> rtype, final Class<?>... ptypes) {
|
||||
return new Callable<MethodHandle>() {
|
||||
@Override
|
||||
public MethodHandle call() {
|
||||
return Bootstrap.createDynamicInvoker("dyn:call", rtype, ptypes);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private MethodHandle getDynamicInvoker(final Object key, final Callable<MethodHandle> creator) {
|
||||
return global.getDynamicInvoker(key, creator);
|
||||
}
|
||||
}
|
||||
|
@ -245,7 +245,7 @@ public final class ScriptingFunctions {
|
||||
* constitute the command line.
|
||||
* @throws IOException in case {@link StreamTokenizer#nextToken()} raises it.
|
||||
*/
|
||||
private static List<String> tokenizeCommandLine(final String execString) throws IOException {
|
||||
public static List<String> tokenizeCommandLine(final String execString) throws IOException {
|
||||
final StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(execString));
|
||||
tokenizer.resetSyntax();
|
||||
tokenizer.wordChars(0, 255);
|
||||
|
@ -29,13 +29,15 @@ import static jdk.nashorn.internal.lookup.Lookup.MH;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodHandles.Lookup;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Collection;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import javax.script.Bindings;
|
||||
import jdk.internal.dynalink.CallSiteDescriptor;
|
||||
import jdk.internal.dynalink.linker.ConversionComparator;
|
||||
@ -47,11 +49,13 @@ import jdk.internal.dynalink.linker.LinkerServices;
|
||||
import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
|
||||
import jdk.internal.dynalink.support.Guards;
|
||||
import jdk.internal.dynalink.support.LinkerServicesImpl;
|
||||
import jdk.internal.dynalink.support.Lookup;
|
||||
import jdk.nashorn.api.scripting.JSObject;
|
||||
import jdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||
import jdk.nashorn.api.scripting.ScriptUtils;
|
||||
import jdk.nashorn.internal.objects.NativeArray;
|
||||
import jdk.nashorn.internal.runtime.JSType;
|
||||
import jdk.nashorn.internal.runtime.ListAdapter;
|
||||
import jdk.nashorn.internal.runtime.ScriptFunction;
|
||||
import jdk.nashorn.internal.runtime.ScriptObject;
|
||||
import jdk.nashorn.internal.runtime.Undefined;
|
||||
@ -167,7 +171,7 @@ final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTyp
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Lookup getCurrentLookup() {
|
||||
private static java.lang.invoke.MethodHandles.Lookup getCurrentLookup() {
|
||||
final LinkRequest currentRequest = AccessController.doPrivileged(new PrivilegedAction<LinkRequest>() {
|
||||
@Override
|
||||
public LinkRequest run() {
|
||||
@ -179,12 +183,12 @@ final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTyp
|
||||
|
||||
/**
|
||||
* Returns a guarded invocation that converts from a source type that is NativeArray to a Java array or List or
|
||||
* Deque type.
|
||||
* Queue or Deque or Collection type.
|
||||
* @param sourceType the source type (presumably NativeArray a superclass of it)
|
||||
* @param targetType the target type (presumably an array type, or List or Deque)
|
||||
* @param targetType the target type (presumably an array type, or List or Queue, or Deque, or Collection)
|
||||
* @return a guarded invocation that converts from the source type to the target type. null is returned if
|
||||
* either the source type is neither NativeArray, nor a superclass of it, or if the target type is not an array
|
||||
* type, List, or Deque.
|
||||
* type, List, Queue, Deque, or Collection.
|
||||
*/
|
||||
private static GuardedInvocation getArrayConverter(final Class<?> sourceType, final Class<?> targetType) {
|
||||
final boolean isSourceTypeNativeArray = sourceType == NativeArray.class;
|
||||
@ -195,12 +199,14 @@ final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTyp
|
||||
final MethodHandle guard = isSourceTypeGeneric ? IS_NATIVE_ARRAY : null;
|
||||
if(targetType.isArray()) {
|
||||
return new GuardedInvocation(ARRAY_CONVERTERS.get(targetType), guard);
|
||||
}
|
||||
if(targetType == List.class) {
|
||||
return new GuardedInvocation(JSType.TO_JAVA_LIST.methodHandle(), guard);
|
||||
}
|
||||
if(targetType == Deque.class) {
|
||||
return new GuardedInvocation(JSType.TO_JAVA_DEQUE.methodHandle(), guard);
|
||||
} else if(targetType == List.class) {
|
||||
return new GuardedInvocation(TO_LIST, guard);
|
||||
} else if(targetType == Deque.class) {
|
||||
return new GuardedInvocation(TO_DEQUE, guard);
|
||||
} else if(targetType == Queue.class) {
|
||||
return new GuardedInvocation(TO_QUEUE, guard);
|
||||
} else if(targetType == Collection.class) {
|
||||
return new GuardedInvocation(TO_COLLECTION, guard);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@ -286,6 +292,23 @@ final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTyp
|
||||
private static final MethodHandle IS_NASHORN_OR_UNDEFINED_TYPE = findOwnMH("isNashornTypeOrUndefined", Boolean.TYPE, Object.class);
|
||||
private static final MethodHandle CREATE_MIRROR = findOwnMH("createMirror", Object.class, Object.class);
|
||||
|
||||
private static final MethodHandle TO_COLLECTION;
|
||||
private static final MethodHandle TO_DEQUE;
|
||||
private static final MethodHandle TO_LIST;
|
||||
private static final MethodHandle TO_QUEUE;
|
||||
static {
|
||||
final MethodHandle listAdapterCreate = new Lookup(MethodHandles.lookup()).findStatic(
|
||||
ListAdapter.class, "create", MethodType.methodType(ListAdapter.class, Object.class));
|
||||
TO_COLLECTION = asReturning(listAdapterCreate, Collection.class);
|
||||
TO_DEQUE = asReturning(listAdapterCreate, Deque.class);
|
||||
TO_LIST = asReturning(listAdapterCreate, List.class);
|
||||
TO_QUEUE = asReturning(listAdapterCreate, Queue.class);
|
||||
}
|
||||
|
||||
private static MethodHandle asReturning(final MethodHandle mh, final Class<?> nrtype) {
|
||||
return mh.asType(mh.type().changeReturnType(nrtype));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static boolean isNashornTypeOrUndefined(final Object obj) {
|
||||
return obj instanceof ScriptObject || obj instanceof Undefined;
|
||||
|
@ -424,9 +424,17 @@ public final class Options {
|
||||
public void process(final String[] args) {
|
||||
final LinkedList<String> argList = new LinkedList<>();
|
||||
addSystemProperties(NASHORN_ARGS_PREPEND_PROPERTY, argList);
|
||||
processArgList(argList);
|
||||
assert argList.isEmpty();
|
||||
Collections.addAll(argList, args);
|
||||
processArgList(argList);
|
||||
assert argList.isEmpty();
|
||||
addSystemProperties(NASHORN_ARGS_PROPERTY, argList);
|
||||
processArgList(argList);
|
||||
assert argList.isEmpty();
|
||||
}
|
||||
|
||||
private void processArgList(final LinkedList<String> argList) {
|
||||
while (!argList.isEmpty()) {
|
||||
final String arg = argList.remove(0);
|
||||
Objects.requireNonNull(arg);
|
||||
|
@ -174,4 +174,4 @@ io.error.cant.write=cannot write "{0}"
|
||||
config.error.no.dest=no destination directory supplied
|
||||
|
||||
uri.error.bad.uri=Bad URI "{0}" near offset {1}
|
||||
|
||||
list.adapter.null.global=Attempted to create the adapter from outside a JavaScript execution context.
|
||||
|
35
nashorn/test/script/basic/JDK-8007456.js
Normal file
35
nashorn/test/script/basic/JDK-8007456.js
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) 2015 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-8007456: Nashorn test framework argument does not handle quoted strings
|
||||
*
|
||||
* @test
|
||||
* @argument "hello world"
|
||||
* @argument "This has spaces"
|
||||
* @run
|
||||
*/
|
||||
|
||||
print(arguments.length);
|
||||
print(arguments[0]);
|
||||
print(arguments[1]);
|
3
nashorn/test/script/basic/JDK-8007456.js.EXPECTED
Normal file
3
nashorn/test/script/basic/JDK-8007456.js.EXPECTED
Normal file
@ -0,0 +1,3 @@
|
||||
2
|
||||
hello world
|
||||
This has spaces
|
57
nashorn/test/script/basic/JDK-8036743.js
Normal file
57
nashorn/test/script/basic/JDK-8036743.js
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2015 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-8036743: need ArrayBuffer constructor with specified data
|
||||
*
|
||||
* @test
|
||||
* @run
|
||||
*/
|
||||
|
||||
var ByteArray = Java.type("byte[]");
|
||||
var ByteBuffer = Java.type("java.nio.ByteBuffer");
|
||||
|
||||
var ba = new ByteArray(4);
|
||||
// use constructor that accepts ByteBuffer as first arg.
|
||||
var abuf = new ArrayBuffer(ByteBuffer.wrap(ba));
|
||||
print("abuf.byteLength = " + abuf.byteLength);
|
||||
var view = new DataView(abuf);
|
||||
|
||||
function setAndPrint(value, endian) {
|
||||
view.setInt32(0, value, endian);
|
||||
print("Little endian? " + endian);
|
||||
print("view[0] = " + view.getInt32(0, endian));
|
||||
print("ba[0] = " + ba[0]);
|
||||
print("ba[1] = " + ba[1]);
|
||||
print("ba[2] = " + ba[2]);
|
||||
print("ba[3] = " + ba[3]);
|
||||
}
|
||||
|
||||
setAndPrint(42, true);
|
||||
setAndPrint(42, false);
|
||||
setAndPrint(java.lang.Byte.MAX_VALUE, true);
|
||||
setAndPrint(java.lang.Byte.MAX_VALUE, false);
|
||||
setAndPrint(java.lang.Short.MAX_VALUE, true);
|
||||
setAndPrint(java.lang.Short.MAX_VALUE, false);
|
||||
setAndPrint(java.lang.Integer.MAX_VALUE, true);
|
||||
setAndPrint(java.lang.Integer.MAX_VALUE, false);
|
49
nashorn/test/script/basic/JDK-8036743.js.EXPECTED
Normal file
49
nashorn/test/script/basic/JDK-8036743.js.EXPECTED
Normal file
@ -0,0 +1,49 @@
|
||||
abuf.byteLength = 4
|
||||
Little endian? true
|
||||
view[0] = 42
|
||||
ba[0] = 42
|
||||
ba[1] = 0
|
||||
ba[2] = 0
|
||||
ba[3] = 0
|
||||
Little endian? false
|
||||
view[0] = 42
|
||||
ba[0] = 0
|
||||
ba[1] = 0
|
||||
ba[2] = 0
|
||||
ba[3] = 42
|
||||
Little endian? true
|
||||
view[0] = 127
|
||||
ba[0] = 127
|
||||
ba[1] = 0
|
||||
ba[2] = 0
|
||||
ba[3] = 0
|
||||
Little endian? false
|
||||
view[0] = 127
|
||||
ba[0] = 0
|
||||
ba[1] = 0
|
||||
ba[2] = 0
|
||||
ba[3] = 127
|
||||
Little endian? true
|
||||
view[0] = 32767
|
||||
ba[0] = -1
|
||||
ba[1] = 127
|
||||
ba[2] = 0
|
||||
ba[3] = 0
|
||||
Little endian? false
|
||||
view[0] = 32767
|
||||
ba[0] = 0
|
||||
ba[1] = 0
|
||||
ba[2] = 127
|
||||
ba[3] = -1
|
||||
Little endian? true
|
||||
view[0] = 2147483647
|
||||
ba[0] = -1
|
||||
ba[1] = -1
|
||||
ba[2] = -1
|
||||
ba[3] = 127
|
||||
Little endian? false
|
||||
view[0] = 2147483647
|
||||
ba[0] = 127
|
||||
ba[1] = -1
|
||||
ba[2] = -1
|
||||
ba[3] = -1
|
46
nashorn/test/script/basic/JDK-8081156.js
Normal file
46
nashorn/test/script/basic/JDK-8081156.js
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2015 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-8081156: jjs "nashorn.args" system property is not effective when script arguments are passed
|
||||
*
|
||||
* @test
|
||||
* @fork
|
||||
* @option -Dnashorn.args=-strict
|
||||
* @argument foo
|
||||
* @argument bar
|
||||
* @run
|
||||
*/
|
||||
|
||||
try {
|
||||
x = 14;
|
||||
throw new Error("should have thrown ReferenceError");
|
||||
} catch (e) {
|
||||
if (! (e instanceof ReferenceError)) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.assertTrue(arguments.length == 2);
|
||||
Assert.assertTrue(arguments[0] == "foo");
|
||||
Assert.assertTrue(arguments[1] == "bar");
|
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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.test;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @bug 8081204
|
||||
* @summary adding and removing elements to a ListAdapter outside of JS context should work.
|
||||
*/
|
||||
@SuppressWarnings("javadoc")
|
||||
public class AddAndRemoveOnListAdapterOutsideOfJavaScriptContextTest {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> T getListAdapter() throws ScriptException {
|
||||
final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
|
||||
return (T)engine.eval("Java.to([1, 2, 3, 4], 'java.util.List')");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokePush() throws ScriptException {
|
||||
final Deque<Object> l = getListAdapter();
|
||||
l.addLast(5);
|
||||
assertEquals(l.size(), 5);
|
||||
assertEquals(l.getLast(), 5);
|
||||
assertEquals(l.getFirst(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPop() throws ScriptException {
|
||||
final Deque<Object> l = getListAdapter();
|
||||
assertEquals(l.removeLast(), 4);
|
||||
assertEquals(l.size(), 3);
|
||||
assertEquals(l.getLast(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnshift() throws ScriptException {
|
||||
final Deque<Object> l = getListAdapter();
|
||||
l.addFirst(0);
|
||||
assertEquals(l.getFirst(), 0);
|
||||
assertEquals(l.getLast(), 4);
|
||||
assertEquals(l.size(), 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShift() throws ScriptException {
|
||||
final Deque<Object> l = getListAdapter();
|
||||
l.removeFirst();
|
||||
assertEquals(l.getFirst(), 2);
|
||||
assertEquals(l.getLast(), 4);
|
||||
assertEquals(l.size(), 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpliceAdd() throws ScriptException {
|
||||
final List<Object> l = getListAdapter();
|
||||
assertEquals(l, Arrays.asList(1, 2, 3, 4));
|
||||
l.add(2, "foo");
|
||||
assertEquals(l, Arrays.asList(1, 2, "foo", 3, 4));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSpliceRemove() throws ScriptException {
|
||||
final List<Object> l = getListAdapter();
|
||||
assertEquals(l, Arrays.asList(1, 2, 3, 4));
|
||||
l.remove(2);
|
||||
assertEquals(l, Arrays.asList(1, 2, 4));
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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.test;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Queue;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
||||
import jdk.nashorn.test.models.JDK_8081015_TestModel;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* @bug 8081015
|
||||
* @summary Test that native arrays get converted to {@link Queue} and {@link Collection}.
|
||||
*/
|
||||
@SuppressWarnings("javadoc")
|
||||
public class JDK_8081015_Test {
|
||||
@Test
|
||||
public void testConvertToCollection() throws ScriptException {
|
||||
test("receiveCollection");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertToDeque() throws ScriptException {
|
||||
test("receiveDeque");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertToList() throws ScriptException {
|
||||
test("receiveList");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertToQueue() throws ScriptException {
|
||||
test("receiveQueue");
|
||||
}
|
||||
|
||||
private static void test(final String methodName) throws ScriptException {
|
||||
final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
|
||||
final JDK_8081015_TestModel model = new JDK_8081015_TestModel();
|
||||
engine.put("test", model);
|
||||
|
||||
assertNull(model.getLastInvoked());
|
||||
engine.eval("test." + methodName + "([1, 2, 3.3, 'foo'])");
|
||||
assertEquals(model.getLastInvoked(), methodName );
|
||||
}
|
||||
}
|
@ -22,7 +22,6 @@
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package jdk.nashorn.internal.test.framework;
|
||||
|
||||
import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_CHECK_COMPILE_MSG;
|
||||
@ -61,14 +60,15 @@ import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
import jdk.nashorn.internal.runtime.ScriptingFunctions;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
@ -78,28 +78,33 @@ import org.xml.sax.InputSource;
|
||||
*/
|
||||
@SuppressWarnings("javadoc")
|
||||
public final class TestFinder {
|
||||
private TestFinder() {}
|
||||
|
||||
interface TestFactory<T> {
|
||||
// 'test' instance type is decided by the client.
|
||||
T createTest(final String framework, final File testFile, final List<String> engineOptions, final Map<String, String> testOptions, final List<String> arguments);
|
||||
// place to log messages from TestFinder
|
||||
void log(String mg);
|
||||
private TestFinder() {
|
||||
}
|
||||
|
||||
interface TestFactory<T> {
|
||||
|
||||
// 'test' instance type is decided by the client.
|
||||
|
||||
T createTest(final String framework, final File testFile, final List<String> engineOptions, final Map<String, String> testOptions, final List<String> arguments);
|
||||
|
||||
// place to log messages from TestFinder
|
||||
|
||||
void log(String mg);
|
||||
}
|
||||
|
||||
// finds all tests from configuration and calls TestFactory to create 'test' instance for each script test found
|
||||
static <T> void findAllTests(final List<T> tests, final Set<String> orphans, final TestFactory<T> testFactory) throws Exception {
|
||||
final String framework = System.getProperty(TEST_JS_FRAMEWORK);
|
||||
final String testList = System.getProperty(TEST_JS_LIST);
|
||||
final String failedTestFileName = System.getProperty(TEST_FAILED_LIST_FILE);
|
||||
if(failedTestFileName != null) {
|
||||
if (failedTestFileName != null) {
|
||||
final File failedTestFile = new File(failedTestFileName);
|
||||
if(failedTestFile.exists() && failedTestFile.length() > 0L) {
|
||||
try(final BufferedReader r = new BufferedReader(new FileReader(failedTestFile))) {
|
||||
for(;;) {
|
||||
if (failedTestFile.exists() && failedTestFile.length() > 0L) {
|
||||
try (final BufferedReader r = new BufferedReader(new FileReader(failedTestFile))) {
|
||||
for (;;) {
|
||||
final String testFileName = r.readLine();
|
||||
if(testFileName == null) {
|
||||
if (testFileName == null) {
|
||||
break;
|
||||
}
|
||||
handleOneTest(framework, new File(testFileName).toPath(), tests, orphans, testFactory);
|
||||
@ -151,7 +156,7 @@ public final class TestFinder {
|
||||
final Exception[] exceptions = new Exception[1];
|
||||
final List<String> excludedActualTests = new ArrayList<>();
|
||||
|
||||
if (! dir.toFile().isDirectory()) {
|
||||
if (!dir.toFile().isDirectory()) {
|
||||
factory.log("WARNING: " + dir + " not found or not a directory");
|
||||
}
|
||||
|
||||
@ -219,27 +224,28 @@ public final class TestFinder {
|
||||
|
||||
boolean explicitOptimistic = false;
|
||||
|
||||
try (Scanner scanner = new Scanner(testFile)) {
|
||||
while (scanner.hasNext()) {
|
||||
// TODO: Scan for /ref=file qualifiers, etc, to determine run
|
||||
// behavior
|
||||
String token = scanner.next();
|
||||
if (token.startsWith("/*")) {
|
||||
inComment = true;
|
||||
} else if (token.endsWith(("*/"))) {
|
||||
inComment = false;
|
||||
} else if (!inComment) {
|
||||
continue;
|
||||
}
|
||||
String allContent = new String(Files.readAllBytes(testFile));
|
||||
Iterator<String> scanner = ScriptingFunctions.tokenizeCommandLine(allContent).iterator();
|
||||
while (scanner.hasNext()) {
|
||||
// TODO: Scan for /ref=file qualifiers, etc, to determine run
|
||||
// behavior
|
||||
String token = scanner.next();
|
||||
if (token.startsWith("/*")) {
|
||||
inComment = true;
|
||||
} else if (token.endsWith(("*/"))) {
|
||||
inComment = false;
|
||||
} else if (!inComment) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// remove whitespace and trailing semicolons, if any
|
||||
// (trailing semicolons are found in some sputnik tests)
|
||||
token = token.trim();
|
||||
final int semicolon = token.indexOf(';');
|
||||
if (semicolon > 0) {
|
||||
token = token.substring(0, semicolon);
|
||||
}
|
||||
switch (token) {
|
||||
// remove whitespace and trailing semicolons, if any
|
||||
// (trailing semicolons are found in some sputnik tests)
|
||||
token = token.trim();
|
||||
final int semicolon = token.indexOf(';');
|
||||
if (semicolon > 0) {
|
||||
token = token.substring(0, semicolon);
|
||||
}
|
||||
switch (token) {
|
||||
case "@test":
|
||||
isTest = true;
|
||||
break;
|
||||
@ -308,24 +314,21 @@ public final class TestFinder {
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// negative tests are expected to fail at runtime only
|
||||
// for those tests that are expected to fail at compile time,
|
||||
// add @test/compile-error
|
||||
if (token.equals("@negative") || token.equals("@strict_mode_negative")) {
|
||||
shouldRun = true;
|
||||
runFailure = true;
|
||||
}
|
||||
// negative tests are expected to fail at runtime only
|
||||
// for those tests that are expected to fail at compile time,
|
||||
// add @test/compile-error
|
||||
if (token.equals("@negative") || token.equals("@strict_mode_negative")) {
|
||||
shouldRun = true;
|
||||
runFailure = true;
|
||||
}
|
||||
|
||||
if (token.equals("@strict_mode") || token.equals("@strict_mode_negative") || token.equals("@onlyStrict") || token.equals("@noStrict")) {
|
||||
if (!strictModeEnabled()) {
|
||||
return;
|
||||
}
|
||||
if (token.equals("@strict_mode") || token.equals("@strict_mode_negative") || token.equals("@onlyStrict") || token.equals("@noStrict")) {
|
||||
if (!strictModeEnabled()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (final Exception ignored) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isTest) {
|
||||
@ -369,8 +372,8 @@ public final class TestFinder {
|
||||
private static final boolean OPTIMISTIC_OVERRIDE = false;
|
||||
|
||||
/**
|
||||
* Check if there is an optimistic override, that disables the default
|
||||
* false optimistic types and sets them to true, for testing purposes
|
||||
* Check if there is an optimistic override, that disables the default false
|
||||
* optimistic types and sets them to true, for testing purposes
|
||||
*
|
||||
* @return true if optimistic type override has been set by test suite
|
||||
*/
|
||||
@ -379,10 +382,9 @@ public final class TestFinder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an optimistic-types=true option to an argument list if this
|
||||
* is set to override the default false. Add an optimistic-types=true
|
||||
* options to an argument list if this is set to override the default
|
||||
* true
|
||||
* Add an optimistic-types=true option to an argument list if this is set to
|
||||
* override the default false. Add an optimistic-types=true options to an
|
||||
* argument list if this is set to override the default true
|
||||
*
|
||||
* @args new argument list array
|
||||
*/
|
||||
@ -396,8 +398,8 @@ public final class TestFinder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an optimistic-types=true option to an argument list if this
|
||||
* is set to override the default false
|
||||
* Add an optimistic-types=true option to an argument list if this is set to
|
||||
* override the default false
|
||||
*
|
||||
* @args argument list
|
||||
*/
|
||||
@ -438,7 +440,7 @@ public final class TestFinder {
|
||||
|
||||
private static void loadExcludesFile(final String testExcludesFile, final Set<String> testExcludeSet) throws XPathExpressionException {
|
||||
final XPath xpath = XPathFactory.newInstance().newXPath();
|
||||
final NodeList testIds = (NodeList)xpath.evaluate("/excludeList/test/@id", new InputSource(testExcludesFile), XPathConstants.NODESET);
|
||||
final NodeList testIds = (NodeList) xpath.evaluate("/excludeList/test/@id", new InputSource(testExcludesFile), XPathConstants.NODESET);
|
||||
for (int i = testIds.getLength() - 1; i >= 0; i--) {
|
||||
testExcludeSet.add(testIds.item(i).getNodeValue());
|
||||
}
|
||||
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertFalse;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Deque;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
@SuppressWarnings("javadoc")
|
||||
public class JDK_8081015_TestModel {
|
||||
private String lastInvoked;
|
||||
|
||||
public void receiveCollection(final Collection<Object> c) {
|
||||
lastInvoked = "receiveCollection";
|
||||
walkCollection(c);
|
||||
}
|
||||
|
||||
public void receiveDeque(final Deque<Object> d) {
|
||||
lastInvoked = "receiveDeque";
|
||||
walkCollection(d);
|
||||
}
|
||||
|
||||
public void receiveList(final List<Object> l) {
|
||||
lastInvoked = "receiveList";
|
||||
walkCollection(l);
|
||||
}
|
||||
|
||||
public void receiveQueue(final Queue<Object> q) {
|
||||
lastInvoked = "receiveQueue";
|
||||
walkCollection(q);
|
||||
}
|
||||
|
||||
public String getLastInvoked() {
|
||||
return lastInvoked;
|
||||
}
|
||||
|
||||
private static void walkCollection(final Collection<Object> c) {
|
||||
final Iterator<Object> it = c.iterator();
|
||||
assertEquals(it.next(), Integer.valueOf(1));
|
||||
assertEquals(it.next(), Integer.valueOf(2));
|
||||
assertEquals(it.next(), Double.valueOf(3.3));
|
||||
assertEquals(it.next(), "foo");
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user