Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/test/script/nosecurity/JDK-8044798.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-8044798: API for debugging Nashorn25*26* @test27* @option -Dnashorn.mirror.always=false28* @fork29* @run30*/3132// basic API exercise checks3334var Arrays = Java.type("java.util.Arrays");35var CharArray = Java.type("char[]");36var DebuggerSupport = Java.type("jdk.nashorn.internal.runtime.DebuggerSupport");37var DebuggerValueDesc = Java.type("jdk.nashorn.internal.runtime.DebuggerSupport.DebuggerValueDesc");3839var valueDescFields = DebuggerValueDesc.class.declaredFields;40Arrays.sort(valueDescFields, function(f1, f2) f1.name.compareTo(f2.name));41for each (var f in valueDescFields) {42f.accessible = true;43}4445var debuggerSupportMethods = DebuggerSupport.class.declaredMethods;4647// methods of DebuggerSupport that we use48var evalMethod, valueInfoMethod, valueInfosMethod;49var getSourceInfoMethod, valueAsStringMethod;5051for each (var m in debuggerSupportMethods) {52m.accessible = true;53switch (m.name) {54case "eval":55evalMethod = m;56break;57case "valueInfo":58if (m.parameterCount == 3) {59valueInfoMethod = m;60}61break;62case "valueInfos":63valueInfosMethod = m;64break;65case "valueAsString":66valueAsStringMethod = m;67break;68case "getSourceInfo":69getSourceInfoMethod = m;70break;71}72}7374// eval75var value = evalMethod.invoke(null, null, null, "33 + 55", false);76print(value);7778// valueInfo79var info = valueInfoMethod.invoke(null, "apply", Function, true);80for each (var f in valueDescFields) {81print(f.name, "=", f.get(info));82}8384// valueInfo - user defined object85var info = valueInfoMethod.invoke(null, "foo", { foo: 343 }, true);86for each (var f in valueDescFields) {87print(f.name, "=", f.get(info));88}8990// valueInfos91var infos = valueInfosMethod.invoke(null, Object, true);92for each (var info in infos) {93for each (var f in valueDescFields) {94print(f.name, "=", f.get(info));95}96}9798// valueInfos - user defined object99var infos = valueInfosMethod.invoke(null, { foo: 34, bar: "hello" }, true);100for each (var info in infos) {101for each (var f in valueDescFields) {102print(f.name, "=", f.get(info));103}104}105106// valueAsString107function printValue(value) {108print(valueAsStringMethod.invoke(null, value));109}110111printValue(undefined);112printValue(null);113printValue("hello");114printValue(Math.PI);115printValue(this);116117// The below are not part of DebuggerSupport. But we need these to118// test DebuggerSupport.getSourceInfo etc. which need compiled script class119120var Source = Java.type("jdk.nashorn.internal.runtime.Source");121var Context = Java.type("jdk.nashorn.internal.runtime.Context");122var sourceCls = Source.class;123var errorMgrCls = Java.type("jdk.nashorn.internal.runtime.ErrorManager").class;124var booleanCls = Java.type("java.lang.Boolean").TYPE;125126// private compile method of Context class127var compileMethod = Context.class.getDeclaredMethod("compile",128sourceCls, errorMgrCls, booleanCls);129compileMethod.accessible = true;130131var scriptCls = compileMethod.invoke(Context.context,132Source.sourceFor("test", "print('hello')"),133new Context.ThrowErrorManager(), false);134135var SCRIPT_CLASS_NAME_PREFIX = "jdk.nashorn.internal.scripts.Script$";136print("script class name pattern satisfied? " +137scriptCls.name.startsWith(SCRIPT_CLASS_NAME_PREFIX));138139var srcInfo = getSourceInfoMethod.invoke(null, scriptCls);140var srcInfoFields = srcInfo.class.declaredFields;141Arrays.sort(srcInfoFields, function(f1, f2) f1.name.compareTo(f2.name));142143print("Source info");144for each (var f in srcInfoFields) {145f.accessible = true;146var fieldValue = f.get(srcInfo);147if (fieldValue instanceof CharArray) {148fieldValue = new java.lang.String(fieldValue);149}150151print(f.name, "=", fieldValue);152}153154155