Mon, 17 Nov 2014 12:40:54 -0800
Added tag jdk8u31-b09 for changeset 762eaacc45ce
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 var keyField;
41 for each (var f in valueDescFields) {
42 if (f.name == "key") {
43 keyField = f;
44 }
45 f.accessible = true;
46 }
48 var debuggerSupportMethods = DebuggerSupport.class.declaredMethods;
50 // methods of DebuggerSupport that we use
51 var evalMethod, valueInfoMethod, valueInfosMethod;
52 var getSourceInfoMethod, valueAsStringMethod;
54 for each (var m in debuggerSupportMethods) {
55 m.accessible = true;
56 switch (m.name) {
57 case "eval":
58 evalMethod = m;
59 break;
60 case "valueInfo":
61 if (m.parameterCount == 3) {
62 valueInfoMethod = m;
63 }
64 break;
65 case "valueInfos":
66 valueInfosMethod = m;
67 break;
68 case "valueAsString":
69 valueAsStringMethod = m;
70 break;
71 case "getSourceInfo":
72 getSourceInfoMethod = m;
73 break;
74 }
75 }
77 // eval
78 var value = evalMethod.invoke(null, null, null, "33 + 55", false);
79 print(value);
81 // valueInfo
82 var info = valueInfoMethod.invoke(null, "apply", Function, true);
83 for each (var f in valueDescFields) {
84 print(f.name, "=", f.get(info));
85 }
87 // valueInfo - user defined object
88 var info = valueInfoMethod.invoke(null, "foo", { foo: 343 }, true);
89 for each (var f in valueDescFields) {
90 print(f.name, "=", f.get(info));
91 }
93 // valueInfos
94 var infos = valueInfosMethod.invoke(null, Object, true);
95 Arrays.sort(infos, function (i1, i2) keyField.get(i1).compareTo(keyField.get(i2)));
97 for each (var info in infos) {
98 for each (var f in valueDescFields) {
99 print(f.name, "=", f.get(info));
100 }
101 }
103 // valueInfos - user defined object
104 var infos = valueInfosMethod.invoke(null, { foo: 34, bar: "hello" }, true);
105 Arrays.sort(infos, function (i1, i2) keyField.get(i1).compareTo(keyField.get(i2)));
107 for each (var info in infos) {
108 for each (var f in valueDescFields) {
109 print(f.name, "=", f.get(info));
110 }
111 }
113 // valueAsString
114 function printValue(value) {
115 print(valueAsStringMethod.invoke(null, value));
116 }
118 printValue(undefined);
119 printValue(null);
120 printValue("hello");
121 printValue(Math.PI);
122 printValue(this);
124 // The below are not part of DebuggerSupport. But we need these to
125 // test DebuggerSupport.getSourceInfo etc. which need compiled script class
127 var Source = Java.type("jdk.nashorn.internal.runtime.Source");
128 var Context = Java.type("jdk.nashorn.internal.runtime.Context");
129 var sourceCls = Source.class;
130 var errorMgrCls = Java.type("jdk.nashorn.internal.runtime.ErrorManager").class;
131 var booleanCls = Java.type("java.lang.Boolean").TYPE;
133 // private compile method of Context class
134 var compileMethod = Context.class.getDeclaredMethod("compile",
135 sourceCls, errorMgrCls, booleanCls);
136 compileMethod.accessible = true;
138 var scriptCls = compileMethod.invoke(Context.context,
139 Source.sourceFor("test", "print('hello')"),
140 new Context.ThrowErrorManager(), false);
142 var SCRIPT_CLASS_NAME_PREFIX = "jdk.nashorn.internal.scripts.Script$";
143 print("script class name pattern satisfied? " +
144 scriptCls.name.startsWith(SCRIPT_CLASS_NAME_PREFIX));
146 var srcInfo = getSourceInfoMethod.invoke(null, scriptCls);
147 var srcInfoFields = srcInfo.class.declaredFields;
148 Arrays.sort(srcInfoFields, function(f1, f2) f1.name.compareTo(f2.name));
150 print("Source info");
151 for each (var f in srcInfoFields) {
152 f.accessible = true;
153 var fieldValue = f.get(srcInfo);
154 if (fieldValue instanceof CharArray) {
155 fieldValue = new java.lang.String(fieldValue);
156 }
158 print(f.name, "=", fieldValue);
159 }