Fri, 14 Jul 2017 14:30:06 -0700
Merge
attila@1250 | 1 | /* |
sundar@1482 | 2 | * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
attila@1250 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
sundar@1482 | 4 | * |
attila@1250 | 5 | * This code is free software; you can redistribute it and/or modify it |
attila@1250 | 6 | * under the terms of the GNU General Public License version 2 only, as |
attila@1250 | 7 | * published by the Free Software Foundation. |
sundar@1482 | 8 | * |
attila@1250 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
attila@1250 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
attila@1250 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
attila@1250 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
attila@1250 | 13 | * accompanied this code). |
sundar@1482 | 14 | * |
attila@1250 | 15 | * You should have received a copy of the GNU General Public License version |
attila@1250 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
attila@1250 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
sundar@1482 | 18 | * |
attila@1250 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
attila@1250 | 20 | * or visit www.oracle.com if you need additional information or have any |
attila@1250 | 21 | * questions. |
attila@1250 | 22 | */ |
attila@1250 | 23 | |
attila@1250 | 24 | /** |
attila@1250 | 25 | * JDK-8072426: Can't compare Java objects to strings or numbers |
attila@1250 | 26 | * |
attila@1250 | 27 | * @test |
attila@1250 | 28 | * @run |
attila@1250 | 29 | */ |
attila@1250 | 30 | |
attila@1250 | 31 | Assert.assertTrue(java.math.RoundingMode.UP == "UP"); |
attila@1250 | 32 | |
attila@1250 | 33 | var JSObject = Java.type("jdk.nashorn.api.scripting.JSObject"); |
attila@1250 | 34 | |
attila@1250 | 35 | // Adds an "isFunction" member to the JSObject that returns the specified value |
attila@1250 | 36 | function addIsFunction(isFunction, obj) { |
attila@1250 | 37 | obj.isFunction = function() { |
attila@1250 | 38 | return isFunction; |
attila@1250 | 39 | }; |
attila@1250 | 40 | return obj; |
attila@1250 | 41 | } |
attila@1250 | 42 | |
attila@1250 | 43 | function makeJSObjectConstantFunction(value) { |
attila@1250 | 44 | return new JSObject(addIsFunction(true, { |
attila@1250 | 45 | call: function() { |
attila@1250 | 46 | return value; |
attila@1250 | 47 | } |
attila@1250 | 48 | })); |
attila@1250 | 49 | } |
attila@1250 | 50 | |
attila@1250 | 51 | function makeJSObjectWithMembers(mapping) { |
attila@1250 | 52 | return new JSObject({ |
attila@1250 | 53 | getMember: function(name) { |
attila@1250 | 54 | Assert.assertTrue(mapping.hasOwnProperty(name)); |
attila@1250 | 55 | return mapping[name]; |
attila@1250 | 56 | }, |
attila@1250 | 57 | toNumber: function() { |
attila@1250 | 58 | // toNumber no longer invoked |
attila@1250 | 59 | Assert.fail(); |
attila@1250 | 60 | } |
attila@1250 | 61 | }); |
attila@1250 | 62 | } |
attila@1250 | 63 | |
attila@1250 | 64 | // Test JSObjectLinker toInt32/toLong/toNumber |
attila@1250 | 65 | function testNumericJSObject(kind, value) { |
attila@1250 | 66 | var obj = makeJSObjectWithMembers({ |
attila@1250 | 67 | valueOf: makeJSObjectConstantFunction(value) |
attila@1250 | 68 | }); |
attila@1250 | 69 | |
attila@1250 | 70 | if (kind === "double") { |
attila@1250 | 71 | // There's no assertEquals(double actual, double expected). There's only |
attila@1250 | 72 | // assertEquals(double actual, double expected, double delta). |
attila@1250 | 73 | Assert["assertEquals(double,double,double)"](value, obj, 0); |
attila@1250 | 74 | } else { |
attila@1250 | 75 | Assert["assertEquals(" + kind + ", " + kind + ")"](value, obj); |
attila@1250 | 76 | } |
attila@1250 | 77 | Assert.assertTrue(value == Number(obj)); |
attila@1250 | 78 | } |
attila@1250 | 79 | testNumericJSObject("int", 42); |
attila@1250 | 80 | testNumericJSObject("long", 4294967296); |
attila@1250 | 81 | testNumericJSObject("double", 1.2); |
attila@1250 | 82 | |
attila@1250 | 83 | // Test fallback from toNumber to toString for numeric conversion when toNumber doesn't exist |
attila@1250 | 84 | (function() { |
attila@1250 | 85 | var obj = makeJSObjectWithMembers({ |
attila@1250 | 86 | valueOf: null, // Explicitly no valueOf |
attila@1250 | 87 | toString: makeJSObjectConstantFunction("123") |
attila@1250 | 88 | }); |
attila@1250 | 89 | Assert["assertEquals(int,int)"](123, obj); |
attila@1250 | 90 | })(); |
attila@1250 | 91 | |
attila@1250 | 92 | // Test fallback from toNumber to toString for numeric conversion when toNumber isn't a callable |
attila@1250 | 93 | (function() { |
attila@1250 | 94 | var obj = makeJSObjectWithMembers({ |
attila@1250 | 95 | valueOf: new JSObject(addIsFunction(false, {})), |
attila@1250 | 96 | toString: makeJSObjectConstantFunction("124") |
attila@1250 | 97 | }); |
attila@1250 | 98 | Assert["assertEquals(int,int)"](124, obj); |
attila@1250 | 99 | })(); |
attila@1250 | 100 | |
attila@1250 | 101 | // Test fallback from toNumber to toString for numeric conversion when toNumber returns a non-primitive |
attila@1250 | 102 | (function() { |
attila@1250 | 103 | var obj = makeJSObjectWithMembers({ |
attila@1250 | 104 | valueOf: makeJSObjectConstantFunction({}), |
attila@1250 | 105 | toString: makeJSObjectConstantFunction("125") |
attila@1250 | 106 | }); |
attila@1250 | 107 | Assert["assertEquals(int,int)"](125, obj); |
attila@1250 | 108 | })(); |
attila@1250 | 109 | |
attila@1250 | 110 | // Test TypeError from toNumber to toString when both return a non-primitive |
attila@1250 | 111 | (function() { |
attila@1250 | 112 | var obj = makeJSObjectWithMembers({ |
attila@1250 | 113 | valueOf: makeJSObjectConstantFunction({}), |
attila@1250 | 114 | toString: makeJSObjectConstantFunction({}) |
attila@1250 | 115 | }); |
attila@1250 | 116 | try { |
attila@1250 | 117 | Number(obj); |
attila@1250 | 118 | Assert.fail(); // must throw |
attila@1250 | 119 | } catch(e) { |
attila@1250 | 120 | Assert.assertTrue(e instanceof TypeError); |
attila@1250 | 121 | } |
attila@1250 | 122 | })(); |
attila@1250 | 123 | |
attila@1250 | 124 | // Test toString for string conversion |
attila@1250 | 125 | (function() { |
attila@1250 | 126 | var obj = makeJSObjectWithMembers({ |
attila@1250 | 127 | toString: makeJSObjectConstantFunction("Hello") |
attila@1250 | 128 | }); |
attila@1250 | 129 | Assert.assertTrue("Hello" === String(obj)); |
attila@1250 | 130 | Assert["assertEquals(String,String)"]("Hello", obj); |
attila@1250 | 131 | })(); |
attila@1250 | 132 | |
attila@1250 | 133 | // Test fallback from toString to valueOf for string conversion when toString doesn't exist |
attila@1250 | 134 | (function() { |
attila@1250 | 135 | var obj = makeJSObjectWithMembers({ |
attila@1250 | 136 | toString: null, |
attila@1250 | 137 | valueOf: makeJSObjectConstantFunction("Hello1") |
attila@1250 | 138 | }); |
attila@1250 | 139 | Assert.assertTrue("Hello1" === String(obj)); |
attila@1250 | 140 | Assert["assertEquals(String,String)"]("Hello1", obj); |
attila@1250 | 141 | })(); |
attila@1250 | 142 | |
attila@1250 | 143 | // Test fallback from toString to valueOf for string conversion when toString is not callable |
attila@1250 | 144 | (function() { |
attila@1250 | 145 | var obj = makeJSObjectWithMembers({ |
attila@1250 | 146 | toString: new JSObject(addIsFunction(false, {})), |
attila@1250 | 147 | valueOf: makeJSObjectConstantFunction("Hello2") |
attila@1250 | 148 | }); |
attila@1250 | 149 | Assert["assertEquals(String,String)"]("Hello2", obj); |
attila@1250 | 150 | })(); |
attila@1250 | 151 | |
attila@1250 | 152 | // Test fallback from toString to valueOf for string conversion when toString returns non-primitive |
attila@1250 | 153 | (function() { |
attila@1250 | 154 | var obj = makeJSObjectWithMembers({ |
attila@1250 | 155 | toString: makeJSObjectConstantFunction({}), |
attila@1250 | 156 | valueOf: makeJSObjectConstantFunction("Hello3") |
attila@1250 | 157 | }); |
attila@1250 | 158 | Assert["assertEquals(String,String)"]("Hello3", obj); |
attila@1250 | 159 | })(); |
attila@1250 | 160 | |
attila@1250 | 161 | // Test toBoolean for JSObject |
attila@1250 | 162 | (function() { |
attila@1250 | 163 | Assert["assertEquals(boolean,boolean)"](true, new JSObject({})); |
attila@1250 | 164 | })(); |