sundar@502: /* sundar@502: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. sundar@502: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. sundar@502: * sundar@502: * This code is free software; you can redistribute it and/or modify it sundar@502: * under the terms of the GNU General Public License version 2 only, as sundar@502: * published by the Free Software Foundation. sundar@502: * sundar@502: * This code is distributed in the hope that it will be useful, but WITHOUT sundar@502: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or sundar@502: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License sundar@502: * version 2 for more details (a copy is included in the LICENSE file that sundar@502: * accompanied this code). sundar@502: * sundar@502: * You should have received a copy of the GNU General Public License version sundar@502: * 2 along with this work; if not, write to the Free Software Foundation, sundar@502: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. sundar@502: * sundar@502: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA sundar@502: * or visit www.oracle.com if you need additional information or have any sundar@502: * questions. sundar@502: */ sundar@502: sundar@502: /** sundar@502: * JDK-8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists sundar@502: * sundar@502: * @test sundar@502: * @run sundar@502: */ sundar@502: sundar@502: function checkIterations(obj) { sundar@502: if (typeof obj.getClass == 'function') { sundar@502: print("iterating on an object of " + obj.getClass()); sundar@502: } else { sundar@502: print("iterating on " + String(obj)); sundar@502: } sundar@502: sundar@502: Array.prototype.forEach.call(obj, sundar@502: function(x) { print("forEach " + x); }); sundar@502: sundar@502: print("left sum " + Array.prototype.reduce.call(obj, sundar@502: function(x, y) { print("reduce", x, y); return x + y; })); sundar@502: sundar@502: print("right sum " + Array.prototype.reduceRight.call(obj, sundar@502: function(x, y) { print("reduceRight", x, y); return x + y; })); sundar@502: sundar@502: print("squared " + Array.prototype.map.call(obj, sundar@502: function(x) x*x)); sundar@502: } sundar@502: sundar@590: var array = new (Java.type("int[]"))(4); sundar@502: for (var i in array) { sundar@502: array[i] = i; sundar@502: } sundar@502: sundar@502: checkIterations(array); sundar@502: sundar@502: var list = new java.util.ArrayList(); sundar@502: list.add(1); sundar@502: list.add(3); sundar@502: list.add(5); sundar@502: list.add(7); sundar@502: sundar@502: checkIterations(list); sundar@502: sundar@502: var mirror = loadWithNewGlobal({ sundar@502: name: "test", sundar@502: script: "[2, 4, 6, 8]" sundar@502: }); sundar@502: sundar@502: checkIterations(mirror);