author | hannesw |
Wed, 25 Jun 2014 14:36:24 +0200 | |
changeset 25243 | 7a1edca6ce94 |
parent 24778 | 2ff5d7041566 |
child 29133 | 1cd7d8af99ba |
permissions | -rw-r--r-- |
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
1 |
/* |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
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:
21453
diff
changeset
|
4 |
* |
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
21453
diff
changeset
|
8 |
* |
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
13 |
* accompanied this code). |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
21453
diff
changeset
|
14 |
* |
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
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:
21453
diff
changeset
|
18 |
* |
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
21 |
* questions. |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
22 |
*/ |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
23 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
24 |
/** |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
25 |
* JDK-8024847: Java.to should accept mirror and external JSObjects as array-like objects as well |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
26 |
* |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
27 |
* @test |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
28 |
* @run |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
29 |
*/ |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
30 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
31 |
var global = loadWithNewGlobal({ name: "test", script:"this" }); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
32 |
var arr = new global.Array(2, 4, 6, 8); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
33 |
var jarr = Java.to(arr, "int[]"); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
34 |
for (var i in jarr) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
35 |
print(jarr[i]); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
36 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
37 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
38 |
arr = null; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
39 |
jarr = null; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
40 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
41 |
// external JSObjects |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
42 |
var JSObject = Java.type("jdk.nashorn.api.scripting.JSObject"); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
43 |
var arr = new JSObject() { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
44 |
getMember: function(name) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
45 |
return name == "length"? 4 : undefined; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
46 |
}, |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
47 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
48 |
hasMember: function(name) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
49 |
return name == "length"; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
50 |
}, |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
51 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
52 |
getSlot: function(idx) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
53 |
return idx*idx; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
54 |
}, |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
55 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
56 |
hasSlot: function(idx) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
57 |
return true; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
58 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
59 |
}; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
60 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
61 |
var jarr = Java.to(arr, "int[]"); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
62 |
for (var i in jarr) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
63 |
print(jarr[i]); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
64 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
65 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
66 |
arr = null; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
67 |
jarr = null; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
68 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
69 |
// List conversion |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
70 |
var arr = global.Array("hello", "world"); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
71 |
var jlist = Java.to(arr, java.util.List); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
72 |
print(jlist instanceof java.util.List); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
73 |
print(jlist); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
74 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
75 |
arr = null; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
76 |
jlist = null; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
77 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
78 |
// external JSObject |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
79 |
var __array__ = [ "nashorn", "js" ]; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
80 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
81 |
var obj = new JSObject() { |
24778
2ff5d7041566
8044638: Tidy up Nashorn codebase for code standards
attila
parents:
21453
diff
changeset
|
82 |
|
19897
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
83 |
hasMember: function(name) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
84 |
return name in __array__; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
85 |
}, |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
86 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
87 |
hasSlot: function(idx) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
88 |
return idx in __array__; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
89 |
}, |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
90 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
91 |
getMember: function(name) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
92 |
return __array__[name]; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
93 |
}, |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
94 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
95 |
getSlot: function(idx) { |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
96 |
return __array__[idx]; |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
97 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
98 |
} |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
99 |
|
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
100 |
var jlist = Java.to(obj, java.util.List); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
101 |
print(jlist instanceof java.util.List); |
4235a28bb5e8
8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
sundar
parents:
diff
changeset
|
102 |
print(jlist); |
21453
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
19897
diff
changeset
|
103 |
|
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
19897
diff
changeset
|
104 |
var obj = new JSObject() { |
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
19897
diff
changeset
|
105 |
toNumber: function() { return 42; } |
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
19897
diff
changeset
|
106 |
}; |
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
19897
diff
changeset
|
107 |
|
b48953eede53
8027128: jdk.nashorn.api.scripting.JSObject should be an interface
sundar
parents:
19897
diff
changeset
|
108 |
print(32 + obj); |