JDK-8044798.js revision 1829:dbe6b239d681
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 */
23
24/**
25 * JDK-8044798: API for debugging Nashorn
26 *
27 * @test
28 * @option -Dnashorn.mirror.always=false
29 * @fork
30 */
31
32// basic API exercise checks
33
34var Arrays = Java.type("java.util.Arrays");
35var CharArray = Java.type("char[]");
36var Reflector = Java.type("jdk.nashorn.test.models.Reflector");
37var DebuggerSupport = Java.type("jdk.nashorn.internal.runtime.DebuggerSupport");
38var DebuggerValueDesc = Java.type("jdk.nashorn.internal.runtime.DebuggerSupport.DebuggerValueDesc");
39
40var valueDescFields = DebuggerValueDesc.class.declaredFields;
41Arrays.sort(valueDescFields, function(f1, f2) f1.name.compareTo(f2.name));
42for each (var f in valueDescFields) {
43    Reflector.setAccessible(f);
44}
45
46var debuggerSupportMethods = DebuggerSupport.class.declaredMethods;
47
48// methods of DebuggerSupport that we use
49var evalMethod, valueInfoMethod, valueInfosMethod;
50var getSourceInfoMethod, valueAsStringMethod;
51
52for each (var m in debuggerSupportMethods) {
53    Reflector.setAccessible(m);
54    switch (m.name) {
55        case "eval":
56            evalMethod = m;
57            break;
58        case "valueInfo":
59            if (m.parameterCount == 3) {
60                valueInfoMethod = m;
61            }
62            break;
63        case "valueInfos":
64            valueInfosMethod = m;
65            break;
66        case "valueAsString":
67            valueAsStringMethod = m;
68            break;
69        case "getSourceInfo":
70            getSourceInfoMethod = m;
71            break;
72    }
73}
74
75// eval
76var value = evalMethod.invoke(null, null, null, "33 + 55", false);
77print(value);
78
79// valueInfo
80var info = valueInfoMethod.invoke(null, "apply", Function, true);
81for each (var f in valueDescFields) {
82    print(f.name, "=", f.get(info));
83}
84
85// valueInfo - user defined object
86var info = valueInfoMethod.invoke(null, "foo", { foo: 343 }, true);
87for each (var f in valueDescFields) {
88    print(f.name, "=", f.get(info));
89}
90
91// valueInfos
92var infos = valueInfosMethod.invoke(null, Object, true);
93for each (var info in infos) {
94    for each (var f in valueDescFields) {
95        print(f.name, "=", f.get(info));
96    }
97}
98
99// valueInfos - user defined object
100var infos = valueInfosMethod.invoke(null, { foo: 34, bar: "hello" }, true);
101for each (var info in infos) {
102    for each (var f in valueDescFields) {
103        print(f.name, "=", f.get(info));
104    }
105}
106
107// valueAsString
108function printValue(value) {
109    print(valueAsStringMethod.invoke(null, value));
110}
111
112printValue(undefined);
113printValue(null);
114printValue("hello");
115printValue(Math.PI);
116printValue(this);
117
118// The below are not part of DebuggerSupport. But we need these to
119// test DebuggerSupport.getSourceInfo etc. which need compiled script class
120
121var Source = Java.type("jdk.nashorn.internal.runtime.Source");
122var Context = Java.type("jdk.nashorn.internal.runtime.Context");
123var ThrowErrorManager = Java.type("jdk.nashorn.internal.runtime.Context.ThrowErrorManager");
124var contextCls = java.lang.Class.forName("jdk.nashorn.internal.runtime.Context");
125var sourceCls = Source.class;
126var errorMgrCls = Java.type("jdk.nashorn.internal.runtime.ErrorManager").class;
127var booleanCls = Java.type("java.lang.Boolean").TYPE;
128var stringCls = Java.type("java.lang.String").class;
129
130// private compile method of Context class
131var compileMethod = contextCls.getDeclaredMethod("compile",
132                sourceCls, errorMgrCls, booleanCls, booleanCls);
133Reflector.setAccessible(compileMethod);
134
135var getContextMethod = contextCls.getMethod("getContext");
136Reflector.setAccessible(getContextMethod);
137
138var sourceForMethod = sourceCls.getMethod("sourceFor", stringCls, stringCls);
139var scriptCls = compileMethod.invoke(getContextMethod.invoke(null),
140    sourceForMethod.invoke(null, "test", "print('hello')"),
141    ThrowErrorManager.class.newInstance(), false, false);
142
143var SCRIPT_CLASS_NAME_PREFIX = "jdk.nashorn.internal.scripts.Script$";
144print("script class name pattern satisfied? " +
145    scriptCls.name.startsWith(SCRIPT_CLASS_NAME_PREFIX));
146
147var srcInfo = getSourceInfoMethod.invoke(null, scriptCls);
148var srcInfoFields = srcInfo.class.declaredFields;
149Arrays.sort(srcInfoFields, function(f1, f2) f1.name.compareTo(f2.name));
150
151print("Source info");
152for each (var f in srcInfoFields) {
153    Reflector.setAccessible(f);
154    var fieldValue = f.get(srcInfo);
155    if (fieldValue instanceof CharArray) {
156        fieldValue = new java.lang.String(fieldValue);
157    }
158
159    print(f.name, "=", fieldValue);
160}
161