Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/test/script/nosecurity/JDK-8055107.js
32281 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* JDK-8055107: Extension directives to turn on callsite profiling, tracing, AST print and other debug features locally25*26* @test27* @option -Dnashorn.debug=true28* @option -scripting29* @run30* @fork31*/3233function runScriptEngine(code) {34var imports = new JavaImporter(35java.io, java.lang, java.util, javax.script);3637with(imports) {38var m = new ScriptEngineManager();39// get current System.err40var oldErr = System.err;41var baos = new ByteArrayOutputStream();42var newErr = new PrintStream(baos);43try {44// set new standard err45System.setErr(newErr);46var engine = m.getEngineByName("nashorn");47engine.eval(code);48newErr.flush();49return new java.lang.String(baos.toByteArray());50} finally {51// restore System.err to old value52System.setErr(oldErr);53}54}55}5657// nashorn callsite trace enterexit58var str = runScriptEngine(<<CODE59function func() {60"nashorn callsite trace enterexit";61k();62}6364function k() {65var x = "hello";66}6768func();69CODE);7071if (!str.contains(" ENTER ")) {72fail("expected 'ENTER' in trace mode output");73}7475if (!str.contains(" EXIT ")) {76fail("expected 'EXIT' in trace mode output");77}7879// nashorn callsite trace objects80var str = runScriptEngine(<<CODE81"nashorn callsite trace objects";82function func(x) {83}8485func("hello");86CODE);8788if (!str.contains(" ENTER ")) {89fail("expected 'ENTER' in trace mode output");90}9192if (!str.contains(" EXIT ")) {93fail("expected 'EXIT' in trace mode output");94}9596if (!str.contains("hello")) {97fail("expected argument to be traced in trace objects mode");98}99100// nashorn callsite trace misses101str = runScriptEngine(<<CODE102function f() {103"nashorn callsite trace misses";104k();105}106107function k() {}108f();109CODE);110111if (!str.contains(" MISS ")) {112fail("expected callsite MISS trace messages");113}114115// nashorn print lower ast116str = runScriptEngine(<<CODE117function foo() {118"nashorn print lower ast";119var x = 'hello';120}121foo();122CODE);123124if (!str.contains("Lower AST for: 'foo'") ||125!str.contains("nashorn print lower ast")) {126fail("expected Lower AST to be printed for 'foo'");127}128129// nashorn print ast130str = runScriptEngine(<<CODE131function foo() {132"nashorn print ast";133}134CODE);135if (!str.contains("[function ") ||136!str.contains("nashorn print ast")) {137fail("expected AST to be printed");138}139140// nashorn print symbols141str = runScriptEngine(<<CODE142function bar(a) {143"nashorn print symbols";144if (a) print(a);145}146147bar();148CODE)149150if (!str.contains("[BLOCK in 'Function bar']")) {151fail("expected symbols to be printed for 'bar'");152}153154// nashorn print parse155str = runScriptEngine(<<CODE156"nashorn print parse";157158function func() {}159CODE);160161if (!str.contains("function func") ||162!str.contains("nashorn print parse")) {163fail("expected nashorn print parse output");164}165166// nashorn print lower parse167str = runScriptEngine(<<CODE168"nashorn print lower parse";169170function func() {}171172func()173CODE);174175if (!str.contains("function {U%}func") ||176!str.contains("nashorn print lower parse")) {177fail("expected nashorn print lower parse output");178}179180181