Wed, 01 Oct 2014 07:47:24 -0700
Added tag jdk8u40-b08 for changeset 89551828b279
1 /*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /**
25 * JDK-8044798: API for debugging Nashorn
26 *
27 * @test
28 * @run
29 */
31 // basic API exercise checks
33 var Arrays = Java.type("java.util.Arrays");
34 var CharArray = Java.type("char[]");
35 var DebuggerSupport = Java.type("jdk.nashorn.internal.runtime.DebuggerSupport");
36 var DebuggerValueDesc = Java.type("jdk.nashorn.internal.runtime.DebuggerSupport.DebuggerValueDesc");
38 var valueDescFields = DebuggerValueDesc.class.declaredFields;
39 Arrays.sort(valueDescFields, function(f1, f2) f1.name.compareTo(f2.name));
40 for each (var f in valueDescFields) {
41 f.accessible = true;
42 }
44 var debuggerSupportMethods = DebuggerSupport.class.declaredMethods;
46 // methods of DebuggerSupport that we use
47 var evalMethod, valueInfoMethod, valueInfosMethod;
48 var getSourceInfoMethod, valueAsStringMethod;
50 for each (var m in debuggerSupportMethods) {
51 m.accessible = true;
52 switch (m.name) {
53 case "eval":
54 evalMethod = m;
55 break;
56 case "valueInfo":
57 if (m.parameterCount == 3) {
58 valueInfoMethod = m;
59 }
60 break;
61 case "valueInfos":
62 valueInfosMethod = m;
63 break;
64 case "valueAsString":
65 valueAsStringMethod = m;
66 break;
67 case "getSourceInfo":
68 getSourceInfoMethod = m;
69 break;
70 }
71 }
73 // eval
74 var value = evalMethod.invoke(null, null, null, "33 + 55", false);
75 print(value);
77 // valueInfo
78 var info = valueInfoMethod.invoke(null, "apply", Function, true);
79 for each (var f in valueDescFields) {
80 print(f.name, "=", f.get(info));
81 }
83 // valueInfo - user defined object
84 var info = valueInfoMethod.invoke(null, "foo", { foo: 343 }, true);
85 for each (var f in valueDescFields) {
86 print(f.name, "=", f.get(info));
87 }
89 // valueInfos
90 var infos = valueInfosMethod.invoke(null, Object, true);
91 for each (var info in infos) {
92 for each (var f in valueDescFields) {
93 print(f.name, "=", f.get(info));
94 }
95 }
97 // valueInfos - user defined object
98 var infos = valueInfosMethod.invoke(null, { foo: 34, bar: "hello" }, true);
99 for each (var info in infos) {
100 for each (var f in valueDescFields) {
101 print(f.name, "=", f.get(info));
102 }
103 }
105 // valueAsString
106 function printValue(value) {
107 print(valueAsStringMethod.invoke(null, value));
108 }
110 printValue(undefined);
111 printValue(null);
112 printValue("hello");
113 printValue(Math.PI);
114 printValue(this);
116 // The below are not part of DebuggerSupport. But we need these to
117 // test DebuggerSupport.getSourceInfo etc. which need compiled script class
119 var Source = Java.type("jdk.nashorn.internal.runtime.Source");
120 var Context = Java.type("jdk.nashorn.internal.runtime.Context");
121 var sourceCls = Source.class;
122 var errorMgrCls = Java.type("jdk.nashorn.internal.runtime.ErrorManager").class;
123 var booleanCls = Java.type("java.lang.Boolean").TYPE;
125 // private compile method of Context class
126 var compileMethod = Context.class.getDeclaredMethod("compile",
127 sourceCls, errorMgrCls, booleanCls);
128 compileMethod.accessible = true;
130 var scriptCls = compileMethod.invoke(Context.context,
131 Source.sourceFor("test", "print('hello')"),
132 new Context.ThrowErrorManager(), false);
134 var SCRIPT_CLASS_NAME_PREFIX = "jdk.nashorn.internal.scripts.Script$";
135 print("script class name pattern satisfied? " +
136 scriptCls.name.startsWith(SCRIPT_CLASS_NAME_PREFIX));
138 var srcInfo = getSourceInfoMethod.invoke(null, scriptCls);
139 var srcInfoFields = srcInfo.class.declaredFields;
140 Arrays.sort(srcInfoFields, function(f1, f2) f1.name.compareTo(f2.name));
142 print("Source info");
143 for each (var f in srcInfoFields) {
144 f.accessible = true;
145 var fieldValue = f.get(srcInfo);
146 if (fieldValue instanceof CharArray) {
147 fieldValue = new java.lang.String(fieldValue);
148 }
150 print(f.name, "=", fieldValue);
151 }