8156665: ES6 for..of should work on Java Iterables and Java arrays

Reviewed-by: attila, hannesw
This commit is contained in:
Athijegannathan Sundararajan 2016-05-10 22:26:51 +05:30
parent dd927b90d5
commit 9f4a84d825
3 changed files with 79 additions and 16 deletions

View file

@ -313,18 +313,8 @@ public final class ScriptRuntime {
} }
} }
/** // value Iterator for important Java objects - arrays, maps, iterables.
* Returns an iterator over property values used in the {@code for each...in} statement. Aside from built-in JS private static Iterator<?> iteratorForJavaArrayOrList(final Object obj) {
* 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 != null && obj.getClass().isArray()) { if (obj != null && obj.getClass().isArray()) {
final Object array = obj; final Object array = obj;
final int length = Array.getLength(obj); final int length = Array.getLength(obj);
@ -352,16 +342,36 @@ public final class ScriptRuntime {
}; };
} }
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) { if (obj instanceof JSObject) {
return ((JSObject)obj).values().iterator(); return ((JSObject)obj).values().iterator();
} }
if (obj instanceof Map) { final Iterator<?> itr = iteratorForJavaArrayOrList(obj);
return ((Map<?,?>)obj).values().iterator(); if (itr != null) {
return itr;
} }
if (obj instanceof Iterable) { if (obj instanceof Map) {
return ((Iterable<?>)obj).iterator(); return ((Map<?,?>)obj).values().iterator();
} }
final Object wrapped = Global.instance().wrapAsObject(obj); final Object wrapped = Global.instance().wrapAsObject(obj);
@ -380,6 +390,14 @@ public final class ScriptRuntime {
* @return iterator based on the ECMA 6 Iterator interface. * @return iterator based on the ECMA 6 Iterator interface.
*/ */
public static Iterator<?> toES6Iterator(final Object obj) { 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 Global global = Global.instance();
final Object iterator = AbstractIterator.getIterator(Global.toObject(obj), global); final Object iterator = AbstractIterator.getIterator(Global.toObject(obj), global);

View file

@ -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);

View file

@ -0,0 +1,5 @@
a
b
c
hello
world