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-8055107: Extension directives to turn on callsite profiling, tracing, AST print and other debug features locally
  26  *
  27  * @test
  28  * @option -Dnashorn.debug=true
  29  * @option -scripting
  30  * @run
  31  * @fork
  32  */
  33 
  34 function runScriptEngine(code) {
  35     var imports = new JavaImporter(
  36         java.io, java.lang, java.util, javax.script);
  37 
  38     with(imports) {
  39         var m = new ScriptEngineManager();
  40         // get current System.err
  41         var oldErr = System.err;
  42         var baos = new ByteArrayOutputStream();
  43         var newErr = new PrintStream(baos);
  44         try {
  45             // set new standard err
  46             System.setErr(newErr);
  47             var engine = m.getEngineByName("nashorn");
  48             engine.eval(code);
  49             newErr.flush();
  50             return new java.lang.String(baos.toByteArray());
  51         } finally {
  52             // restore System.err to old value
  53             System.setErr(oldErr);
  54         }
  55     }
  56 }
  57 
  58 // nashorn callsite trace enterexit
  59 var str = runScriptEngine(<<CODE
  60 function func() {
  61    "nashorn callsite trace enterexit";
  62    k();
  63 }
  64 
  65 function k() {
  66     var x = "hello";
  67 }
  68 
  69 func();
  70 CODE);
  71 
  72 if (!str.contains(" ENTER ")) {
  73     fail("expected 'ENTER' in trace mode output");
  74 }
  75 
  76 if (!str.contains(" EXIT ")) {
  77     fail("expected 'EXIT' in trace mode output");
  78 }
  79 
  80 // nashorn callsite trace objects
  81 var str = runScriptEngine(<<CODE
  82 "nashorn callsite trace objects";
  83 function func(x) {
  84 }
  85 
  86 func("hello");
  87 CODE);
  88 
  89 if (!str.contains(" ENTER ")) {
  90     fail("expected 'ENTER' in trace mode output");
  91 }
  92 
  93 if (!str.contains(" EXIT ")) {
  94     fail("expected 'EXIT' in trace mode output");
  95 }
  96 
  97 if (!str.contains("hello")) {
  98     fail("expected argument to be traced in trace objects mode");
  99 }
 100 
 101 // nashorn callsite trace misses
 102 str = runScriptEngine(<<CODE
 103 function f() {
 104    "nashorn callsite trace misses";
 105    k();
 106 }
 107 
 108 function k() {}
 109 f();
 110 CODE);
 111 
 112 if (!str.contains(" MISS ")) {
 113     fail("expected callsite MISS trace messages");
 114 }
 115 
 116 // nashorn print lower ast
 117 str = runScriptEngine(<<CODE
 118 function foo() {
 119     "nashorn print lower ast";
 120     var x = 'hello';
 121 }
 122 foo();
 123 CODE);
 124 
 125 if (!str.contains("Lower AST for: 'foo'") ||
 126     !str.contains("nashorn print lower ast")) {
 127     fail("expected Lower AST to be printed for 'foo'");
 128 }
 129 
 130 // nashorn print ast
 131 str = runScriptEngine(<<CODE
 132 function foo() {
 133   "nashorn print ast";
 134 }
 135 CODE);
 136 if (!str.contains("[function ") ||
 137     !str.contains("nashorn print ast")) {
 138     fail("expected AST to be printed");
 139 }
 140 
 141 // nashorn print symbols
 142 str = runScriptEngine(<<CODE
 143 function bar(a) {
 144     "nashorn print symbols";
 145     if (a) print(a);
 146 }
 147 
 148 bar();
 149 CODE)
 150 
 151 if (!str.contains("[BLOCK in 'Function bar']")) {
 152     fail("expected symbols to be printed for 'bar'");
 153 }
 154 
 155 // nashorn print parse
 156 str = runScriptEngine(<<CODE
 157 "nashorn print parse";
 158 
 159 function func() {}
 160 CODE);
 161 
 162 if (!str.contains("function func") ||
 163     !str.contains("nashorn print parse")) {
 164     fail("expected nashorn print parse output");
 165 }
 166 
 167 // nashorn print lower parse
 168 str = runScriptEngine(<<CODE
 169 "nashorn print lower parse";
 170 
 171 function func() {}
 172 
 173 func()
 174 CODE);
 175 
 176 if (!str.contains("function {U%}func") ||
 177     !str.contains("nashorn print lower parse")) {
 178     fail("expected nashorn print lower parse output");
 179 }