test/script/basic/JDK-8051778.js

Tue, 28 Jul 2015 14:52:34 +0530

author
sundar
date
Tue, 28 Jul 2015 14:52:34 +0530
changeset 1482
58791cd01bc9
parent 1110
a56051d3cdf5
permissions
-rw-r--r--

8132092: Nashorn copyright has to be updated
Reviewed-by: jlaskey, hannesw, mhaupt

attila@1110 1 /*
sundar@1482 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
attila@1110 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
sundar@1482 4 *
attila@1110 5 * This code is free software; you can redistribute it and/or modify it
attila@1110 6 * under the terms of the GNU General Public License version 2 only, as
attila@1110 7 * published by the Free Software Foundation.
sundar@1482 8 *
attila@1110 9 * This code is distributed in the hope that it will be useful, but WITHOUT
attila@1110 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
attila@1110 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
attila@1110 12 * version 2 for more details (a copy is included in the LICENSE file that
attila@1110 13 * accompanied this code).
sundar@1482 14 *
attila@1110 15 * You should have received a copy of the GNU General Public License version
attila@1110 16 * 2 along with this work; if not, write to the Free Software Foundation,
attila@1110 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
sundar@1482 18 *
attila@1110 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
attila@1110 20 * or visit www.oracle.com if you need additional information or have any
attila@1110 21 * questions.
attila@1110 22 */
attila@1110 23
attila@1110 24 /**
attila@1110 25 * JDK-8051778: support bind on all Nashorn callables
attila@1110 26 *
attila@1110 27 * @test
attila@1110 28 * @run
attila@1110 29 */
attila@1110 30
attila@1110 31 var bind = Function.prototype.bind;
attila@1110 32
attila@1110 33 // Bind a POJO method
attila@1110 34 var l = new java.util.ArrayList();
attila@1110 35 var l_add_foo = bind.call(l.add, l, "foo");
attila@1110 36 l_add_foo();
attila@1110 37 print("l=" + l);
attila@1110 38
attila@1110 39 // Bind a BoundCallable
attila@1110 40 var l_add = bind.call(l.add, l);
attila@1110 41 var l_add_foo2 = bind.call(l_add, null, "foo2");
attila@1110 42 l_add_foo2();
attila@1110 43 print("l=" + l);
attila@1110 44
attila@1110 45 // Bind a POJO method retrieved from one instance to a different but
attila@1110 46 // compatible instance.
attila@1110 47 var l2 = new java.util.ArrayList();
attila@1110 48 var l2_size = bind.call(l.size, l2);
attila@1110 49 print("l2_size()=" + l2_size());
attila@1110 50
attila@1110 51 // Bind a Java type object (used as a constructor).
attila@1110 52 var construct_two = bind.call(java.lang.Integer, null, 2);
attila@1110 53 print("Bound Integer(2) constructor: " + new construct_two())
attila@1110 54
attila@1110 55 // Bind a @FunctionalInterface proxying to an object literal. NOTE: the
attila@1110 56 // expected value of this.a is always "original" and never "bound". This
attila@1110 57 // might seem counterintuitive, but we are not binding the apply()
attila@1110 58 // function of the object literal that defines the BiFunction behaviour,
attila@1110 59 // we are binding the SAM proxy object instead, and it is always
attila@1110 60 // forwarding to the apply() function with "this" set to the object
attila@1110 61 // literal. Basically, binding "this" for SAM proxies is useless; only
attila@1110 62 // binding arguments makes sense.
attila@1110 63 var f1 = new java.util.function.BiFunction() {
attila@1110 64 apply: function(x, y) {
attila@1110 65 return "BiFunction with literal: " + this.a + ", " + x + ", " + y;
attila@1110 66 },
attila@1110 67 a: "unbound"
attila@1110 68 };
attila@1110 69 print((bind.call(f1, {a: "bound"}))(1, 2))
attila@1110 70 print((bind.call(f1, {a: "bound"}, 3))(4))
attila@1110 71 print((bind.call(f1, {a: "bound"}, 5, 6))())
attila@1110 72
attila@1110 73 // Bind a @FunctionalInterface proxying to a function. With the same
attila@1110 74 // reasoning as above (binding the proxy vs. binding the JS function),
attila@1110 75 // the value of this.a will always be undefined, and never "bound".
attila@1110 76 var f2 = new java.util.function.BiFunction(
attila@1110 77 function(x, y) {
attila@1110 78 return "BiFunction with function: " + this.a + ", " + x + ", " + y;
attila@1110 79 }
attila@1110 80 );
attila@1110 81 print((bind.call(f2, {a: "bound"}))(7, 8))
attila@1110 82 print((bind.call(f2, {a: "bound"}, 9))(10))
attila@1110 83 print((bind.call(f2, {a: "bound"}, 11, 12))())

mercurial