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  * @test
  26  * @bug 8036987, 8037572
  27  * @summary Implement tests that checks static types in the compiled code
  28  * @option --optimistic-types=true
  29  * @run
  30  */
  31 
  32 var inspect = Java.type("jdk.nashorn.test.tools.StaticTypeInspector").inspect
  33 var a=3, b=2.3, c=true, d;
  34 var x = { a: 2, b:0, c:undefined}
  35 var trees = new Array("redwood", "bay", "cedar", "oak");
  36 
  37 // Testing conditional operator
  38 print(inspect("" ? b : x.a, "ternary operator"))
  39 var b1 = b;
  40 print(inspect(x.b ? b1 : x.a, "ternary operator"))
  41 var b2 = b;
  42 print(inspect(c ? b2 : a, "ternary operator"))
  43 var b3 = b;
  44 print(inspect(!c ? b3 : a, "ternary operator"))
  45 var b4 = b;
  46 print(inspect(d ? b4 : x.c, "ternary operator"))
  47 print(inspect(x.c ? a : c, "ternary operator"))
  48 print(inspect(c ? d : a, "ternary operator"))
  49 var b5 = b;
  50 print(inspect(c ? +a : b5, "ternary operator"))
  51 
  52 // Testing format methods
  53 print(inspect(b.toFixed(2), "global double toFixed()"))
  54 print(inspect(b.toPrecision(2)/1, "global double toPrecision() divided by 1"))
  55 print(inspect(b.toExponential(2), "global double toExponential()"))
  56 
  57 // Testing arrays
  58 print(inspect(trees[1], "member object"))
  59 trees[1] = undefined;
  60 print(inspect(trees[1], "member undefined"))
  61 var b6=b;
  62 print(inspect(1 in trees ? b6 : a, "conditional on array member"))
  63 delete trees[2]
  64 var b7=b;
  65 print(inspect(2 in trees ? b7 : a, "conditional on array member"))
  66 print(inspect(3 in trees ? trees[2]="bay" : a, "conditional on array member"))
  67 var b8=b;
  68 print(inspect("oak" in trees ? b8 : a, "conditional on array member"))
  69 
  70 // Testing nested functions and return value
  71 function f1() {
  72     var x = 2, y = 1;
  73     function g() {
  74         print(inspect(x, "outer local variable"));
  75         print(inspect(a, "global variable"));
  76         print(inspect(x*y, "outer local int multiplication by outer local int"));
  77         print(inspect(a*d, "global int multiplication by global undefined"));
  78     }
  79     g()
  80 }
  81 f1()
  82 
  83 function f2(a,b,c) {
  84     g = (a+b) * c;
  85     print(inspect(c, "local undefined"));
  86     print(inspect(a+b, "local undefined addition local undefined"));
  87     print(inspect(g, "local undefined multiplication by undefined"));
  88 }
  89 f2()
  90 
  91 function f3(a,b) {
  92     g = a && b;
  93     print(inspect(g, "local undefined AND local undefined"));
  94     print(inspect(c||g, "global true OR local undefined"));
  95 }
  96 f3()
  97 
  98 function f4() {
  99     var x = true, y = 0;
 100     function g() {
 101         print(inspect(x+y, "outer local true addition local int"));
 102         print(inspect(a+x, "global int addition outer local true"));
 103         print(inspect(x*y, "outer local true multiplication by outer local int"));
 104         print(inspect(y*d, "outer local int multiplication by global undefined"));
 105     }
 106     g()
 107 }
 108 f4()