attila@1065: /* sundar@1482: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. attila@1065: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. sundar@1482: * attila@1065: * This code is free software; you can redistribute it and/or modify it attila@1065: * under the terms of the GNU General Public License version 2 only, as attila@1065: * published by the Free Software Foundation. sundar@1482: * attila@1065: * This code is distributed in the hope that it will be useful, but WITHOUT attila@1065: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or attila@1065: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License attila@1065: * version 2 for more details (a copy is included in the LICENSE file that attila@1065: * accompanied this code). sundar@1482: * attila@1065: * You should have received a copy of the GNU General Public License version attila@1065: * 2 along with this work; if not, write to the Free Software Foundation, attila@1065: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. sundar@1482: * attila@1065: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA attila@1065: * or visit www.oracle.com if you need additional information or have any attila@1065: * questions. attila@1065: */ attila@1065: attila@1065: /** attila@1065: * JDK-8058610: must not let long operations overflow attila@1065: * attila@1065: * @test attila@1065: * @run attila@1065: */ attila@1065: attila@1065: function mul(x) { attila@1065: return x.foo * x.bar; attila@1065: } attila@1065: print("=== mul ===") attila@1065: print(mul({foo: 2147483647, bar: 2147483647})); // 2^31 attila@1065: print(mul({foo: 17179869184, bar: 2147483647})); // 2^34 attila@1065: attila@1065: function self_mul(x) { attila@1065: return x.foo *= x.bar; attila@1065: } attila@1065: print("=== self_mul ===") attila@1065: print(self_mul({foo: 2147483647, bar: 2147483647})); // 2^31 attila@1065: print(self_mul({foo: 17179869184, bar: 2147483647})); // 2^34 attila@1065: attila@1065: // We'll need to use this function to obtain long values larger in attila@1065: // magnitude than those precisely representable in a double (2^53), attila@1065: // as Nashorn's parser will reify such literals as a double. For attila@1065: // overflow on add and sub we need (2^63)-1. attila@1065: var parseLong = Java.type("java.lang.Long").parseLong; attila@1065: attila@1065: function sub(x) { attila@1065: return x.foo - x.bar; attila@1065: } attila@1065: print("=== sub ===") attila@1065: print(sub({foo: 2147483647, bar: -2147483647})); // 2^31 attila@1065: print(sub({foo: parseLong("9223372036854775807"), bar: parseLong("-9223372036854775807")})); // 2^63-1 attila@1065: attila@1065: function self_sub(x) { attila@1065: return x.foo -= x.bar; attila@1065: } attila@1065: print("=== self_sub ===") attila@1065: print(self_sub({foo: 2147483647, bar: -2147483647})); // 2^31 attila@1065: print(self_sub({foo: parseLong("9223372036854775807"), bar: parseLong("-9223372036854775807")})); // 2^63-1 attila@1065: attila@1065: function add(x) { attila@1065: return x.foo + x.bar; attila@1065: } attila@1065: print("=== add ===") attila@1065: print(add({foo: 2147483647, bar: 2147483647})); // 2^31 attila@1065: print(add({foo: parseLong("9223372036854775807"), bar: parseLong("9223372036854775807")})); // 2^63-1 attila@1065: attila@1065: function self_add(x) { attila@1065: return x.foo += x.bar; attila@1065: } attila@1065: print("=== self_add ===") attila@1065: print(self_add({foo: 2147483647, bar: 2147483647})); // 2^31 attila@1065: print(self_add({foo: parseLong("9223372036854775807"), bar: parseLong("9223372036854775807")})); // 2^63-1