attila@1250: /* sundar@1482: * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. attila@1250: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. sundar@1482: * attila@1250: * This code is free software; you can redistribute it and/or modify it attila@1250: * under the terms of the GNU General Public License version 2 only, as attila@1250: * published by the Free Software Foundation. sundar@1482: * attila@1250: * This code is distributed in the hope that it will be useful, but WITHOUT attila@1250: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or attila@1250: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License attila@1250: * version 2 for more details (a copy is included in the LICENSE file that attila@1250: * accompanied this code). sundar@1482: * attila@1250: * You should have received a copy of the GNU General Public License version attila@1250: * 2 along with this work; if not, write to the Free Software Foundation, attila@1250: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. sundar@1482: * attila@1250: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA attila@1250: * or visit www.oracle.com if you need additional information or have any attila@1250: * questions. attila@1250: */ attila@1250: attila@1250: /** attila@1250: * JDK-8072426: Can't compare Java objects to strings or numbers attila@1250: * attila@1250: * @test attila@1250: * @run attila@1250: */ attila@1250: attila@1250: Assert.assertTrue(java.math.RoundingMode.UP == "UP"); attila@1250: attila@1250: var JSObject = Java.type("jdk.nashorn.api.scripting.JSObject"); attila@1250: attila@1250: // Adds an "isFunction" member to the JSObject that returns the specified value attila@1250: function addIsFunction(isFunction, obj) { attila@1250: obj.isFunction = function() { attila@1250: return isFunction; attila@1250: }; attila@1250: return obj; attila@1250: } attila@1250: attila@1250: function makeJSObjectConstantFunction(value) { attila@1250: return new JSObject(addIsFunction(true, { attila@1250: call: function() { attila@1250: return value; attila@1250: } attila@1250: })); attila@1250: } attila@1250: attila@1250: function makeJSObjectWithMembers(mapping) { attila@1250: return new JSObject({ attila@1250: getMember: function(name) { attila@1250: Assert.assertTrue(mapping.hasOwnProperty(name)); attila@1250: return mapping[name]; attila@1250: }, attila@1250: toNumber: function() { attila@1250: // toNumber no longer invoked attila@1250: Assert.fail(); attila@1250: } attila@1250: }); attila@1250: } attila@1250: attila@1250: // Test JSObjectLinker toInt32/toLong/toNumber attila@1250: function testNumericJSObject(kind, value) { attila@1250: var obj = makeJSObjectWithMembers({ attila@1250: valueOf: makeJSObjectConstantFunction(value) attila@1250: }); attila@1250: attila@1250: if (kind === "double") { attila@1250: // There's no assertEquals(double actual, double expected). There's only attila@1250: // assertEquals(double actual, double expected, double delta). attila@1250: Assert["assertEquals(double,double,double)"](value, obj, 0); attila@1250: } else { attila@1250: Assert["assertEquals(" + kind + ", " + kind + ")"](value, obj); attila@1250: } attila@1250: Assert.assertTrue(value == Number(obj)); attila@1250: } attila@1250: testNumericJSObject("int", 42); attila@1250: testNumericJSObject("long", 4294967296); attila@1250: testNumericJSObject("double", 1.2); attila@1250: attila@1250: // Test fallback from toNumber to toString for numeric conversion when toNumber doesn't exist attila@1250: (function() { attila@1250: var obj = makeJSObjectWithMembers({ attila@1250: valueOf: null, // Explicitly no valueOf attila@1250: toString: makeJSObjectConstantFunction("123") attila@1250: }); attila@1250: Assert["assertEquals(int,int)"](123, obj); attila@1250: })(); attila@1250: attila@1250: // Test fallback from toNumber to toString for numeric conversion when toNumber isn't a callable attila@1250: (function() { attila@1250: var obj = makeJSObjectWithMembers({ attila@1250: valueOf: new JSObject(addIsFunction(false, {})), attila@1250: toString: makeJSObjectConstantFunction("124") attila@1250: }); attila@1250: Assert["assertEquals(int,int)"](124, obj); attila@1250: })(); attila@1250: attila@1250: // Test fallback from toNumber to toString for numeric conversion when toNumber returns a non-primitive attila@1250: (function() { attila@1250: var obj = makeJSObjectWithMembers({ attila@1250: valueOf: makeJSObjectConstantFunction({}), attila@1250: toString: makeJSObjectConstantFunction("125") attila@1250: }); attila@1250: Assert["assertEquals(int,int)"](125, obj); attila@1250: })(); attila@1250: attila@1250: // Test TypeError from toNumber to toString when both return a non-primitive attila@1250: (function() { attila@1250: var obj = makeJSObjectWithMembers({ attila@1250: valueOf: makeJSObjectConstantFunction({}), attila@1250: toString: makeJSObjectConstantFunction({}) attila@1250: }); attila@1250: try { attila@1250: Number(obj); attila@1250: Assert.fail(); // must throw attila@1250: } catch(e) { attila@1250: Assert.assertTrue(e instanceof TypeError); attila@1250: } attila@1250: })(); attila@1250: attila@1250: // Test toString for string conversion attila@1250: (function() { attila@1250: var obj = makeJSObjectWithMembers({ attila@1250: toString: makeJSObjectConstantFunction("Hello") attila@1250: }); attila@1250: Assert.assertTrue("Hello" === String(obj)); attila@1250: Assert["assertEquals(String,String)"]("Hello", obj); attila@1250: })(); attila@1250: attila@1250: // Test fallback from toString to valueOf for string conversion when toString doesn't exist attila@1250: (function() { attila@1250: var obj = makeJSObjectWithMembers({ attila@1250: toString: null, attila@1250: valueOf: makeJSObjectConstantFunction("Hello1") attila@1250: }); attila@1250: Assert.assertTrue("Hello1" === String(obj)); attila@1250: Assert["assertEquals(String,String)"]("Hello1", obj); attila@1250: })(); attila@1250: attila@1250: // Test fallback from toString to valueOf for string conversion when toString is not callable attila@1250: (function() { attila@1250: var obj = makeJSObjectWithMembers({ attila@1250: toString: new JSObject(addIsFunction(false, {})), attila@1250: valueOf: makeJSObjectConstantFunction("Hello2") attila@1250: }); attila@1250: Assert["assertEquals(String,String)"]("Hello2", obj); attila@1250: })(); attila@1250: attila@1250: // Test fallback from toString to valueOf for string conversion when toString returns non-primitive attila@1250: (function() { attila@1250: var obj = makeJSObjectWithMembers({ attila@1250: toString: makeJSObjectConstantFunction({}), attila@1250: valueOf: makeJSObjectConstantFunction("Hello3") attila@1250: }); attila@1250: Assert["assertEquals(String,String)"]("Hello3", obj); attila@1250: })(); attila@1250: attila@1250: // Test toBoolean for JSObject attila@1250: (function() { attila@1250: Assert["assertEquals(boolean,boolean)"](true, new JSObject({})); attila@1250: })();