mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 02:54:35 +02:00
8156665: ES6 for..of should work on Java Iterables and Java arrays
Reviewed-by: attila, hannesw
This commit is contained in:
parent
dd927b90d5
commit
9f4a84d825
3 changed files with 79 additions and 16 deletions
|
@ -313,18 +313,8 @@ public final class ScriptRuntime {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,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) {
|
||||
return ((JSObject)obj).values().iterator();
|
||||
}
|
||||
|
||||
if (obj instanceof Map) {
|
||||
return ((Map<?,?>)obj).values().iterator();
|
||||
final Iterator<?> itr = iteratorForJavaArrayOrList(obj);
|
||||
if (itr != null) {
|
||||
return itr;
|
||||
}
|
||||
|
||||
if (obj instanceof Iterable) {
|
||||
return ((Iterable<?>)obj).iterator();
|
||||
if (obj instanceof Map) {
|
||||
return ((Map<?,?>)obj).values().iterator();
|
||||
}
|
||||
|
||||
final Object wrapped = Global.instance().wrapAsObject(obj);
|
||||
|
@ -380,6 +390,14 @@ public final class ScriptRuntime {
|
|||
* @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);
|
||||
|
||||
|
|
40
nashorn/test/script/basic/es6/JDK-8156665.js
Normal file
40
nashorn/test/script/basic/es6/JDK-8156665.js
Normal 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);
|
5
nashorn/test/script/basic/es6/JDK-8156665.js.EXPECTED
Normal file
5
nashorn/test/script/basic/es6/JDK-8156665.js.EXPECTED
Normal file
|
@ -0,0 +1,5 @@
|
|||
a
|
||||
b
|
||||
c
|
||||
hello
|
||||
world
|
Loading…
Add table
Add a link
Reference in a new issue