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 34function 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 59var str = runScriptEngine(<<CODE 60function func() { 61 "nashorn callsite trace enterexit"; 62 k(); 63} 64 65function k() { 66 var x = "hello"; 67} 68 69func(); 70CODE); 71 72if (!str.contains(" ENTER ")) { 73 fail("expected 'ENTER' in trace mode output"); 74} 75 76if (!str.contains(" EXIT ")) { 77 fail("expected 'EXIT' in trace mode output"); 78} 79 80// nashorn callsite trace objects 81var str = runScriptEngine(<<CODE 82"nashorn callsite trace objects"; 83function func(x) { 84} 85 86func("hello"); 87CODE); 88 89if (!str.contains(" ENTER ")) { 90 fail("expected 'ENTER' in trace mode output"); 91} 92 93if (!str.contains(" EXIT ")) { 94 fail("expected 'EXIT' in trace mode output"); 95} 96 97if (!str.contains("hello")) { 98 fail("expected argument to be traced in trace objects mode"); 99} 100 101// nashorn callsite trace misses 102str = runScriptEngine(<<CODE 103function f() { 104 "nashorn callsite trace misses"; 105 k(); 106} 107 108function k() {} 109f(); 110CODE); 111 112if (!str.contains(" MISS ")) { 113 fail("expected callsite MISS trace messages"); 114} 115 116// nashorn print lower ast 117str = runScriptEngine(<<CODE 118function foo() { 119 "nashorn print lower ast"; 120 var x = 'hello'; 121} 122foo(); 123CODE); 124 125if (!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 131str = runScriptEngine(<<CODE 132function foo() { 133 "nashorn print ast"; 134} 135CODE); 136if (!str.contains("[function ") || 137 !str.contains("nashorn print ast")) { 138 fail("expected AST to be printed"); 139} 140 141// nashorn print symbols 142str = runScriptEngine(<<CODE 143function bar(a) { 144 "nashorn print symbols"; 145 if (a) print(a); 146} 147 148bar(); 149CODE) 150 151if (!str.contains("[BLOCK in 'Function bar']")) { 152 fail("expected symbols to be printed for 'bar'"); 153} 154 155// nashorn print parse 156str = runScriptEngine(<<CODE 157"nashorn print parse"; 158 159function func() {} 160CODE); 161 162if (!str.contains("function func") || 163 !str.contains("nashorn print parse")) { 164 fail("expected nashorn print parse output"); 165} 166 167// nashorn print lower parse 168str = runScriptEngine(<<CODE 169"nashorn print lower parse"; 170 171function func() {} 172 173func() 174CODE); 175 176if (!str.contains("function {U%}func") || 177 !str.contains("nashorn print lower parse")) { 178 fail("expected nashorn print lower parse output"); 179} 180