JDK-8021122.js revision 458:0cfa27ed82fe
1171854Sdes/* 2171854Sdes * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3171854Sdes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4171854Sdes * 5171854Sdes * This code is free software; you can redistribute it and/or modify it 6171854Sdes * under the terms of the GNU General Public License version 2 only, as 7171854Sdes * published by the Free Software Foundation. 8171854Sdes * 9171854Sdes * This code is distributed in the hope that it will be useful, but WITHOUT 10171854Sdes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11171854Sdes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12171854Sdes * version 2 for more details (a copy is included in the LICENSE file that 13171854Sdes * accompanied this code). 14171854Sdes * 15171854Sdes * You should have received a copy of the GNU General Public License version 16171854Sdes * 2 along with this work; if not, write to the Free Software Foundation, 17171854Sdes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18171854Sdes * 19171854Sdes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20171854Sdes * or visit www.oracle.com if you need additional information or have any 21171854Sdes * questions. 22171854Sdes */ 23171854Sdes 24171854Sdes/** 25171854Sdes * JDK-8021122: Not all callables are handled for toString and other function valued properties 26171854Sdes * 27171854Sdes * @test 28171854Sdes * @run 29171854Sdes */ 30171854Sdes 31171854Sdesvar a = {} 32171854Sdesvar obj = new java.util.HashMap(); 33171854SdesObject.bindProperties(a, obj); 34171854Sdestry { 35171854Sdes print(a); 36171854Sdes} catch (e) { 37171854Sdes print(e); 38171854Sdes} 39171854Sdes 40171854Sdesvar a = {} 41171854Sdesvar global = loadWithNewGlobal({ name:"xx", script: "this" }); 42171854Sdesvar obj = global.eval("({ toString: function() { return 'hello'; } })"); 43171854SdesObject.bindProperties(a, obj); 44171854Sdestry { 45171854Sdes print(a); 46171854Sdes} catch (e) { 47171854Sdes print(e); 48171854Sdes} 49171854Sdes 50171854Sdesfunction runLambdaTests() { 51171854Sdes var r = new java.lang.Runnable() { 52171854Sdes run: function() { print("I am runnable"); } 53171854Sdes }; 54171854Sdes 55171854Sdes // call any @FunctionalInterface object as though it is a function 56171854Sdes r(); 57171854Sdes 58171854Sdes var twice = new java.util.function.Function() { 59171854Sdes apply: function(x) 2*x 60171854Sdes }; 61171854Sdes 62171854Sdes print(twice(34)); 63171854Sdes 64171854Sdes var sum = new java.util.function.BiFunction() { 65171854Sdes apply: function(x, y) x + y 66171854Sdes }; 67171854Sdes 68171854Sdes print(sum(32, 12)) 69171854Sdes 70171854Sdes // make toString to be a @FunctionalInterface object 71171854Sdes var a = {}; 72171854Sdes a.toString = new java.util.function.Supplier() { 73171854Sdes get: function() { return "MyString"; } 74171854Sdes }; 75171854Sdes 76171854Sdes try { 77171854Sdes print(a); 78171854Sdes } catch (e) { 79171854Sdes print(e); 80171854Sdes } 81171854Sdes} 82171854Sdes 83171854Sdestry { 84171854Sdes // check for java.util.function.Function class 85171854Sdes Java.type("java.util.function.Function"); 86171854Sdes runLambdaTests(); 87171854Sdes} catch (e) { 88171854Sdes // fake output to match .EXPECTED values 89171854Sdes print("I am runnable"); 90171854Sdes print("68"); 91171854Sdes print("44"); 92171854Sdes print("MyString"); 93171854Sdes} 94171854Sdes