Wed, 14 Aug 2013 20:51:53 +0530
8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists
Reviewed-by: jlaskey, lagergren
1.1 --- a/src/jdk/nashorn/internal/runtime/arrays/ArrayIterator.java Mon Aug 12 18:00:17 2013 -0300 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,88 +0,0 @@ 1.4 -/* 1.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 - * 1.8 - * This code is free software; you can redistribute it and/or modify it 1.9 - * under the terms of the GNU General Public License version 2 only, as 1.10 - * published by the Free Software Foundation. Oracle designates this 1.11 - * particular file as subject to the "Classpath" exception as provided 1.12 - * by Oracle in the LICENSE file that accompanied this code. 1.13 - * 1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 - * version 2 for more details (a copy is included in the LICENSE file that 1.18 - * accompanied this code). 1.19 - * 1.20 - * You should have received a copy of the GNU General Public License version 1.21 - * 2 along with this work; if not, write to the Free Software Foundation, 1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 - * 1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 - * or visit www.oracle.com if you need additional information or have any 1.26 - * questions. 1.27 - */ 1.28 - 1.29 -package jdk.nashorn.internal.runtime.arrays; 1.30 - 1.31 -import jdk.nashorn.internal.runtime.ScriptObject; 1.32 - 1.33 -/** 1.34 - * Iterator over a NativeArray 1.35 - */ 1.36 -class ArrayIterator extends ArrayLikeIterator<Object> { 1.37 - 1.38 - /** Array {@link ScriptObject} to iterate over */ 1.39 - protected final ScriptObject array; 1.40 - 1.41 - /** length of array */ 1.42 - protected final long length; 1.43 - 1.44 - /** 1.45 - * Constructor 1.46 - * @param array array to iterate over 1.47 - * @param includeUndefined should undefined elements be included in iteration 1.48 - */ 1.49 - protected ArrayIterator(final ScriptObject array, final boolean includeUndefined) { 1.50 - super(includeUndefined); 1.51 - this.array = array; 1.52 - this.length = array.getArray().length(); 1.53 - } 1.54 - 1.55 - /** 1.56 - * Is the current index still inside the array 1.57 - * @return true if inside the array 1.58 - */ 1.59 - protected boolean indexInArray() { 1.60 - return index < length; 1.61 - } 1.62 - 1.63 - @Override 1.64 - public Object next() { 1.65 - return array.get(bumpIndex()); 1.66 - } 1.67 - 1.68 - @Override 1.69 - public long getLength() { 1.70 - return length; 1.71 - } 1.72 - 1.73 - @Override 1.74 - public boolean hasNext() { 1.75 - if (!includeUndefined) { 1.76 - while (indexInArray()) { 1.77 - if (array.has(index)) { 1.78 - break; 1.79 - } 1.80 - bumpIndex(); 1.81 - } 1.82 - } 1.83 - 1.84 - return indexInArray(); 1.85 - } 1.86 - 1.87 - @Override 1.88 - public void remove() { 1.89 - array.delete(index, false); 1.90 - } 1.91 -}
2.1 --- a/src/jdk/nashorn/internal/runtime/arrays/ArrayLikeIterator.java Mon Aug 12 18:00:17 2013 -0300 2.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/ArrayLikeIterator.java Wed Aug 14 20:51:53 2013 +0530 2.3 @@ -26,6 +26,7 @@ 2.4 package jdk.nashorn.internal.runtime.arrays; 2.5 2.6 import java.util.Iterator; 2.7 +import java.util.List; 2.8 import jdk.nashorn.api.scripting.ScriptObjectMirror; 2.9 import jdk.nashorn.internal.runtime.JSType; 2.10 import jdk.nashorn.internal.runtime.ScriptObject; 2.11 @@ -49,7 +50,7 @@ 2.12 * 2.13 * @param includeUndefined should undefined elements be included in the iteration? 2.14 */ 2.15 - protected ArrayLikeIterator(final boolean includeUndefined) { 2.16 + ArrayLikeIterator(final boolean includeUndefined) { 2.17 this.includeUndefined = includeUndefined; 2.18 this.index = 0; 2.19 } 2.20 @@ -118,18 +119,26 @@ 2.21 Object obj = object; 2.22 2.23 if (ScriptObject.isArray(obj)) { 2.24 - return new ArrayIterator((ScriptObject) obj, includeUndefined); 2.25 + return new ScriptArrayIterator((ScriptObject) obj, includeUndefined); 2.26 } 2.27 2.28 obj = JSType.toScriptObject(obj); 2.29 if (obj instanceof ScriptObject) { 2.30 - return new MapIterator((ScriptObject)obj, includeUndefined); 2.31 + return new ScriptObjectIterator((ScriptObject)obj, includeUndefined); 2.32 } 2.33 2.34 if (obj instanceof ScriptObjectMirror) { 2.35 return new ScriptObjectMirrorIterator((ScriptObjectMirror)obj, includeUndefined); 2.36 } 2.37 2.38 + if (obj instanceof List) { 2.39 + return new JavaListIterator((List)obj, includeUndefined); 2.40 + } 2.41 + 2.42 + if (obj != null && obj.getClass().isArray()) { 2.43 + return new JavaArrayIterator(obj, includeUndefined); 2.44 + } 2.45 + 2.46 return new EmptyArrayLikeIterator(); 2.47 } 2.48 2.49 @@ -143,19 +152,25 @@ 2.50 Object obj = object; 2.51 2.52 if (ScriptObject.isArray(obj)) { 2.53 - return new ReverseArrayIterator((ScriptObject) obj, includeUndefined); 2.54 + return new ReverseScriptArrayIterator((ScriptObject) obj, includeUndefined); 2.55 } 2.56 2.57 obj = JSType.toScriptObject(obj); 2.58 if (obj instanceof ScriptObject) { 2.59 - return new ReverseMapIterator((ScriptObject)obj, includeUndefined); 2.60 + return new ReverseScriptObjectIterator((ScriptObject)obj, includeUndefined); 2.61 } 2.62 2.63 if (obj instanceof ScriptObjectMirror) { 2.64 return new ReverseScriptObjectMirrorIterator((ScriptObjectMirror)obj, includeUndefined); 2.65 } 2.66 2.67 - assert !obj.getClass().isArray(); 2.68 + if (obj instanceof List) { 2.69 + return new ReverseJavaListIterator((List)obj, includeUndefined); 2.70 + } 2.71 + 2.72 + if (obj != null && obj.getClass().isArray()) { 2.73 + return new ReverseJavaArrayIterator(obj, includeUndefined); 2.74 + } 2.75 2.76 return new EmptyArrayLikeIterator(); 2.77 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/JavaArrayIterator.java Wed Aug 14 20:51:53 2013 +0530 3.3 @@ -0,0 +1,80 @@ 3.4 +/* 3.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. Oracle designates this 3.11 + * particular file as subject to the "Classpath" exception as provided 3.12 + * by Oracle in the LICENSE file that accompanied this code. 3.13 + * 3.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.17 + * version 2 for more details (a copy is included in the LICENSE file that 3.18 + * accompanied this code). 3.19 + * 3.20 + * You should have received a copy of the GNU General Public License version 3.21 + * 2 along with this work; if not, write to the Free Software Foundation, 3.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.23 + * 3.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.25 + * or visit www.oracle.com if you need additional information or have any 3.26 + * questions. 3.27 + */ 3.28 + 3.29 +package jdk.nashorn.internal.runtime.arrays; 3.30 + 3.31 +import java.lang.reflect.Array; 3.32 + 3.33 +/** 3.34 + * Iterator over a Java List. 3.35 + */ 3.36 +class JavaArrayIterator extends ArrayLikeIterator<Object> { 3.37 + 3.38 + /** Array to iterate over */ 3.39 + protected final Object array; 3.40 + 3.41 + /** length of array */ 3.42 + protected final long length; 3.43 + 3.44 + /** 3.45 + * Constructor 3.46 + * @param array array to iterate over 3.47 + * @param includeUndefined should undefined elements be included in iteration 3.48 + */ 3.49 + protected JavaArrayIterator(final Object array, final boolean includeUndefined) { 3.50 + super(includeUndefined); 3.51 + assert array.getClass().isArray() : "expecting Java array object"; 3.52 + this.array = array; 3.53 + this.length = Array.getLength(array); 3.54 + } 3.55 + 3.56 + /** 3.57 + * Is the current index still inside the array 3.58 + * @return true if inside the array 3.59 + */ 3.60 + protected boolean indexInArray() { 3.61 + return index < length; 3.62 + } 3.63 + 3.64 + @Override 3.65 + public Object next() { 3.66 + return Array.get(array, (int)bumpIndex()); 3.67 + } 3.68 + 3.69 + @Override 3.70 + public long getLength() { 3.71 + return length; 3.72 + } 3.73 + 3.74 + @Override 3.75 + public boolean hasNext() { 3.76 + return indexInArray(); 3.77 + } 3.78 + 3.79 + @Override 3.80 + public void remove() { 3.81 + throw new UnsupportedOperationException("remove"); 3.82 + } 3.83 +} 3.84 \ No newline at end of file
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/JavaListIterator.java Wed Aug 14 20:51:53 2013 +0530 4.3 @@ -0,0 +1,79 @@ 4.4 +/* 4.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. Oracle designates this 4.11 + * particular file as subject to the "Classpath" exception as provided 4.12 + * by Oracle in the LICENSE file that accompanied this code. 4.13 + * 4.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.17 + * version 2 for more details (a copy is included in the LICENSE file that 4.18 + * accompanied this code). 4.19 + * 4.20 + * You should have received a copy of the GNU General Public License version 4.21 + * 2 along with this work; if not, write to the Free Software Foundation, 4.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.23 + * 4.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.25 + * or visit www.oracle.com if you need additional information or have any 4.26 + * questions. 4.27 + */ 4.28 + 4.29 +package jdk.nashorn.internal.runtime.arrays; 4.30 + 4.31 +import java.util.List; 4.32 + 4.33 +/** 4.34 + * Iterator over a Java List. 4.35 + */ 4.36 +class JavaListIterator extends ArrayLikeIterator<Object> { 4.37 + 4.38 + /** {@link java.util.List} to iterate over */ 4.39 + protected final List<?> list; 4.40 + 4.41 + /** length of array */ 4.42 + protected final long length; 4.43 + 4.44 + /** 4.45 + * Constructor 4.46 + * @param list list to iterate over 4.47 + * @param includeUndefined should undefined elements be included in iteration 4.48 + */ 4.49 + protected JavaListIterator(final List<?> list, final boolean includeUndefined) { 4.50 + super(includeUndefined); 4.51 + this.list = list; 4.52 + this.length = list.size(); 4.53 + } 4.54 + 4.55 + /** 4.56 + * Is the current index still inside the array 4.57 + * @return true if inside the array 4.58 + */ 4.59 + protected boolean indexInArray() { 4.60 + return index < length; 4.61 + } 4.62 + 4.63 + @Override 4.64 + public Object next() { 4.65 + return list.get((int)bumpIndex()); 4.66 + } 4.67 + 4.68 + @Override 4.69 + public long getLength() { 4.70 + return length; 4.71 + } 4.72 + 4.73 + @Override 4.74 + public boolean hasNext() { 4.75 + return indexInArray(); 4.76 + } 4.77 + 4.78 + @Override 4.79 + public void remove() { 4.80 + list.remove(index); 4.81 + } 4.82 +}
5.1 --- a/src/jdk/nashorn/internal/runtime/arrays/MapIterator.java Mon Aug 12 18:00:17 2013 -0300 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,80 +0,0 @@ 5.4 -/* 5.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 5.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 - * 5.8 - * This code is free software; you can redistribute it and/or modify it 5.9 - * under the terms of the GNU General Public License version 2 only, as 5.10 - * published by the Free Software Foundation. Oracle designates this 5.11 - * particular file as subject to the "Classpath" exception as provided 5.12 - * by Oracle in the LICENSE file that accompanied this code. 5.13 - * 5.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 5.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.17 - * version 2 for more details (a copy is included in the LICENSE file that 5.18 - * accompanied this code). 5.19 - * 5.20 - * You should have received a copy of the GNU General Public License version 5.21 - * 2 along with this work; if not, write to the Free Software Foundation, 5.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.23 - * 5.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.25 - * or visit www.oracle.com if you need additional information or have any 5.26 - * questions. 5.27 - */ 5.28 - 5.29 -package jdk.nashorn.internal.runtime.arrays; 5.30 - 5.31 -import java.util.NoSuchElementException; 5.32 -import jdk.nashorn.internal.runtime.JSType; 5.33 -import jdk.nashorn.internal.runtime.ScriptObject; 5.34 - 5.35 -/** 5.36 - * Iterator over a map 5.37 - */ 5.38 -class MapIterator extends ArrayLikeIterator<Object> { 5.39 - 5.40 - protected final ScriptObject obj; 5.41 - private final long length; 5.42 - 5.43 - MapIterator(final ScriptObject obj, final boolean includeUndefined) { 5.44 - super(includeUndefined); 5.45 - this.obj = obj; 5.46 - this.length = JSType.toUint32(obj.getLength()); 5.47 - this.index = 0; 5.48 - } 5.49 - 5.50 - protected boolean indexInArray() { 5.51 - return index < length; 5.52 - } 5.53 - 5.54 - @Override 5.55 - public long getLength() { 5.56 - return length; 5.57 - } 5.58 - 5.59 - @Override 5.60 - public boolean hasNext() { 5.61 - if (length == 0L) { 5.62 - return false; //return empty string if toUint32(length) == 0 5.63 - } 5.64 - 5.65 - while (indexInArray()) { 5.66 - if (obj.has(index) || includeUndefined) { 5.67 - break; 5.68 - } 5.69 - bumpIndex(); 5.70 - } 5.71 - 5.72 - return indexInArray(); 5.73 - } 5.74 - 5.75 - @Override 5.76 - public Object next() { 5.77 - if (indexInArray()) { 5.78 - return obj.get(bumpIndex()); 5.79 - } 5.80 - 5.81 - throw new NoSuchElementException(); 5.82 - } 5.83 -}
6.1 --- a/src/jdk/nashorn/internal/runtime/arrays/ReverseArrayIterator.java Mon Aug 12 18:00:17 2013 -0300 6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 6.3 @@ -1,59 +0,0 @@ 6.4 -/* 6.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 6.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 - * 6.8 - * This code is free software; you can redistribute it and/or modify it 6.9 - * under the terms of the GNU General Public License version 2 only, as 6.10 - * published by the Free Software Foundation. Oracle designates this 6.11 - * particular file as subject to the "Classpath" exception as provided 6.12 - * by Oracle in the LICENSE file that accompanied this code. 6.13 - * 6.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 6.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.17 - * version 2 for more details (a copy is included in the LICENSE file that 6.18 - * accompanied this code). 6.19 - * 6.20 - * You should have received a copy of the GNU General Public License version 6.21 - * 2 along with this work; if not, write to the Free Software Foundation, 6.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.23 - * 6.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.25 - * or visit www.oracle.com if you need additional information or have any 6.26 - * questions. 6.27 - */ 6.28 - 6.29 -package jdk.nashorn.internal.runtime.arrays; 6.30 - 6.31 -import jdk.nashorn.internal.runtime.ScriptObject; 6.32 - 6.33 -/** 6.34 - * Reverse iterator over a NativeArray 6.35 - */ 6.36 -final class ReverseArrayIterator extends ArrayIterator { 6.37 - 6.38 - /** 6.39 - * Constructor 6.40 - * @param array array to iterate over 6.41 - * @param includeUndefined should undefined elements be included in iteration 6.42 - */ 6.43 - public ReverseArrayIterator(final ScriptObject array, final boolean includeUndefined) { 6.44 - super(array, includeUndefined); 6.45 - this.index = array.getArray().length() - 1; 6.46 - } 6.47 - 6.48 - @Override 6.49 - public boolean isReverse() { 6.50 - return true; 6.51 - } 6.52 - 6.53 - @Override 6.54 - protected boolean indexInArray() { 6.55 - return index >= 0; 6.56 - } 6.57 - 6.58 - @Override 6.59 - protected long bumpIndex() { 6.60 - return index--; 6.61 - } 6.62 -}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/ReverseJavaArrayIterator.java Wed Aug 14 20:51:53 2013 +0530 7.3 @@ -0,0 +1,58 @@ 7.4 +/* 7.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 + * 7.8 + * This code is free software; you can redistribute it and/or modify it 7.9 + * under the terms of the GNU General Public License version 2 only, as 7.10 + * published by the Free Software Foundation. Oracle designates this 7.11 + * particular file as subject to the "Classpath" exception as provided 7.12 + * by Oracle in the LICENSE file that accompanied this code. 7.13 + * 7.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 7.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.17 + * version 2 for more details (a copy is included in the LICENSE file that 7.18 + * accompanied this code). 7.19 + * 7.20 + * You should have received a copy of the GNU General Public License version 7.21 + * 2 along with this work; if not, write to the Free Software Foundation, 7.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.23 + * 7.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.25 + * or visit www.oracle.com if you need additional information or have any 7.26 + * questions. 7.27 + */ 7.28 + 7.29 +package jdk.nashorn.internal.runtime.arrays; 7.30 + 7.31 +import java.lang.reflect.Array; 7.32 + 7.33 +/** 7.34 + * Reverse iterator over a array 7.35 + */ 7.36 +final class ReverseJavaArrayIterator extends JavaArrayIterator { 7.37 + /** 7.38 + * Constructor 7.39 + * @param array array to iterate over 7.40 + * @param includeUndefined should undefined elements be included in iteration 7.41 + */ 7.42 + public ReverseJavaArrayIterator(final Object array, final boolean includeUndefined) { 7.43 + super(array, includeUndefined); 7.44 + this.index = Array.getLength(array) - 1; 7.45 + } 7.46 + 7.47 + @Override 7.48 + public boolean isReverse() { 7.49 + return true; 7.50 + } 7.51 + 7.52 + @Override 7.53 + protected boolean indexInArray() { 7.54 + return index >= 0; 7.55 + } 7.56 + 7.57 + @Override 7.58 + protected long bumpIndex() { 7.59 + return index--; 7.60 + } 7.61 +} 7.62 \ No newline at end of file
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/ReverseJavaListIterator.java Wed Aug 14 20:51:53 2013 +0530 8.3 @@ -0,0 +1,58 @@ 8.4 +/* 8.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.7 + * 8.8 + * This code is free software; you can redistribute it and/or modify it 8.9 + * under the terms of the GNU General Public License version 2 only, as 8.10 + * published by the Free Software Foundation. Oracle designates this 8.11 + * particular file as subject to the "Classpath" exception as provided 8.12 + * by Oracle in the LICENSE file that accompanied this code. 8.13 + * 8.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 8.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 8.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 8.17 + * version 2 for more details (a copy is included in the LICENSE file that 8.18 + * accompanied this code). 8.19 + * 8.20 + * You should have received a copy of the GNU General Public License version 8.21 + * 2 along with this work; if not, write to the Free Software Foundation, 8.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 8.23 + * 8.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 8.25 + * or visit www.oracle.com if you need additional information or have any 8.26 + * questions. 8.27 + */ 8.28 + 8.29 +package jdk.nashorn.internal.runtime.arrays; 8.30 + 8.31 +import java.util.List; 8.32 + 8.33 +/** 8.34 + * Reverse iterator over a List 8.35 + */ 8.36 +final class ReverseJavaListIterator extends JavaListIterator { 8.37 + /** 8.38 + * Constructor 8.39 + * @param list list to iterate over 8.40 + * @param includeUndefined should undefined elements be included in iteration 8.41 + */ 8.42 + public ReverseJavaListIterator(final List<?> list, final boolean includeUndefined) { 8.43 + super(list, includeUndefined); 8.44 + this.index = list.size() - 1; 8.45 + } 8.46 + 8.47 + @Override 8.48 + public boolean isReverse() { 8.49 + return true; 8.50 + } 8.51 + 8.52 + @Override 8.53 + protected boolean indexInArray() { 8.54 + return index >= 0; 8.55 + } 8.56 + 8.57 + @Override 8.58 + protected long bumpIndex() { 8.59 + return index--; 8.60 + } 8.61 +}
9.1 --- a/src/jdk/nashorn/internal/runtime/arrays/ReverseMapIterator.java Mon Aug 12 18:00:17 2013 -0300 9.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 9.3 @@ -1,55 +0,0 @@ 9.4 -/* 9.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 9.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 9.7 - * 9.8 - * This code is free software; you can redistribute it and/or modify it 9.9 - * under the terms of the GNU General Public License version 2 only, as 9.10 - * published by the Free Software Foundation. Oracle designates this 9.11 - * particular file as subject to the "Classpath" exception as provided 9.12 - * by Oracle in the LICENSE file that accompanied this code. 9.13 - * 9.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 9.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 9.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 9.17 - * version 2 for more details (a copy is included in the LICENSE file that 9.18 - * accompanied this code). 9.19 - * 9.20 - * You should have received a copy of the GNU General Public License version 9.21 - * 2 along with this work; if not, write to the Free Software Foundation, 9.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 9.23 - * 9.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 9.25 - * or visit www.oracle.com if you need additional information or have any 9.26 - * questions. 9.27 - */ 9.28 - 9.29 -package jdk.nashorn.internal.runtime.arrays; 9.30 - 9.31 -import jdk.nashorn.internal.runtime.JSType; 9.32 -import jdk.nashorn.internal.runtime.ScriptObject; 9.33 - 9.34 -/** 9.35 - * Reverse iterator over a map 9.36 - */ 9.37 -final class ReverseMapIterator extends MapIterator { 9.38 - 9.39 - ReverseMapIterator(final ScriptObject obj, final boolean includeUndefined) { 9.40 - super(obj, includeUndefined); 9.41 - this.index = JSType.toUint32(obj.getLength()) - 1; 9.42 - } 9.43 - 9.44 - @Override 9.45 - public boolean isReverse() { 9.46 - return true; 9.47 - } 9.48 - 9.49 - @Override 9.50 - protected boolean indexInArray() { 9.51 - return index >= 0; 9.52 - } 9.53 - 9.54 - @Override 9.55 - protected long bumpIndex() { 9.56 - return index--; 9.57 - } 9.58 -}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/ReverseScriptArrayIterator.java Wed Aug 14 20:51:53 2013 +0530 10.3 @@ -0,0 +1,59 @@ 10.4 +/* 10.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 10.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 10.7 + * 10.8 + * This code is free software; you can redistribute it and/or modify it 10.9 + * under the terms of the GNU General Public License version 2 only, as 10.10 + * published by the Free Software Foundation. Oracle designates this 10.11 + * particular file as subject to the "Classpath" exception as provided 10.12 + * by Oracle in the LICENSE file that accompanied this code. 10.13 + * 10.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 10.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 10.17 + * version 2 for more details (a copy is included in the LICENSE file that 10.18 + * accompanied this code). 10.19 + * 10.20 + * You should have received a copy of the GNU General Public License version 10.21 + * 2 along with this work; if not, write to the Free Software Foundation, 10.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 10.23 + * 10.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 10.25 + * or visit www.oracle.com if you need additional information or have any 10.26 + * questions. 10.27 + */ 10.28 + 10.29 +package jdk.nashorn.internal.runtime.arrays; 10.30 + 10.31 +import jdk.nashorn.internal.runtime.ScriptObject; 10.32 + 10.33 +/** 10.34 + * Reverse iterator over a NativeArray 10.35 + */ 10.36 +final class ReverseScriptArrayIterator extends ScriptArrayIterator { 10.37 + 10.38 + /** 10.39 + * Constructor 10.40 + * @param array array to iterate over 10.41 + * @param includeUndefined should undefined elements be included in iteration 10.42 + */ 10.43 + public ReverseScriptArrayIterator(final ScriptObject array, final boolean includeUndefined) { 10.44 + super(array, includeUndefined); 10.45 + this.index = array.getArray().length() - 1; 10.46 + } 10.47 + 10.48 + @Override 10.49 + public boolean isReverse() { 10.50 + return true; 10.51 + } 10.52 + 10.53 + @Override 10.54 + protected boolean indexInArray() { 10.55 + return index >= 0; 10.56 + } 10.57 + 10.58 + @Override 10.59 + protected long bumpIndex() { 10.60 + return index--; 10.61 + } 10.62 +}
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/ReverseScriptObjectIterator.java Wed Aug 14 20:51:53 2013 +0530 11.3 @@ -0,0 +1,55 @@ 11.4 +/* 11.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 11.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 11.7 + * 11.8 + * This code is free software; you can redistribute it and/or modify it 11.9 + * under the terms of the GNU General Public License version 2 only, as 11.10 + * published by the Free Software Foundation. Oracle designates this 11.11 + * particular file as subject to the "Classpath" exception as provided 11.12 + * by Oracle in the LICENSE file that accompanied this code. 11.13 + * 11.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 11.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11.17 + * version 2 for more details (a copy is included in the LICENSE file that 11.18 + * accompanied this code). 11.19 + * 11.20 + * You should have received a copy of the GNU General Public License version 11.21 + * 2 along with this work; if not, write to the Free Software Foundation, 11.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 11.23 + * 11.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 11.25 + * or visit www.oracle.com if you need additional information or have any 11.26 + * questions. 11.27 + */ 11.28 + 11.29 +package jdk.nashorn.internal.runtime.arrays; 11.30 + 11.31 +import jdk.nashorn.internal.runtime.JSType; 11.32 +import jdk.nashorn.internal.runtime.ScriptObject; 11.33 + 11.34 +/** 11.35 + * Reverse iterator over a map 11.36 + */ 11.37 +final class ReverseScriptObjectIterator extends ScriptObjectIterator { 11.38 + 11.39 + ReverseScriptObjectIterator(final ScriptObject obj, final boolean includeUndefined) { 11.40 + super(obj, includeUndefined); 11.41 + this.index = JSType.toUint32(obj.getLength()) - 1; 11.42 + } 11.43 + 11.44 + @Override 11.45 + public boolean isReverse() { 11.46 + return true; 11.47 + } 11.48 + 11.49 + @Override 11.50 + protected boolean indexInArray() { 11.51 + return index >= 0; 11.52 + } 11.53 + 11.54 + @Override 11.55 + protected long bumpIndex() { 11.56 + return index--; 11.57 + } 11.58 +}
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/ScriptArrayIterator.java Wed Aug 14 20:51:53 2013 +0530 12.3 @@ -0,0 +1,88 @@ 12.4 +/* 12.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 12.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 12.7 + * 12.8 + * This code is free software; you can redistribute it and/or modify it 12.9 + * under the terms of the GNU General Public License version 2 only, as 12.10 + * published by the Free Software Foundation. Oracle designates this 12.11 + * particular file as subject to the "Classpath" exception as provided 12.12 + * by Oracle in the LICENSE file that accompanied this code. 12.13 + * 12.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 12.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12.17 + * version 2 for more details (a copy is included in the LICENSE file that 12.18 + * accompanied this code). 12.19 + * 12.20 + * You should have received a copy of the GNU General Public License version 12.21 + * 2 along with this work; if not, write to the Free Software Foundation, 12.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 12.23 + * 12.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 12.25 + * or visit www.oracle.com if you need additional information or have any 12.26 + * questions. 12.27 + */ 12.28 + 12.29 +package jdk.nashorn.internal.runtime.arrays; 12.30 + 12.31 +import jdk.nashorn.internal.runtime.ScriptObject; 12.32 + 12.33 +/** 12.34 + * Iterator over a NativeArray 12.35 + */ 12.36 +class ScriptArrayIterator extends ArrayLikeIterator<Object> { 12.37 + 12.38 + /** Array {@link ScriptObject} to iterate over */ 12.39 + protected final ScriptObject array; 12.40 + 12.41 + /** length of array */ 12.42 + protected final long length; 12.43 + 12.44 + /** 12.45 + * Constructor 12.46 + * @param array array to iterate over 12.47 + * @param includeUndefined should undefined elements be included in iteration 12.48 + */ 12.49 + protected ScriptArrayIterator(final ScriptObject array, final boolean includeUndefined) { 12.50 + super(includeUndefined); 12.51 + this.array = array; 12.52 + this.length = array.getArray().length(); 12.53 + } 12.54 + 12.55 + /** 12.56 + * Is the current index still inside the array 12.57 + * @return true if inside the array 12.58 + */ 12.59 + protected boolean indexInArray() { 12.60 + return index < length; 12.61 + } 12.62 + 12.63 + @Override 12.64 + public Object next() { 12.65 + return array.get(bumpIndex()); 12.66 + } 12.67 + 12.68 + @Override 12.69 + public long getLength() { 12.70 + return length; 12.71 + } 12.72 + 12.73 + @Override 12.74 + public boolean hasNext() { 12.75 + if (!includeUndefined) { 12.76 + while (indexInArray()) { 12.77 + if (array.has(index)) { 12.78 + break; 12.79 + } 12.80 + bumpIndex(); 12.81 + } 12.82 + } 12.83 + 12.84 + return indexInArray(); 12.85 + } 12.86 + 12.87 + @Override 12.88 + public void remove() { 12.89 + array.delete(index, false); 12.90 + } 12.91 +}
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/ScriptObjectIterator.java Wed Aug 14 20:51:53 2013 +0530 13.3 @@ -0,0 +1,80 @@ 13.4 +/* 13.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 13.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 13.7 + * 13.8 + * This code is free software; you can redistribute it and/or modify it 13.9 + * under the terms of the GNU General Public License version 2 only, as 13.10 + * published by the Free Software Foundation. Oracle designates this 13.11 + * particular file as subject to the "Classpath" exception as provided 13.12 + * by Oracle in the LICENSE file that accompanied this code. 13.13 + * 13.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 13.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13.17 + * version 2 for more details (a copy is included in the LICENSE file that 13.18 + * accompanied this code). 13.19 + * 13.20 + * You should have received a copy of the GNU General Public License version 13.21 + * 2 along with this work; if not, write to the Free Software Foundation, 13.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 13.23 + * 13.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 13.25 + * or visit www.oracle.com if you need additional information or have any 13.26 + * questions. 13.27 + */ 13.28 + 13.29 +package jdk.nashorn.internal.runtime.arrays; 13.30 + 13.31 +import java.util.NoSuchElementException; 13.32 +import jdk.nashorn.internal.runtime.JSType; 13.33 +import jdk.nashorn.internal.runtime.ScriptObject; 13.34 + 13.35 +/** 13.36 + * Iterator over a map 13.37 + */ 13.38 +class ScriptObjectIterator extends ArrayLikeIterator<Object> { 13.39 + 13.40 + protected final ScriptObject obj; 13.41 + private final long length; 13.42 + 13.43 + ScriptObjectIterator(final ScriptObject obj, final boolean includeUndefined) { 13.44 + super(includeUndefined); 13.45 + this.obj = obj; 13.46 + this.length = JSType.toUint32(obj.getLength()); 13.47 + this.index = 0; 13.48 + } 13.49 + 13.50 + protected boolean indexInArray() { 13.51 + return index < length; 13.52 + } 13.53 + 13.54 + @Override 13.55 + public long getLength() { 13.56 + return length; 13.57 + } 13.58 + 13.59 + @Override 13.60 + public boolean hasNext() { 13.61 + if (length == 0L) { 13.62 + return false; //return empty string if toUint32(length) == 0 13.63 + } 13.64 + 13.65 + while (indexInArray()) { 13.66 + if (obj.has(index) || includeUndefined) { 13.67 + break; 13.68 + } 13.69 + bumpIndex(); 13.70 + } 13.71 + 13.72 + return indexInArray(); 13.73 + } 13.74 + 13.75 + @Override 13.76 + public Object next() { 13.77 + if (indexInArray()) { 13.78 + return obj.get(bumpIndex()); 13.79 + } 13.80 + 13.81 + throw new NoSuchElementException(); 13.82 + } 13.83 +}
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/test/script/basic/JDK-8023026.js Wed Aug 14 20:51:53 2013 +0530 14.3 @@ -0,0 +1,71 @@ 14.4 +/* 14.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 14.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 14.7 + * 14.8 + * This code is free software; you can redistribute it and/or modify it 14.9 + * under the terms of the GNU General Public License version 2 only, as 14.10 + * published by the Free Software Foundation. 14.11 + * 14.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 14.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14.15 + * version 2 for more details (a copy is included in the LICENSE file that 14.16 + * accompanied this code). 14.17 + * 14.18 + * You should have received a copy of the GNU General Public License version 14.19 + * 2 along with this work; if not, write to the Free Software Foundation, 14.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 14.21 + * 14.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 14.23 + * or visit www.oracle.com if you need additional information or have any 14.24 + * questions. 14.25 + */ 14.26 + 14.27 +/** 14.28 + * JDK-8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists 14.29 + * 14.30 + * @test 14.31 + * @run 14.32 + */ 14.33 + 14.34 +function checkIterations(obj) { 14.35 + if (typeof obj.getClass == 'function') { 14.36 + print("iterating on an object of " + obj.getClass()); 14.37 + } else { 14.38 + print("iterating on " + String(obj)); 14.39 + } 14.40 + 14.41 + Array.prototype.forEach.call(obj, 14.42 + function(x) { print("forEach " + x); }); 14.43 + 14.44 + print("left sum " + Array.prototype.reduce.call(obj, 14.45 + function(x, y) { print("reduce", x, y); return x + y; })); 14.46 + 14.47 + print("right sum " + Array.prototype.reduceRight.call(obj, 14.48 + function(x, y) { print("reduceRight", x, y); return x + y; })); 14.49 + 14.50 + print("squared " + Array.prototype.map.call(obj, 14.51 + function(x) x*x)); 14.52 +} 14.53 + 14.54 +var array = new (Java.type("[I"))(4); 14.55 +for (var i in array) { 14.56 + array[i] = i; 14.57 +} 14.58 + 14.59 +checkIterations(array); 14.60 + 14.61 +var list = new java.util.ArrayList(); 14.62 +list.add(1); 14.63 +list.add(3); 14.64 +list.add(5); 14.65 +list.add(7); 14.66 + 14.67 +checkIterations(list); 14.68 + 14.69 +var mirror = loadWithNewGlobal({ 14.70 + name: "test", 14.71 + script: "[2, 4, 6, 8]" 14.72 +}); 14.73 + 14.74 +checkIterations(mirror);
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/test/script/basic/JDK-8023026.js.EXPECTED Wed Aug 14 20:51:53 2013 +0530 15.3 @@ -0,0 +1,42 @@ 15.4 +iterating on an object of class [I 15.5 +forEach 0 15.6 +forEach 1 15.7 +forEach 2 15.8 +forEach 3 15.9 +reduce 0 1 15.10 +reduce 1 2 15.11 +reduce 3 3 15.12 +left sum 6 15.13 +reduceRight 3 2 15.14 +reduceRight 5 1 15.15 +reduceRight 6 0 15.16 +right sum 6 15.17 +squared 0,1,4,9 15.18 +iterating on an object of class java.util.ArrayList 15.19 +forEach 1 15.20 +forEach 3 15.21 +forEach 5 15.22 +forEach 7 15.23 +reduce 1 3 15.24 +reduce 4 5 15.25 +reduce 9 7 15.26 +left sum 16 15.27 +reduceRight 7 5 15.28 +reduceRight 12 3 15.29 +reduceRight 15 1 15.30 +right sum 16 15.31 +squared 1,9,25,49 15.32 +iterating on [object Array] 15.33 +forEach 2 15.34 +forEach 4 15.35 +forEach 6 15.36 +forEach 8 15.37 +reduce 2 4 15.38 +reduce 6 6 15.39 +reduce 12 8 15.40 +left sum 20 15.41 +reduceRight 8 6 15.42 +reduceRight 14 4 15.43 +reduceRight 18 2 15.44 +right sum 20 15.45 +squared 4,16,36,64