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