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

     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  */
    24 /**
    25  * JDK-8051778: support bind on all Nashorn callables
    26  *
    27  * @test
    28  * @run
    29  */
    31 var bind = Function.prototype.bind;
    33 // Bind a POJO method
    34 var l = new java.util.ArrayList();
    35 var l_add_foo = bind.call(l.add, l, "foo");
    36 l_add_foo();
    37 print("l=" + l);
    39 // Bind a BoundCallable
    40 var l_add = bind.call(l.add, l);
    41 var l_add_foo2 = bind.call(l_add, null, "foo2");
    42 l_add_foo2();
    43 print("l=" + l);
    45 // Bind a POJO method retrieved from one instance to a different but 
    46 // compatible instance.
    47 var l2 = new java.util.ArrayList();
    48 var l2_size = bind.call(l.size, l2);
    49 print("l2_size()=" + l2_size());
    51 // Bind a Java type object (used as a constructor).
    52 var construct_two = bind.call(java.lang.Integer, null, 2);
    53 print("Bound Integer(2) constructor: " + new construct_two())
    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.
    63 var 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 };
    69 print((bind.call(f1, {a: "bound"}))(1, 2))
    70 print((bind.call(f1, {a: "bound"}, 3))(4))
    71 print((bind.call(f1, {a: "bound"}, 5, 6))())
    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".
    76 var f2 = new java.util.function.BiFunction(
    77     function(x, y) {
    78         return "BiFunction with function: " + this.a + ", " + x + ", " + y;
    79     }
    80 );
    81 print((bind.call(f2, {a: "bound"}))(7, 8))
    82 print((bind.call(f2, {a: "bound"}, 9))(10))
    83 print((bind.call(f2, {a: "bound"}, 11, 12))())

mercurial