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 
  31 var global = loadWithNewGlobal({ name: "test", script:"this" });
  32 var arr = new global.Array(2, 4, 6, 8);
  33 var jarr = Java.to(arr, "int[]");
  34 for (var i in jarr) {
  35     print(jarr[i]);
  36 }
  37 
  38 arr = null;
  39 jarr = null;
  40 
  41 // external JSObjects
  42 var JSObject = Java.type("jdk.nashorn.api.scripting.JSObject");
  43 var 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 
  61 var jarr = Java.to(arr, "int[]");
  62 for (var i in jarr) {
  63     print(jarr[i]);
  64 }
  65 
  66 arr = null;
  67 jarr = null;
  68 
  69 // List conversion
  70 var arr = global.Array("hello", "world");
  71 var jlist = Java.to(arr, java.util.List);
  72 print(jlist instanceof java.util.List);
  73 print(jlist);
  74 
  75 arr = null;
  76 jlist = null;
  77 
  78 // external JSObject
  79 var __array__ =  [ "nashorn", "js" ];
  80 
  81 var 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 
 100 var jlist = Java.to(obj, java.util.List);
 101 print(jlist instanceof java.util.List);
 102 print(jlist);
 103 
 104 var 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 
 119 print(32 + obj);