JDK-8051778.js revision 1365:833a4df84bc7
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-8051778: support bind on all Nashorn callables
26 *
27 * @test
28 * @run
29 */
30
31var bind = Function.prototype.bind;
32
33// Bind a POJO method
34var l = new java.util.ArrayList();
35var l_add_foo = bind.call(l.add, l, "foo");
36l_add_foo();
37print("l=" + l);
38
39// Bind a BoundCallable
40var l_add = bind.call(l.add, l);
41var l_add_foo2 = bind.call(l_add, null, "foo2");
42l_add_foo2();
43print("l=" + l);
44
45// Bind a POJO method retrieved from one instance to a different but
46// compatible instance.
47var l2 = new java.util.ArrayList();
48var l2_size = bind.call(l.size, l2);
49print("l2_size()=" + l2_size());
50
51// Bind a Java type object (used as a constructor).
52var construct_two = bind.call(java.lang.Integer, null, 2);
53print("Bound Integer(2) constructor: " + new construct_two())
54
55// Bind a @FunctionalInterface proxying to an object literal. NOTE: the
56// expected value of this.a is always "original" and never "bound". This
57// might seem counterintuitive, but we are not binding the apply()
58// function of the object literal that defines the BiFunction behaviour,
59// we are binding the SAM proxy object instead, and it is always
60// forwarding to the apply() function with "this" set to the object
61// literal. Basically, binding "this" for SAM proxies is useless; only
62// binding arguments makes sense.
63var f1 = new java.util.function.BiFunction() {
64    apply: function(x, y) {
65        return "BiFunction with literal: " + this.a + ", " + x + ", " + y;
66    },
67    a: "unbound"
68};
69print((bind.call(f1, {a: "bound"}))(1, 2))
70print((bind.call(f1, {a: "bound"}, 3))(4))
71print((bind.call(f1, {a: "bound"}, 5, 6))())
72
73// Bind a @FunctionalInterface proxying to a function. With the same
74// reasoning as above (binding the proxy vs. binding the JS function),
75// the value of this.a will always be undefined, and never "bound".
76var f2 = new java.util.function.BiFunction(
77    function(x, y) {
78        return "BiFunction with function: " + this.a + ", " + x + ", " + y;
79    }
80);
81print((bind.call(f2, {a: "bound"}))(7, 8))
82print((bind.call(f2, {a: "bound"}, 9))(10))
83print((bind.call(f2, {a: "bound"}, 11, 12))())
84