# HG changeset patch # User sundar # Date 1462899411 -19800 # Node ID 21f72dc38a9613cb96ecd036f85639056833802b # Parent 7c04fcb12bd4a31570a238e663fa846dfa5ec3b8 8156665: ES6 for..of should work on Java Iterables and Java arrays Reviewed-by: attila, hannesw diff -r 7c04fcb12bd4 -r 21f72dc38a96 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptRuntime.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptRuntime.java Wed Jul 05 21:41:01 2017 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptRuntime.java Tue May 10 22:26:51 2016 +0530 @@ -313,18 +313,8 @@ } } - /** - * Returns an iterator over property values used in the {@code for each...in} statement. Aside from built-in JS - * objects, it also operates on Java arrays, any {@link Iterable}, as well as on {@link Map} objects, iterating over - * map values. - * @param obj object to iterate on. - * @return iterator over the object's property values. - */ - public static Iterator toValueIterator(final Object obj) { - if (obj instanceof ScriptObject) { - return ((ScriptObject)obj).valueIterator(); - } - + // value Iterator for important Java objects - arrays, maps, iterables. + private static Iterator iteratorForJavaArrayOrList(final Object obj) { if (obj != null && obj.getClass().isArray()) { final Object array = obj; final int length = Array.getLength(obj); @@ -352,18 +342,38 @@ }; } + if (obj instanceof Iterable) { + return ((Iterable)obj).iterator(); + } + + return null; + } + + /** + * Returns an iterator over property values used in the {@code for each...in} statement. Aside from built-in JS + * objects, it also operates on Java arrays, any {@link Iterable}, as well as on {@link Map} objects, iterating over + * map values. + * @param obj object to iterate on. + * @return iterator over the object's property values. + */ + public static Iterator toValueIterator(final Object obj) { + if (obj instanceof ScriptObject) { + return ((ScriptObject)obj).valueIterator(); + } + if (obj instanceof JSObject) { return ((JSObject)obj).values().iterator(); } + final Iterator itr = iteratorForJavaArrayOrList(obj); + if (itr != null) { + return itr; + } + if (obj instanceof Map) { return ((Map)obj).values().iterator(); } - if (obj instanceof Iterable) { - return ((Iterable)obj).iterator(); - } - final Object wrapped = Global.instance().wrapAsObject(obj); if (wrapped instanceof ScriptObject) { return ((ScriptObject)wrapped).valueIterator(); @@ -380,6 +390,14 @@ * @return iterator based on the ECMA 6 Iterator interface. */ public static Iterator toES6Iterator(final Object obj) { + // if not a ScriptObject, try convenience iterator for Java objects! + if (!(obj instanceof ScriptObject)) { + final Iterator itr = iteratorForJavaArrayOrList(obj); + if (itr != null) { + return itr; + } + } + final Global global = Global.instance(); final Object iterator = AbstractIterator.getIterator(Global.toObject(obj), global); diff -r 7c04fcb12bd4 -r 21f72dc38a96 nashorn/test/script/basic/es6/JDK-8156665.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nashorn/test/script/basic/es6/JDK-8156665.js Tue May 10 22:26:51 2016 +0530 @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2016, 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-8156665: ES6 for..of should work on Java Iterables and Java arrays + * + * @test + * @run + * @option --language=es6 + */ + +var ca = new (Java.type("char[]"))(3); +ca[0] = 'a', ca[1] = 'b', ca[2] = 'c'; +for (i of ca) print(i); + +var al = new java.util.ArrayList(); +al.add("hello"); +al.add("world"); + +for (i of al) print(i); diff -r 7c04fcb12bd4 -r 21f72dc38a96 nashorn/test/script/basic/es6/JDK-8156665.js.EXPECTED --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nashorn/test/script/basic/es6/JDK-8156665.js.EXPECTED Tue May 10 22:26:51 2016 +0530 @@ -0,0 +1,5 @@ +a +b +c +hello +world