Tue, 01 Sep 2015 18:28:11 +0530
8068901: Surprising behavior with more than one functional interface on a class
8068903: Can't invoke vararg @FunctionalInterface methods
Reviewed-by: attila, hannesw
attila@1252 | 1 | /* |
sundar@1482 | 2 | * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
attila@1252 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
sundar@1482 | 4 | * |
attila@1252 | 5 | * This code is free software; you can redistribute it and/or modify it |
attila@1252 | 6 | * under the terms of the GNU General Public License version 2 only, as |
attila@1252 | 7 | * published by the Free Software Foundation. |
sundar@1482 | 8 | * |
attila@1252 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
attila@1252 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
attila@1252 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
attila@1252 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
attila@1252 | 13 | * accompanied this code). |
sundar@1482 | 14 | * |
attila@1252 | 15 | * You should have received a copy of the GNU General Public License version |
attila@1252 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
attila@1252 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
sundar@1482 | 18 | * |
attila@1252 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
attila@1252 | 20 | * or visit www.oracle.com if you need additional information or have any |
attila@1252 | 21 | * questions. |
attila@1252 | 22 | */ |
attila@1252 | 23 | |
attila@1252 | 24 | /** |
attila@1252 | 25 | * JDK-8035712: Restore some of the RuntimeCallSite specializations |
attila@1252 | 26 | * |
attila@1252 | 27 | * @test |
attila@1252 | 28 | * @run |
attila@1252 | 29 | */ |
attila@1252 | 30 | |
attila@1252 | 31 | if ((typeof Assert) == "undefined") { |
attila@1252 | 32 | Assert = { |
attila@1252 | 33 | assertTrue: function(x) { if(!x) { throw "expected true" } }, |
attila@1252 | 34 | assertFalse: function(x) { if(x) { throw "expected false" } }, |
attila@1252 | 35 | }; |
attila@1252 | 36 | } |
attila@1252 | 37 | |
attila@1252 | 38 | function nop() {} |
attila@1252 | 39 | |
attila@1252 | 40 | function EQ(x, y) { |
attila@1252 | 41 | // Exercise normal evaluation |
attila@1252 | 42 | Assert.assertTrue (x == y); |
attila@1252 | 43 | Assert.assertTrue (y == x); |
attila@1252 | 44 | Assert.assertFalse(x != y); |
attila@1252 | 45 | Assert.assertFalse(y != x); |
attila@1252 | 46 | // Exercise the branch optimizer |
attila@1252 | 47 | if (x == y) { nop(); } else { Assert.fail(); } |
attila@1252 | 48 | if (y == x) { nop(); } else { Assert.fail(); } |
attila@1252 | 49 | if (x != y) { Assert.fail(); } else { nop(); } |
attila@1252 | 50 | if (y != x) { Assert.fail(); } else { nop(); } |
attila@1252 | 51 | } |
attila@1252 | 52 | |
attila@1252 | 53 | function NE(x, y) { |
attila@1252 | 54 | // Exercise normal evaluation |
attila@1252 | 55 | Assert.assertTrue (x != y); |
attila@1252 | 56 | Assert.assertTrue (y != x); |
attila@1252 | 57 | Assert.assertFalse(x == y); |
attila@1252 | 58 | Assert.assertFalse(y == x); |
attila@1252 | 59 | // Exercise the branch optimizer |
attila@1252 | 60 | if (x != y) { nop(); } else { Assert.fail(); } |
attila@1252 | 61 | if (y != x) { nop(); } else { Assert.fail(); } |
attila@1252 | 62 | if (x == y) { Assert.fail(); } else { nop(); } |
attila@1252 | 63 | if (y == x) { Assert.fail(); } else { nop(); } |
attila@1252 | 64 | } |
attila@1252 | 65 | |
attila@1252 | 66 | function STRICT_EQ(x, y) { |
attila@1252 | 67 | // Exercise normal evaluation |
attila@1252 | 68 | Assert.assertTrue (x === y); |
attila@1252 | 69 | Assert.assertTrue (y === x); |
attila@1252 | 70 | Assert.assertFalse(x !== y); |
attila@1252 | 71 | Assert.assertFalse(y !== x); |
attila@1252 | 72 | // Exercise the branch optimizer |
attila@1252 | 73 | if (x === y) { nop(); } else { Assert.fail(); } |
attila@1252 | 74 | if (y === x) { nop(); } else { Assert.fail(); } |
attila@1252 | 75 | if (x !== y) { Assert.fail(); } else { nop(); } |
attila@1252 | 76 | if (y !== x) { Assert.fail(); } else { nop(); } |
attila@1252 | 77 | } |
attila@1252 | 78 | |
attila@1252 | 79 | function STRICT_NE(x, y) { |
attila@1252 | 80 | // Exercise normal evaluation |
attila@1252 | 81 | Assert.assertTrue (x !== y); |
attila@1252 | 82 | Assert.assertTrue (y !== x); |
attila@1252 | 83 | Assert.assertFalse(x === y); |
attila@1252 | 84 | Assert.assertFalse(y === x); |
attila@1252 | 85 | // Exercise the branch optimizer |
attila@1252 | 86 | if (x !== y) { nop(); } else { Assert.fail(); } |
attila@1252 | 87 | if (y !== x) { nop(); } else { Assert.fail(); } |
attila@1252 | 88 | if (x === y) { Assert.fail(); } else { nop(); } |
attila@1252 | 89 | if (y === x) { Assert.fail(); } else { nop(); } |
attila@1252 | 90 | } |
attila@1252 | 91 | |
attila@1252 | 92 | function cmpToAnyNumber(cmp, value) { |
attila@1252 | 93 | cmp(1, value); |
attila@1252 | 94 | cmp(4294967296, value); |
attila@1252 | 95 | cmp(1.2, value); |
attila@1252 | 96 | cmp(Infinity, value); |
attila@1252 | 97 | cmp(-Infinity, value); |
attila@1252 | 98 | cmp(1/Infinity, value); |
attila@1252 | 99 | cmp(0, value); |
attila@1252 | 100 | cmp(-0, value); |
attila@1252 | 101 | cmp(true, value); |
attila@1252 | 102 | cmp(false, value); |
attila@1252 | 103 | } |
attila@1252 | 104 | |
attila@1252 | 105 | function notEqualToAnyNumber(value) { |
attila@1252 | 106 | cmpToAnyNumber(NE, value); |
attila@1252 | 107 | cmpToAnyNumber(STRICT_NE, value); |
attila@1252 | 108 | } |
attila@1252 | 109 | |
attila@1252 | 110 | notEqualToAnyNumber(null); |
attila@1252 | 111 | notEqualToAnyNumber(void 0); |
attila@1252 | 112 | notEqualToAnyNumber("abc"); |
attila@1252 | 113 | notEqualToAnyNumber({}); |
attila@1252 | 114 | notEqualToAnyNumber(["xyz"]); |
attila@1252 | 115 | |
attila@1252 | 116 | function objectWithPrimitiveFunctionNotEqualToAnyNumber(fnName) { |
attila@1252 | 117 | var obj = { |
attila@1252 | 118 | count: 0 |
attila@1252 | 119 | }; |
attila@1252 | 120 | obj[fnName] = function() { this.count++; return "foo"; }; |
attila@1252 | 121 | notEqualToAnyNumber(obj); |
attila@1252 | 122 | // Every NE will invoke it 8 times; cmpToAnyNumber has 10 comparisons |
attila@1252 | 123 | // STRICT_NE doesn't invoke toString. |
attila@1252 | 124 | Assert.assertTrue(80 === obj.count); |
attila@1252 | 125 | } |
attila@1252 | 126 | objectWithPrimitiveFunctionNotEqualToAnyNumber("valueOf"); |
attila@1252 | 127 | objectWithPrimitiveFunctionNotEqualToAnyNumber("toString"); |
attila@1252 | 128 | |
attila@1252 | 129 | function objectEqualButNotStrictlyEqual(val, obj) { |
attila@1252 | 130 | EQ(val, obj); |
attila@1252 | 131 | STRICT_NE(val, obj); |
attila@1252 | 132 | } |
attila@1252 | 133 | |
attila@1252 | 134 | function numberEqualButNotStrictlyEqualToObject(num, obj) { |
attila@1252 | 135 | objectEqualButNotStrictlyEqual(num, obj); |
attila@1252 | 136 | objectEqualButNotStrictlyEqual(num, [obj]); |
attila@1252 | 137 | objectEqualButNotStrictlyEqual(num, [[obj]]); |
attila@1252 | 138 | } |
attila@1252 | 139 | |
attila@1252 | 140 | function numberEqualButNotStrictlyEqualToZeroObjects(num) { |
attila@1252 | 141 | numberEqualButNotStrictlyEqualToObject(num, [0]); |
attila@1252 | 142 | numberEqualButNotStrictlyEqualToObject(num, ""); |
attila@1252 | 143 | numberEqualButNotStrictlyEqualToObject(num, []); |
attila@1252 | 144 | numberEqualButNotStrictlyEqualToObject(num, "0"); |
attila@1252 | 145 | } |
attila@1252 | 146 | |
attila@1252 | 147 | numberEqualButNotStrictlyEqualToZeroObjects(0); |
attila@1252 | 148 | numberEqualButNotStrictlyEqualToZeroObjects(1/Infinity); |
attila@1252 | 149 | numberEqualButNotStrictlyEqualToZeroObjects(false); |
attila@1252 | 150 | |
attila@1252 | 151 | function numberEqualButNotStrictlyEqualToObjectEquivalent(num) { |
attila@1252 | 152 | var str = String(num); |
attila@1252 | 153 | objectEqualButNotStrictlyEqual(num, str); |
attila@1252 | 154 | objectEqualButNotStrictlyEqual(num, { valueOf: function() { return str }}); |
attila@1252 | 155 | objectEqualButNotStrictlyEqual(num, { toString: function() { return str }}); |
attila@1252 | 156 | objectEqualButNotStrictlyEqual(num, { valueOf: function() { return num }}); |
attila@1252 | 157 | objectEqualButNotStrictlyEqual(num, { toString: function() { return num }}); |
attila@1252 | 158 | } |
attila@1252 | 159 | |
attila@1252 | 160 | numberEqualButNotStrictlyEqualToObjectEquivalent(1); |
attila@1252 | 161 | numberEqualButNotStrictlyEqualToObjectEquivalent(4294967296); |
attila@1252 | 162 | numberEqualButNotStrictlyEqualToObjectEquivalent(1.2); |
attila@1252 | 163 | numberEqualButNotStrictlyEqualToObjectEquivalent(Infinity); |
attila@1252 | 164 | numberEqualButNotStrictlyEqualToObjectEquivalent(-Infinity); |
attila@1252 | 165 | numberEqualButNotStrictlyEqualToObjectEquivalent(1/Infinity); |
attila@1252 | 166 | numberEqualButNotStrictlyEqualToObjectEquivalent(0); |
attila@1252 | 167 | numberEqualButNotStrictlyEqualToObjectEquivalent(-0); |
attila@1252 | 168 | |
attila@1252 | 169 | STRICT_EQ(1, new java.lang.Integer(1)); |
attila@1252 | 170 | STRICT_EQ(1, new java.lang.Double(1)); |
attila@1252 | 171 | STRICT_EQ(1.2, new java.lang.Double(1.2)); |
attila@1252 | 172 | |
attila@1252 | 173 | function LE(x, y) { |
attila@1252 | 174 | // Exercise normal evaluation |
attila@1252 | 175 | Assert.assertTrue(x <= y); |
attila@1252 | 176 | Assert.assertTrue(y >= x); |
attila@1252 | 177 | Assert.assertFalse(x > y); |
attila@1252 | 178 | Assert.assertFalse(x < y); |
attila@1252 | 179 | // Exercise the branch optimizer |
attila@1252 | 180 | if (x <= y) { nop(); } else { Assert.fail(); } |
attila@1252 | 181 | if (y >= x) { nop(); } else { Assert.fail(); } |
attila@1252 | 182 | if (x > y) { Assert.fail(); } else { nop(); } |
attila@1252 | 183 | if (y < x) { Assert.fail(); } else { nop(); } |
attila@1252 | 184 | } |
attila@1252 | 185 | |
attila@1252 | 186 | function mutuallyLessThanOrEqual(x, y) { |
attila@1252 | 187 | LE(x, y); |
attila@1252 | 188 | LE(y, x); |
attila@1252 | 189 | } |
attila@1252 | 190 | |
attila@1252 | 191 | mutuallyLessThanOrEqual(0, null); |
attila@1252 | 192 | mutuallyLessThanOrEqual(false, null); |
attila@1252 | 193 | mutuallyLessThanOrEqual(1/Infinity, null); |
attila@1252 | 194 | |
attila@1252 | 195 | function mutuallyLessThanEqualToObjectWithValue(num, val) { |
attila@1252 | 196 | mutuallyLessThanOrEqual(num, { valueOf: function() { return val } }); |
attila@1252 | 197 | mutuallyLessThanOrEqual(num, { toString: function() { return val } }); |
attila@1252 | 198 | } |
attila@1252 | 199 | |
attila@1252 | 200 | mutuallyLessThanEqualToObjectWithValue(false, 0); |
attila@1252 | 201 | mutuallyLessThanEqualToObjectWithValue(false, ""); |
attila@1252 | 202 | |
attila@1252 | 203 | mutuallyLessThanEqualToObjectWithValue(true, 1); |
attila@1252 | 204 | mutuallyLessThanEqualToObjectWithValue(true, "1"); |
attila@1252 | 205 | |
attila@1252 | 206 | function lessThanEqualToObjectEquivalent(num) { |
attila@1252 | 207 | var str = String(num); |
attila@1252 | 208 | mutuallyLessThanOrEqual(num, str); |
attila@1252 | 209 | mutuallyLessThanEqualToObjectWithValue(num, num); |
attila@1252 | 210 | mutuallyLessThanEqualToObjectWithValue(num, str); |
attila@1252 | 211 | } |
attila@1252 | 212 | |
attila@1252 | 213 | lessThanEqualToObjectEquivalent(1); |
attila@1252 | 214 | lessThanEqualToObjectEquivalent(4294967296); |
attila@1252 | 215 | lessThanEqualToObjectEquivalent(1.2); |
attila@1252 | 216 | lessThanEqualToObjectEquivalent(Infinity); |
attila@1252 | 217 | lessThanEqualToObjectEquivalent(-Infinity); |
attila@1252 | 218 | lessThanEqualToObjectEquivalent(1/Infinity); |
attila@1252 | 219 | lessThanEqualToObjectEquivalent(0); |
attila@1252 | 220 | lessThanEqualToObjectEquivalent(-0); |
attila@1252 | 221 | |
attila@1252 | 222 | function INCOMPARABLE(x, y) { |
attila@1252 | 223 | // Exercise normal evaluation |
attila@1252 | 224 | Assert.assertFalse(x < y); |
attila@1252 | 225 | Assert.assertFalse(x > y); |
attila@1252 | 226 | Assert.assertFalse(x <= y); |
attila@1252 | 227 | Assert.assertFalse(x >= y); |
attila@1252 | 228 | Assert.assertFalse(y < x); |
attila@1252 | 229 | Assert.assertFalse(y > x); |
attila@1252 | 230 | Assert.assertFalse(y <= x); |
attila@1252 | 231 | Assert.assertFalse(y >= x); |
attila@1252 | 232 | // Exercise the branch optimizer |
attila@1252 | 233 | if (x < y) { Assert.fail(); } else { nop(); } |
attila@1252 | 234 | if (x > y) { Assert.fail(); } else { nop(); } |
attila@1252 | 235 | if (x <= y) { Assert.fail(); } else { nop(); } |
attila@1252 | 236 | if (x >= y) { Assert.fail(); } else { nop(); } |
attila@1252 | 237 | if (y < x) { Assert.fail(); } else { nop(); } |
attila@1252 | 238 | if (y > x) { Assert.fail(); } else { nop(); } |
attila@1252 | 239 | if (y <= x) { Assert.fail(); } else { nop(); } |
attila@1252 | 240 | if (y >= x) { Assert.fail(); } else { nop(); } |
attila@1252 | 241 | } |
attila@1252 | 242 | |
attila@1252 | 243 | function isIncomparable(value) { |
attila@1252 | 244 | cmpToAnyNumber(INCOMPARABLE, value); |
attila@1252 | 245 | } |
attila@1252 | 246 | |
attila@1252 | 247 | isIncomparable(void 0); |
attila@1252 | 248 | isIncomparable({ valueOf: function() { return NaN }}); |
attila@1252 | 249 | isIncomparable({ toString: function() { return NaN }}); |
attila@1252 | 250 | |
attila@1252 | 251 | // Force ScriptRuntime.LT(Object, Object) etc. comparisons |
attila@1252 | 252 | function cmpObj(fn, x, y) { |
attila@1252 | 253 | fn({valueOf: function() { return x }}, {valueOf: function() { return y }}); |
attila@1252 | 254 | } |
attila@1252 | 255 | |
attila@1252 | 256 | function LT(x, y) { |
attila@1252 | 257 | Assert.assertTrue(x < y); |
attila@1252 | 258 | Assert.assertTrue(y > x); |
attila@1252 | 259 | Assert.assertFalse(x >= y); |
attila@1252 | 260 | Assert.assertFalse(y <= x); |
attila@1252 | 261 | } |
attila@1252 | 262 | |
attila@1252 | 263 | cmpObj(LT, 1, 2); |
attila@1252 | 264 | cmpObj(LT, 1, "2"); |
attila@1252 | 265 | cmpObj(LT, "1", 2); |
attila@1252 | 266 | cmpObj(LT, "a", "b"); |
attila@1252 | 267 | cmpObj(LT, -Infinity, 0); |
attila@1252 | 268 | cmpObj(LT, 0, Infinity); |
attila@1252 | 269 | cmpObj(LT, -Infinity, Infinity); |
attila@1252 | 270 | cmpObj(INCOMPARABLE, 1, NaN); |
attila@1252 | 271 | cmpObj(INCOMPARABLE, NaN, NaN); |
attila@1252 | 272 | cmpObj(INCOMPARABLE, "boo", NaN); |
attila@1252 | 273 | cmpObj(INCOMPARABLE, 1, "boo"); // boo number value will be NaN |
attila@1252 | 274 | |
attila@1252 | 275 | // Test that a comparison call site can deoptimize from (int, int) to (object, object) |
attila@1252 | 276 | (function(){ |
attila@1252 | 277 | var x = [1, 2, "a"]; |
attila@1252 | 278 | var y = [2, "3", "b"]; |
attila@1252 | 279 | for(var i = 0; i < 3; ++i) { |
attila@1252 | 280 | Assert.assertTrue(x[i] < y[i]); |
attila@1252 | 281 | } |
attila@1252 | 282 | })(); |