1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * JDK-8058610: must not let long operations overflow 26 * 27 * @test 28 * @run 29 */ 30 31 function mul(x) { 32 return x.foo * x.bar; 33 } 34 print("=== mul ===") 35 print(mul({foo: 2147483647, bar: 2147483647})); // 2^31 36 print(mul({foo: 17179869184, bar: 2147483647})); // 2^34 37 38 function self_mul(x) { 39 return x.foo *= x.bar; 40 } 41 print("=== self_mul ===") 42 print(self_mul({foo: 2147483647, bar: 2147483647})); // 2^31 43 print(self_mul({foo: 17179869184, bar: 2147483647})); // 2^34 44 45 // We'll need to use this function to obtain long values larger in 46 // magnitude than those precisely representable in a double (2^53), 47 // as Nashorn's parser will reify such literals as a double. For 48 // overflow on add and sub we need (2^63)-1. 49 var parseLong = Java.type("java.lang.Long").parseLong; 50 51 function sub(x) { 52 return x.foo - x.bar; 53 } 54 print("=== sub ===") 55 print(sub({foo: 2147483647, bar: -2147483647})); // 2^31 56 print(sub({foo: parseLong("9223372036854775807"), bar: parseLong("-9223372036854775807")})); // 2^63-1 57 58 function self_sub(x) { 59 return x.foo -= x.bar; 60 } 61 print("=== self_sub ===") 62 print(self_sub({foo: 2147483647, bar: -2147483647})); // 2^31 63 print(self_sub({foo: parseLong("9223372036854775807"), bar: parseLong("-9223372036854775807")})); // 2^63-1 64 65 function add(x) { 66 return x.foo + x.bar; 67 } 68 print("=== add ===") 69 print(add({foo: 2147483647, bar: 2147483647})); // 2^31 70 print(add({foo: parseLong("9223372036854775807"), bar: parseLong("9223372036854775807")})); // 2^63-1 71 72 function self_add(x) { 73 return x.foo += x.bar; 74 } 75 print("=== self_add ===") 76 print(self_add({foo: 2147483647, bar: 2147483647})); // 2^31 77 print(self_add({foo: parseLong("9223372036854775807"), bar: parseLong("9223372036854775807")})); // 2^63-1