1/*
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.
4 *
5 * 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.
8 *
9 * 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).
14 *
15 * 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.
18 *
19 * 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() {
82
83    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);
103
104var obj = new JSObject() {
105    getMember: function(name) {
106        if (name == "valueOf") {
107            return new JSObject() {
108                isFunction: function() {
109                    return true;
110                },
111                call: function(thiz) {
112                    return 42;
113                }
114            };
115        }
116    }
117};
118
119print(32 + obj);
120