optimistic_check_type.js revision 822:005ac813256a
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-8036987 : Implement tests that checks static types in the compiled code 26 * @test 27 * @run 28 */ 29 30var inspect = Java.type("jdk.nashorn.test.tools.StaticTypeInspector").inspect 31var a=3,b,c,z,y; 32 33// Testing arithmetic operators 34print(inspect(y*z, "undefined value multiplication by undefined value")) 35print(inspect(y/z, "undefined value division by undefined value")) 36 37var x = { a: 2, b:1 } 38print(inspect(x.a*x.b, "int multiplication by int")) 39print(inspect(x.a/x.b, "int division by int without remainder")) 40 41x.a = 7; 42x.b = 2; 43print(inspect(x.a/x.b, "int division by int with remainder")) 44print(inspect(x.a%x.b, "int modulus by int")) 45print(inspect(x.a+x.b, "int plus int")) 46 47x.a = Number.MAX_VALUE; 48x.b = Number.MAX_VALUE; 49print(inspect(x.a*x.b, "max value multiplication by max value")) 50 51x.a = Number.POSITIVE_INFINITY; 52x.b = Number.POSITIVE_INFINITY; 53print(inspect(x.a*x.b, "infinity multiplication by infinity")) 54 55x.a = -1; 56x.b = Number.POSITIVE_INFINITY; 57print(inspect(x.a/x.b, "-1 division by infinity")) 58 59x.a = Number.POSITIVE_INFINITY; 60x.b = 0; 61print(inspect(x.a/x.b, "infinity division by zero")) 62 63x.a = Number.POSITIVE_INFINITY; 64x.b = 'Hello'; 65print(inspect(x.a/x.b, "infinity division by String")) 66 67// Testing nested functions and return value 68function f() { 69 var x = 2, y = 1; 70 function g() { 71 print(inspect(x, "outer local variable")); 72 print(inspect(a, "global variable")); 73 print(inspect(x*y, "outer local variable multiplication by outer local variable")); 74 print(inspect(a*b, "global variable multiplication by global variable undefined")); 75 } 76 g() 77} 78f() 79 80function f1(a,b,c) { 81 d = (a+b) * c; 82 print(inspect(c, "c")); 83 print(inspect(a+b, "a+b")); 84 print(inspect(d, "d")); 85} 86f1() 87 88 89function f2(a,b) { 90 d = a && b; 91 print(inspect(d, "d")); 92} 93f2()