JDK-8055107.js revision 969:6831ff454574
1115170Speter/*
299127Sobrien * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
399127Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
499127Sobrien *
599127Sobrien * This code is free software; you can redistribute it and/or modify it
699127Sobrien * under the terms of the GNU General Public License version 2 only, as
799127Sobrien * published by the Free Software Foundation.
899127Sobrien *
999127Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1099127Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1199127Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12115170Speter * version 2 for more details (a copy is included in the LICENSE file that
1399127Sobrien * accompanied this code).
1499127Sobrien *
1599127Sobrien * You should have received a copy of the GNU General Public License version
16115170Speter * 2 along with this work; if not, write to the Free Software Foundation,
1799127Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1899127Sobrien *
1999127Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2099127Sobrien * or visit www.oracle.com if you need additional information or have any
21169612Swkoszek * questions.
2299127Sobrien */
2399127Sobrien
2499127Sobrien/**
2599127Sobrien * JDK-8055107: Extension directives to turn on callsite profiling, tracing, AST print and other debug features locally
2699127Sobrien *
2799127Sobrien * @test
2899127Sobrien * @option -Dnashorn.debug=true
2999127Sobrien * @option -scripting
3099127Sobrien * @run
3199127Sobrien * @fork
3299127Sobrien */
3399127Sobrien
34250840Smarcelfunction runScriptEngine(code) {
35250840Smarcel    var imports = new JavaImporter(
36179225Sjb        java.io, java.lang, java.util, javax.script);
37115405Speter
38115405Speter    with(imports) {
39115405Speter        var m = new ScriptEngineManager();
40114370Speter        // get current System.err
4199127Sobrien        var oldErr = System.err;
42218893Sdim        var baos = new ByteArrayOutputStream();
43232263Sdim        var newErr = new PrintStream(baos);
44232263Sdim        try {
45232263Sdim            // set new standard err
46232263Sdim            System.setErr(newErr);
47232263Sdim            var engine = m.getEngineByName("nashorn");
48218893Sdim            engine.eval(code);
4999127Sobrien            newErr.flush();
5099127Sobrien            return new java.lang.String(baos.toByteArray());
5199127Sobrien        } finally {
5299127Sobrien            // restore System.err to old value
5399127Sobrien            System.setErr(oldErr);
5499127Sobrien        }
5599127Sobrien    }
5699127Sobrien}
5799127Sobrien
5899127Sobrien// nashorn callsite trace enterexit
5999127Sobrienvar str = runScriptEngine(<<CODE
6099127Sobrienfunction func() {
6199127Sobrien   "nashorn callsite trace enterexit";
6299127Sobrien   k();
6399127Sobrien}
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