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