attila@1110: /* sundar@1482: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. attila@1110: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. sundar@1482: * attila@1110: * This code is free software; you can redistribute it and/or modify it attila@1110: * under the terms of the GNU General Public License version 2 only, as attila@1110: * published by the Free Software Foundation. sundar@1482: * attila@1110: * This code is distributed in the hope that it will be useful, but WITHOUT attila@1110: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or attila@1110: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License attila@1110: * version 2 for more details (a copy is included in the LICENSE file that attila@1110: * accompanied this code). sundar@1482: * attila@1110: * You should have received a copy of the GNU General Public License version attila@1110: * 2 along with this work; if not, write to the Free Software Foundation, attila@1110: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. sundar@1482: * attila@1110: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA attila@1110: * or visit www.oracle.com if you need additional information or have any attila@1110: * questions. attila@1110: */ attila@1110: attila@1110: /** attila@1110: * JDK-8051778: support bind on all Nashorn callables attila@1110: * attila@1110: * @test attila@1110: * @run attila@1110: */ attila@1110: attila@1110: var bind = Function.prototype.bind; attila@1110: attila@1110: // Bind a POJO method attila@1110: var l = new java.util.ArrayList(); attila@1110: var l_add_foo = bind.call(l.add, l, "foo"); attila@1110: l_add_foo(); attila@1110: print("l=" + l); attila@1110: attila@1110: // Bind a BoundCallable attila@1110: var l_add = bind.call(l.add, l); attila@1110: var l_add_foo2 = bind.call(l_add, null, "foo2"); attila@1110: l_add_foo2(); attila@1110: print("l=" + l); attila@1110: attila@1110: // Bind a POJO method retrieved from one instance to a different but attila@1110: // compatible instance. attila@1110: var l2 = new java.util.ArrayList(); attila@1110: var l2_size = bind.call(l.size, l2); attila@1110: print("l2_size()=" + l2_size()); attila@1110: attila@1110: // Bind a Java type object (used as a constructor). attila@1110: var construct_two = bind.call(java.lang.Integer, null, 2); attila@1110: print("Bound Integer(2) constructor: " + new construct_two()) attila@1110: attila@1110: // Bind a @FunctionalInterface proxying to an object literal. NOTE: the attila@1110: // expected value of this.a is always "original" and never "bound". This attila@1110: // might seem counterintuitive, but we are not binding the apply() attila@1110: // function of the object literal that defines the BiFunction behaviour, attila@1110: // we are binding the SAM proxy object instead, and it is always attila@1110: // forwarding to the apply() function with "this" set to the object attila@1110: // literal. Basically, binding "this" for SAM proxies is useless; only attila@1110: // binding arguments makes sense. attila@1110: var f1 = new java.util.function.BiFunction() { attila@1110: apply: function(x, y) { attila@1110: return "BiFunction with literal: " + this.a + ", " + x + ", " + y; attila@1110: }, attila@1110: a: "unbound" attila@1110: }; attila@1110: print((bind.call(f1, {a: "bound"}))(1, 2)) attila@1110: print((bind.call(f1, {a: "bound"}, 3))(4)) attila@1110: print((bind.call(f1, {a: "bound"}, 5, 6))()) attila@1110: attila@1110: // Bind a @FunctionalInterface proxying to a function. With the same attila@1110: // reasoning as above (binding the proxy vs. binding the JS function), attila@1110: // the value of this.a will always be undefined, and never "bound". attila@1110: var f2 = new java.util.function.BiFunction( attila@1110: function(x, y) { attila@1110: return "BiFunction with function: " + this.a + ", " + x + ", " + y; attila@1110: } attila@1110: ); attila@1110: print((bind.call(f2, {a: "bound"}))(7, 8)) attila@1110: print((bind.call(f2, {a: "bound"}, 9))(10)) attila@1110: print((bind.call(f2, {a: "bound"}, 11, 12))())