1 /*
   2  * Copyright (c) 2014, 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-8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
  26  *
  27  * @test
  28  * @option -scripting
  29  * @run
  30  */
  31 
  32 function main() {
  33     var JSObject;
  34     try {
  35         JSObject = Java.type("netscape.javascript.JSObject");
  36     } catch (e) {
  37         if (e instanceof java.lang.ClassNotFoundException) {
  38             // pass vacuously by emitting the .EXPECTED file content
  39             var str = readFully(__DIR__ + "JDK-8055796_2.js.EXPECTED");
  40             print(str.substring(0, str.length - 1));
  41             return;
  42         } else {
  43             fail("unexpected exception on JSObject", e);
  44         }
  45     }
  46     test(JSObject);
  47 }
  48 
  49 function test(JSObject) {
  50     var bjsobj = new (Java.extend(JSObject))() {
  51         getMember: function(name) {
  52             if (name == "func") {
  53                 return function(arg) {
  54                     print("func called with " + arg);
  55                 }
  56             }
  57             return name.toUpperCase();
  58         },
  59 
  60         getSlot: function(index) {
  61             return index*index;
  62         },
  63 
  64         setMember: function(name, value) {
  65             print(name + " set to " + value);
  66         },
  67 
  68         setSlot: function(index, value) {
  69             print("[" + index + "] set to " + value);
  70         }
  71     };
  72 
  73     print("getMember('foo') =", bjsobj['getMember(String)']('foo'));
  74     print("getSlot(6) =", bjsobj['getSlot(int)'](6));
  75     bjsobj['setMember(String, Object)']('bar', 'hello');
  76     bjsobj['setSlot(int, Object)'](10, 42);
  77 }
  78 
  79 main();