blob: 96fdaa71929336323e90ba256b69e5c65c33d83c [file] [log] [blame]
sundard2a62ea2013-09-16 15:08:36 +05301/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
attila6f816242014-06-04 13:08:57 +02004 *
sundard2a62ea2013-09-16 15:08:36 +05305 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
attila6f816242014-06-04 13:08:57 +02008 *
sundard2a62ea2013-09-16 15:08:36 +05309 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
attila6f816242014-06-04 13:08:57 +020014 *
sundard2a62ea2013-09-16 15:08:36 +053015 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
attila6f816242014-06-04 13:08:57 +020018 *
sundard2a62ea2013-09-16 15:08:36 +053019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * JDK-8024847: Java.to should accept mirror and external JSObjects as array-like objects as well
26 *
27 * @test
28 * @run
29 */
30
31var global = loadWithNewGlobal({ name: "test", script:"this" });
32var arr = new global.Array(2, 4, 6, 8);
33var jarr = Java.to(arr, "int[]");
34for (var i in jarr) {
35 print(jarr[i]);
36}
37
38arr = null;
39jarr = null;
40
41// external JSObjects
42var JSObject = Java.type("jdk.nashorn.api.scripting.JSObject");
43var arr = new JSObject() {
44 getMember: function(name) {
45 return name == "length"? 4 : undefined;
46 },
47
48 hasMember: function(name) {
49 return name == "length";
50 },
51
52 getSlot: function(idx) {
53 return idx*idx;
54 },
55
56 hasSlot: function(idx) {
57 return true;
58 }
59};
60
61var jarr = Java.to(arr, "int[]");
62for (var i in jarr) {
63 print(jarr[i]);
64}
65
66arr = null;
67jarr = null;
68
69// List conversion
70var arr = global.Array("hello", "world");
71var jlist = Java.to(arr, java.util.List);
72print(jlist instanceof java.util.List);
73print(jlist);
74
75arr = null;
76jlist = null;
77
78// external JSObject
79var __array__ = [ "nashorn", "js" ];
80
81var obj = new JSObject() {
attila6f816242014-06-04 13:08:57 +020082
sundard2a62ea2013-09-16 15:08:36 +053083 hasMember: function(name) {
84 return name in __array__;
85 },
86
87 hasSlot: function(idx) {
88 return idx in __array__;
89 },
90
91 getMember: function(name) {
92 return __array__[name];
93 },
94
95 getSlot: function(idx) {
96 return __array__[idx];
97 }
98}
99
100var jlist = Java.to(obj, java.util.List);
101print(jlist instanceof java.util.List);
102print(jlist);
sundar7ae98842013-10-23 17:30:13 +0530103
104var obj = new JSObject() {
105 toNumber: function() { return 42; }
106};
107
108print(32 + obj);