test/script/nosecurity/JDK-8055107.js

Wed, 05 Sep 2018 01:21:35 -0700

author
diazhou
date
Wed, 05 Sep 2018 01:21:35 -0700
changeset 2374
037913b52507
parent 963
e2497b11a021
permissions
-rw-r--r--

Added tag jdk8u192-b09 for changeset 456c0d45c43b

attila@963 1 /*
attila@963 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
attila@963 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
attila@963 4 *
attila@963 5 * This code is free software; you can redistribute it and/or modify it
attila@963 6 * under the terms of the GNU General Public License version 2 only, as
attila@963 7 * published by the Free Software Foundation.
attila@963 8 *
attila@963 9 * This code is distributed in the hope that it will be useful, but WITHOUT
attila@963 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
attila@963 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
attila@963 12 * version 2 for more details (a copy is included in the LICENSE file that
attila@963 13 * accompanied this code).
attila@963 14 *
attila@963 15 * You should have received a copy of the GNU General Public License version
attila@963 16 * 2 along with this work; if not, write to the Free Software Foundation,
attila@963 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
attila@963 18 *
attila@963 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
attila@963 20 * or visit www.oracle.com if you need additional information or have any
attila@963 21 * questions.
attila@963 22 */
attila@963 23
attila@963 24 /**
attila@963 25 * JDK-8055107: Extension directives to turn on callsite profiling, tracing, AST print and other debug features locally
attila@963 26 *
attila@963 27 * @test
attila@963 28 * @option -Dnashorn.debug=true
attila@963 29 * @option -scripting
attila@963 30 * @run
attila@963 31 * @fork
attila@963 32 */
attila@963 33
attila@963 34 function runScriptEngine(code) {
attila@963 35 var imports = new JavaImporter(
attila@963 36 java.io, java.lang, java.util, javax.script);
attila@963 37
attila@963 38 with(imports) {
attila@963 39 var m = new ScriptEngineManager();
attila@963 40 // get current System.err
attila@963 41 var oldErr = System.err;
attila@963 42 var baos = new ByteArrayOutputStream();
attila@963 43 var newErr = new PrintStream(baos);
attila@963 44 try {
attila@963 45 // set new standard err
attila@963 46 System.setErr(newErr);
attila@963 47 var engine = m.getEngineByName("nashorn");
attila@963 48 engine.eval(code);
attila@963 49 newErr.flush();
attila@963 50 return new java.lang.String(baos.toByteArray());
attila@963 51 } finally {
attila@963 52 // restore System.err to old value
attila@963 53 System.setErr(oldErr);
attila@963 54 }
attila@963 55 }
attila@963 56 }
attila@963 57
attila@963 58 // nashorn callsite trace enterexit
attila@963 59 var str = runScriptEngine(<<CODE
attila@963 60 function func() {
attila@963 61 "nashorn callsite trace enterexit";
attila@963 62 k();
attila@963 63 }
attila@963 64
attila@963 65 function k() {
attila@963 66 var x = "hello";
attila@963 67 }
attila@963 68
attila@963 69 func();
attila@963 70 CODE);
attila@963 71
attila@963 72 if (!str.contains(" ENTER ")) {
attila@963 73 fail("expected 'ENTER' in trace mode output");
attila@963 74 }
attila@963 75
attila@963 76 if (!str.contains(" EXIT ")) {
attila@963 77 fail("expected 'EXIT' in trace mode output");
attila@963 78 }
attila@963 79
attila@963 80 // nashorn callsite trace objects
attila@963 81 var str = runScriptEngine(<<CODE
attila@963 82 "nashorn callsite trace objects";
attila@963 83 function func(x) {
attila@963 84 }
attila@963 85
attila@963 86 func("hello");
attila@963 87 CODE);
attila@963 88
attila@963 89 if (!str.contains(" ENTER ")) {
attila@963 90 fail("expected 'ENTER' in trace mode output");
attila@963 91 }
attila@963 92
attila@963 93 if (!str.contains(" EXIT ")) {
attila@963 94 fail("expected 'EXIT' in trace mode output");
attila@963 95 }
attila@963 96
attila@963 97 if (!str.contains("hello")) {
attila@963 98 fail("expected argument to be traced in trace objects mode");
attila@963 99 }
attila@963 100
attila@963 101 // nashorn callsite trace misses
attila@963 102 str = runScriptEngine(<<CODE
attila@963 103 function f() {
attila@963 104 "nashorn callsite trace misses";
attila@963 105 k();
attila@963 106 }
attila@963 107
attila@963 108 function k() {}
attila@963 109 f();
attila@963 110 CODE);
attila@963 111
attila@963 112 if (!str.contains(" MISS ")) {
attila@963 113 fail("expected callsite MISS trace messages");
attila@963 114 }
attila@963 115
attila@963 116 // nashorn print lower ast
attila@963 117 str = runScriptEngine(<<CODE
attila@963 118 function foo() {
attila@963 119 "nashorn print lower ast";
attila@963 120 var x = 'hello';
attila@963 121 }
attila@963 122 foo();
attila@963 123 CODE);
attila@963 124
attila@963 125 if (!str.contains("Lower AST for: 'foo'") ||
attila@963 126 !str.contains("nashorn print lower ast")) {
attila@963 127 fail("expected Lower AST to be printed for 'foo'");
attila@963 128 }
attila@963 129
attila@963 130 // nashorn print ast
attila@963 131 str = runScriptEngine(<<CODE
attila@963 132 function foo() {
attila@963 133 "nashorn print ast";
attila@963 134 }
attila@963 135 CODE);
attila@963 136 if (!str.contains("[function ") ||
attila@963 137 !str.contains("nashorn print ast")) {
attila@963 138 fail("expected AST to be printed");
attila@963 139 }
attila@963 140
attila@963 141 // nashorn print symbols
attila@963 142 str = runScriptEngine(<<CODE
attila@963 143 function bar(a) {
attila@963 144 "nashorn print symbols";
attila@963 145 if (a) print(a);
attila@963 146 }
attila@963 147
attila@963 148 bar();
attila@963 149 CODE)
attila@963 150
attila@963 151 if (!str.contains("[BLOCK in 'Function bar']")) {
attila@963 152 fail("expected symbols to be printed for 'bar'");
attila@963 153 }
attila@963 154
attila@963 155 // nashorn print parse
attila@963 156 str = runScriptEngine(<<CODE
attila@963 157 "nashorn print parse";
attila@963 158
attila@963 159 function func() {}
attila@963 160 CODE);
attila@963 161
attila@963 162 if (!str.contains("function func") ||
attila@963 163 !str.contains("nashorn print parse")) {
attila@963 164 fail("expected nashorn print parse output");
attila@963 165 }
attila@963 166
attila@963 167 // nashorn print lower parse
attila@963 168 str = runScriptEngine(<<CODE
attila@963 169 "nashorn print lower parse";
attila@963 170
attila@963 171 function func() {}
attila@963 172
attila@963 173 func()
attila@963 174 CODE);
attila@963 175
attila@963 176 if (!str.contains("function {U%}func") ||
attila@963 177 !str.contains("nashorn print lower parse")) {
attila@963 178 fail("expected nashorn print lower parse output");
attila@963 179 }

mercurial