Fri, 22 Aug 2014 15:47:28 +0530
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
Reviewed-by: attila, hannesw
sundar@967 | 1 | /* |
sundar@967 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
sundar@967 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
sundar@967 | 4 | * |
sundar@967 | 5 | * This code is free software; you can redistribute it and/or modify it |
sundar@967 | 6 | * under the terms of the GNU General Public License version 2 only, as |
sundar@967 | 7 | * published by the Free Software Foundation. |
sundar@967 | 8 | * |
sundar@967 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
sundar@967 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
sundar@967 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
sundar@967 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
sundar@967 | 13 | * accompanied this code). |
sundar@967 | 14 | * |
sundar@967 | 15 | * You should have received a copy of the GNU General Public License version |
sundar@967 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
sundar@967 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
sundar@967 | 18 | * |
sundar@967 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
sundar@967 | 20 | * or visit www.oracle.com if you need additional information or have any |
sundar@967 | 21 | * questions. |
sundar@967 | 22 | */ |
sundar@967 | 23 | |
sundar@967 | 24 | /** |
sundar@967 | 25 | * JDK-8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly |
sundar@967 | 26 | * |
sundar@967 | 27 | * @test |
sundar@967 | 28 | * @run |
sundar@967 | 29 | */ |
sundar@967 | 30 | |
sundar@967 | 31 | var m = new javax.script.ScriptEngineManager(); |
sundar@967 | 32 | var e = m.getEngineByName("nashorn"); |
sundar@967 | 33 | var jsobj = e.eval("({ foo: 33, valueOf: function() 42 })"); |
sundar@967 | 34 | |
sundar@967 | 35 | print("foo =", jsobj['getMember(java.lang.String)']("foo")); |
sundar@967 | 36 | print("eval =", jsobj['eval(String)']("this + 44")); |
sundar@967 | 37 | print("valueOf function? =", (jsobj.valueOf)['isFunction()']()); |
sundar@967 | 38 | |
sundar@967 | 39 | var JSObject = Java.type("netscape.javascript.JSObject"); |
sundar@967 | 40 | var bjsobj = new (Java.extend(JSObject))() { |
sundar@967 | 41 | getMember: function(name) { |
sundar@967 | 42 | if (name == "func") { |
sundar@967 | 43 | return function(arg) { |
sundar@967 | 44 | print("func called with " + arg); |
sundar@967 | 45 | } |
sundar@967 | 46 | } |
sundar@967 | 47 | return name.toUpperCase(); |
sundar@967 | 48 | }, |
sundar@967 | 49 | |
sundar@967 | 50 | getSlot: function(index) { |
sundar@967 | 51 | return index*index; |
sundar@967 | 52 | }, |
sundar@967 | 53 | |
sundar@967 | 54 | setMember: function(name, value) { |
sundar@967 | 55 | print(name + " set to " + value); |
sundar@967 | 56 | }, |
sundar@967 | 57 | |
sundar@967 | 58 | setSlot: function(index, value) { |
sundar@967 | 59 | print("[" + index + "] set to " + value); |
sundar@967 | 60 | } |
sundar@967 | 61 | }; |
sundar@967 | 62 | |
sundar@967 | 63 | print("getMember('foo') =", bjsobj['getMember(String)']('foo')); |
sundar@967 | 64 | print("getSlot(6) =", bjsobj['getSlot(int)'](6)); |
sundar@967 | 65 | bjsobj['setMember(String, Object)']('bar', 'hello'); |
sundar@967 | 66 | bjsobj['setSlot(int, Object)'](10, 42); |
sundar@967 | 67 |