Mon, 07 Dec 2015 13:15:47 -0800
Merge
sundar@502 | 1 | /* |
sundar@502 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
sundar@502 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
attila@962 | 4 | * |
sundar@502 | 5 | * This code is free software; you can redistribute it and/or modify it |
sundar@502 | 6 | * under the terms of the GNU General Public License version 2 only, as |
sundar@502 | 7 | * published by the Free Software Foundation. |
attila@962 | 8 | * |
sundar@502 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
sundar@502 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
sundar@502 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
sundar@502 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
sundar@502 | 13 | * accompanied this code). |
attila@962 | 14 | * |
sundar@502 | 15 | * You should have received a copy of the GNU General Public License version |
sundar@502 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
sundar@502 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
attila@962 | 18 | * |
sundar@502 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
sundar@502 | 20 | * or visit www.oracle.com if you need additional information or have any |
sundar@502 | 21 | * questions. |
sundar@502 | 22 | */ |
sundar@502 | 23 | |
sundar@502 | 24 | /** |
sundar@502 | 25 | * JDK-8023026: Array.prototype iterator functions like forEach, reduce should work for Java arrays, lists |
sundar@502 | 26 | * |
sundar@502 | 27 | * @test |
sundar@502 | 28 | * @run |
sundar@502 | 29 | */ |
sundar@502 | 30 | |
sundar@502 | 31 | function checkIterations(obj) { |
sundar@502 | 32 | if (typeof obj.getClass == 'function') { |
sundar@502 | 33 | print("iterating on an object of " + obj.getClass()); |
sundar@502 | 34 | } else { |
sundar@502 | 35 | print("iterating on " + String(obj)); |
sundar@502 | 36 | } |
sundar@502 | 37 | |
sundar@502 | 38 | Array.prototype.forEach.call(obj, |
sundar@502 | 39 | function(x) { print("forEach " + x); }); |
sundar@502 | 40 | |
sundar@502 | 41 | print("left sum " + Array.prototype.reduce.call(obj, |
sundar@502 | 42 | function(x, y) { print("reduce", x, y); return x + y; })); |
sundar@502 | 43 | |
sundar@502 | 44 | print("right sum " + Array.prototype.reduceRight.call(obj, |
sundar@502 | 45 | function(x, y) { print("reduceRight", x, y); return x + y; })); |
sundar@502 | 46 | |
sundar@502 | 47 | print("squared " + Array.prototype.map.call(obj, |
sundar@502 | 48 | function(x) x*x)); |
sundar@502 | 49 | } |
sundar@502 | 50 | |
sundar@590 | 51 | var array = new (Java.type("int[]"))(4); |
sundar@502 | 52 | for (var i in array) { |
sundar@502 | 53 | array[i] = i; |
sundar@502 | 54 | } |
sundar@502 | 55 | |
sundar@502 | 56 | checkIterations(array); |
sundar@502 | 57 | |
sundar@502 | 58 | var list = new java.util.ArrayList(); |
sundar@502 | 59 | list.add(1); |
sundar@502 | 60 | list.add(3); |
sundar@502 | 61 | list.add(5); |
sundar@502 | 62 | list.add(7); |
sundar@502 | 63 | |
sundar@502 | 64 | checkIterations(list); |
sundar@502 | 65 | |
sundar@502 | 66 | var mirror = loadWithNewGlobal({ |
sundar@502 | 67 | name: "test", |
sundar@502 | 68 | script: "[2, 4, 6, 8]" |
sundar@502 | 69 | }); |
sundar@502 | 70 | |
sundar@502 | 71 | checkIterations(mirror); |