test/script/nosecurity/JDK-8055107.js

changeset 963
e2497b11a021
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/script/nosecurity/JDK-8055107.js	Wed Aug 20 10:26:01 2014 +0200
     1.3 @@ -0,0 +1,179 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * JDK-8055107: Extension directives to turn on callsite profiling, tracing, AST print and other debug features locally
    1.29 + *
    1.30 + * @test
    1.31 + * @option -Dnashorn.debug=true
    1.32 + * @option -scripting
    1.33 + * @run
    1.34 + * @fork
    1.35 + */
    1.36 +
    1.37 +function runScriptEngine(code) {
    1.38 +    var imports = new JavaImporter(
    1.39 +        java.io, java.lang, java.util, javax.script);
    1.40 +
    1.41 +    with(imports) {
    1.42 +        var m = new ScriptEngineManager();
    1.43 +        // get current System.err
    1.44 +        var oldErr = System.err;
    1.45 +        var baos = new ByteArrayOutputStream();
    1.46 +        var newErr = new PrintStream(baos);
    1.47 +        try {
    1.48 +            // set new standard err
    1.49 +            System.setErr(newErr);
    1.50 +            var engine = m.getEngineByName("nashorn");
    1.51 +            engine.eval(code);
    1.52 +            newErr.flush();
    1.53 +            return new java.lang.String(baos.toByteArray());
    1.54 +        } finally {
    1.55 +            // restore System.err to old value
    1.56 +            System.setErr(oldErr);
    1.57 +        }
    1.58 +    }
    1.59 +}
    1.60 +
    1.61 +// nashorn callsite trace enterexit
    1.62 +var str = runScriptEngine(<<CODE
    1.63 +function func() {
    1.64 +   "nashorn callsite trace enterexit";
    1.65 +   k();
    1.66 +}
    1.67 +
    1.68 +function k() {
    1.69 +    var x = "hello";
    1.70 +}
    1.71 +
    1.72 +func();
    1.73 +CODE);
    1.74 +
    1.75 +if (!str.contains(" ENTER ")) {
    1.76 +    fail("expected 'ENTER' in trace mode output");
    1.77 +}
    1.78 +
    1.79 +if (!str.contains(" EXIT ")) {
    1.80 +    fail("expected 'EXIT' in trace mode output");
    1.81 +}
    1.82 +
    1.83 +// nashorn callsite trace objects
    1.84 +var str = runScriptEngine(<<CODE
    1.85 +"nashorn callsite trace objects";
    1.86 +function func(x) {
    1.87 +}
    1.88 +
    1.89 +func("hello");
    1.90 +CODE);
    1.91 +
    1.92 +if (!str.contains(" ENTER ")) {
    1.93 +    fail("expected 'ENTER' in trace mode output");
    1.94 +}
    1.95 +
    1.96 +if (!str.contains(" EXIT ")) {
    1.97 +    fail("expected 'EXIT' in trace mode output");
    1.98 +}
    1.99 +
   1.100 +if (!str.contains("hello")) {
   1.101 +    fail("expected argument to be traced in trace objects mode");
   1.102 +}
   1.103 +
   1.104 +// nashorn callsite trace misses
   1.105 +str = runScriptEngine(<<CODE
   1.106 +function f() {
   1.107 +   "nashorn callsite trace misses";
   1.108 +   k();
   1.109 +}
   1.110 +
   1.111 +function k() {}
   1.112 +f();
   1.113 +CODE);
   1.114 +
   1.115 +if (!str.contains(" MISS ")) {
   1.116 +    fail("expected callsite MISS trace messages");
   1.117 +}
   1.118 +
   1.119 +// nashorn print lower ast
   1.120 +str = runScriptEngine(<<CODE
   1.121 +function foo() {
   1.122 +    "nashorn print lower ast";
   1.123 +    var x = 'hello';
   1.124 +}
   1.125 +foo();
   1.126 +CODE);
   1.127 +
   1.128 +if (!str.contains("Lower AST for: 'foo'") ||
   1.129 +    !str.contains("nashorn print lower ast")) {
   1.130 +    fail("expected Lower AST to be printed for 'foo'");
   1.131 +}
   1.132 +
   1.133 +// nashorn print ast
   1.134 +str = runScriptEngine(<<CODE
   1.135 +function foo() {
   1.136 +  "nashorn print ast";
   1.137 +}
   1.138 +CODE);
   1.139 +if (!str.contains("[function ") ||
   1.140 +    !str.contains("nashorn print ast")) {
   1.141 +    fail("expected AST to be printed");
   1.142 +}
   1.143 +
   1.144 +// nashorn print symbols
   1.145 +str = runScriptEngine(<<CODE
   1.146 +function bar(a) {
   1.147 +    "nashorn print symbols";
   1.148 +    if (a) print(a);
   1.149 +}
   1.150 +
   1.151 +bar();
   1.152 +CODE)
   1.153 +
   1.154 +if (!str.contains("[BLOCK in 'Function bar']")) {
   1.155 +    fail("expected symbols to be printed for 'bar'");
   1.156 +}
   1.157 +
   1.158 +// nashorn print parse
   1.159 +str = runScriptEngine(<<CODE
   1.160 +"nashorn print parse";
   1.161 +
   1.162 +function func() {}
   1.163 +CODE);
   1.164 +
   1.165 +if (!str.contains("function func") ||
   1.166 +    !str.contains("nashorn print parse")) {
   1.167 +    fail("expected nashorn print parse output");
   1.168 +}
   1.169 +
   1.170 +// nashorn print lower parse
   1.171 +str = runScriptEngine(<<CODE
   1.172 +"nashorn print lower parse";
   1.173 +
   1.174 +function func() {}
   1.175 +
   1.176 +func()
   1.177 +CODE);
   1.178 +
   1.179 +if (!str.contains("function {U%}func") ||
   1.180 +    !str.contains("nashorn print lower parse")) {
   1.181 +    fail("expected nashorn print lower parse output");
   1.182 +}

mercurial