JDK-8055796.js revision 979:ee00439d1d54
11541Srgrimes/* 222521Sdyson * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 31541Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41541Srgrimes * 51541Srgrimes * This code is free software; you can redistribute it and/or modify it 61541Srgrimes * under the terms of the GNU General Public License version 2 only, as 71541Srgrimes * published by the Free Software Foundation. 81541Srgrimes * 91541Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT 101541Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 111541Srgrimes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 121541Srgrimes * version 2 for more details (a copy is included in the LICENSE file that 131541Srgrimes * accompanied this code). 141541Srgrimes * 151541Srgrimes * You should have received a copy of the GNU General Public License version 161541Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 171541Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 181541Srgrimes * 191541Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 201541Srgrimes * or visit www.oracle.com if you need additional information or have any 211541Srgrimes * questions. 221541Srgrimes */ 231541Srgrimes 241541Srgrimes/** 251541Srgrimes * JDK-8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly 261541Srgrimes * 271541Srgrimes * @test 281541Srgrimes * @run 291541Srgrimes */ 301541Srgrimes 311541Srgrimesvar m = new javax.script.ScriptEngineManager(); 321541Srgrimesvar e = m.getEngineByName("nashorn"); 3322521Sdysonvar jsobj = e.eval("({ foo: 33, valueOf: function() 42 })"); 3450477Speter 351541Srgrimesprint("foo =", jsobj['getMember(java.lang.String)']("foo")); 361541Srgrimesprint("eval =", jsobj['eval(String)']("this + 44")); 371541Srgrimesprint("valueOf function? =", (jsobj.valueOf)['isFunction()']()); 3895759Stanimura 3995759Stanimuravar JSObject = Java.type("netscape.javascript.JSObject"); 4095759Stanimuravar bjsobj = new (Java.extend(JSObject))() { 4195759Stanimura getMember: function(name) { 4222600Smpp if (name == "func") { 4331561Sbde return function(arg) { 4489316Salfred print("func called with " + arg); 4530354Sphk } 4695759Stanimura } 4783366Sjulian return name.toUpperCase(); 4895759Stanimura }, 491541Srgrimes 501541Srgrimes getSlot: function(index) { 5195759Stanimura return index*index; 5295759Stanimura }, 537090Sbde 5495759Stanimura setMember: function(name, value) { 5595759Stanimura print(name + " set to " + value); 5677031Sru }, 571541Srgrimes 581541Srgrimes setSlot: function(index, value) { 591541Srgrimes print("[" + index + "] set to " + value); 601541Srgrimes } 611541Srgrimes}; 621541Srgrimes 631541Srgrimesprint("getMember('foo') =", bjsobj['getMember(String)']('foo')); 641541Srgrimesprint("getSlot(6) =", bjsobj['getSlot(int)'](6)); 651541Srgrimesbjsobj['setMember(String, Object)']('bar', 'hello'); 661541Srgrimesbjsobj['setSlot(int, Object)'](10, 42); 671541Srgrimes 681541Srgrimes