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