aboutsummaryrefslogtreecommitdiff
path: root/src/jdk/nashorn/internal/runtime/DebuggerSupport.java
blob: baa69d93d4d4c51937fb47f4985e82556d92247c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.nashorn.internal.runtime;

import static jdk.nashorn.internal.codegen.CompilerConstants.SOURCE;
import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT;

import java.lang.invoke.MethodHandle;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import jdk.nashorn.internal.scripts.JS;

/**
 * This class provides support for external debuggers.  Its primary purpose is
 * is to simplify the debugger tasks and provide better performance.
 * Even though the methods are not public, there are still part of the
 * external debugger interface.
 */
final class DebuggerSupport {
    /**
     * Hook to force the loading of the DebuggerSupport class so that it is
     * available to external debuggers.
     */
    static boolean FORCELOAD = true;

    static {
        /**
         * Hook to force the loading of the DebuggerValueDesc class so that it is
         * available to external debuggers.
         */
        @SuppressWarnings("unused")
        final
        DebuggerValueDesc forceLoad = new DebuggerValueDesc(null, false, null, null);

        // Hook to force the loading of the SourceInfo class
        @SuppressWarnings("unused")
        final
        SourceInfo srcInfo = new SourceInfo(null, 0, null, null);
    }

    /** This class is used to send a bulk description of a value. */
    static class DebuggerValueDesc {
        /** Property key (or index) or field name. */
        final String key;

        /** If the value is expandable. */
        final boolean expandable;

        /** Property or field value as object. */
        final Object valueAsObject;

        /** Property or field value as string. */
        final String valueAsString;

        DebuggerValueDesc(final String key, final boolean expandable, final Object valueAsObject, final String valueAsString) {
            this.key           = key;
            this.expandable    = expandable;
            this.valueAsObject = valueAsObject;
            this.valueAsString = valueAsString;
        }
    }

    static class SourceInfo {
        final String name;
        final URL    url;
        final int    hash;
        final char[] content;

        SourceInfo(final String name, final int hash, final URL url, final char[] content) {
            this.name    = name;
            this.hash    = hash;
            this.url     = url;
            this.content = content;
        }
    }

    /**
     * Hook that is called just before invoking method handle
     * from ScriptFunctionData via invoke, constructor method calls.
     *
     * @param mh script class method about to be invoked.
     */
    static void notifyInvoke(final MethodHandle mh) {
        // Do nothing here. This is placeholder method on which a
        // debugger can place a breakpoint so that it can access the
        // (script class) method handle that is about to be invoked.
        // See ScriptFunctionData.invoke and ScriptFunctionData.construct.
    }

    /**
     * Return the script source info for the given script class.
     *
     * @param clazz compiled script class
     * @return SourceInfo
     */
    static SourceInfo getSourceInfo(final Class<?> clazz) {
        if (JS.class.isAssignableFrom(clazz)) {
            try {
                final Field sourceField = clazz.getDeclaredField(SOURCE.symbolName());
                sourceField.setAccessible(true);
                final Source src = (Source) sourceField.get(null);
                return src.getSourceInfo();
            } catch (final IllegalAccessException | NoSuchFieldException ignored) {
                return null;
            }
        }

        return null;
    }

    /**
     * Return the current context global.
     * @return context global.
     */
    static Object getGlobal() {
        return Context.getGlobal();
    }

    /**
     * Call eval on the current global.
     * @param scope           Scope to use.
     * @param self            Receiver to use.
     * @param string          String to evaluate.
     * @param returnException true if exceptions are to be returned.
     * @return Result of eval, or, an exception or null depending on returnException.
     */
    static Object eval(final ScriptObject scope, final Object self, final String string, final boolean returnException) {
        final ScriptObject global = Context.getGlobal();
        final ScriptObject initialScope = scope != null ? scope : global;
        final Object callThis = self != null ? self : global;
        final Context context = global.getContext();

        try {
            return context.eval(initialScope, string, callThis, ScriptRuntime.UNDEFINED);
        } catch (final Throwable ex) {
            return returnException ? ex : null;
        }
    }

    /**
     * This method returns a bulk description of an object's properties.
     * @param object Script object to be displayed by the debugger.
     * @param all    true if to include non-enumerable values.
     * @return An array of DebuggerValueDesc.
     */
    static DebuggerValueDesc[] valueInfos(final Object object, final boolean all) {
        assert object instanceof ScriptObject;
        return getDebuggerValueDescs((ScriptObject)object, all, new HashSet<>());
    }

    /**
     * This method returns a debugger description of the value.
     * @param name  Name of value (property name).
     * @param value Data value.
     * @param all   true if to include non-enumerable values.
     * @return A DebuggerValueDesc.
     */
    static DebuggerValueDesc valueInfo(final String name, final Object value, final boolean all) {
        return valueInfo(name, value, all, new HashSet<>());
    }

   /**
     * This method returns a debugger description of the value.
     * @param name       Name of value (property name).
     * @param value      Data value.
     * @param all        true if to include non-enumerable values.
     * @param duplicates Duplication set to avoid cycles.
     * @return A DebuggerValueDesc.
     */
    private static DebuggerValueDesc valueInfo(final String name, final Object value, final boolean all, final Set<Object> duplicates) {
        if (value instanceof ScriptObject && !(value instanceof ScriptFunction)) {
            final ScriptObject object = (ScriptObject)value;
            return new DebuggerValueDesc(name, !object.isEmpty(), value, objectAsString(object, all, duplicates));
        }
        return new DebuggerValueDesc(name, false, value, valueAsString(value));
    }

    /**
     * Generate the descriptions for an object's properties.
     * @param object     Object to introspect.
     * @param all        true if to include non-enumerable values.
     * @param duplicates Duplication set to avoid cycles.
     * @return An array of DebuggerValueDesc.
     */
    private static DebuggerValueDesc[] getDebuggerValueDescs(final ScriptObject object, final boolean all, final Set<Object> duplicates) {
        if (duplicates.contains(object)) {
            return null;
        }

        duplicates.add(object);

        final String[] keys = object.getOwnKeys(all);
        final DebuggerValueDesc[] descs = new DebuggerValueDesc[keys.length];

        for (int i = 0; i < keys.length; i++) {
            final String key = keys[i];
            descs[i] = valueInfo(key, object.get(key), all, duplicates);
        }

        duplicates.remove(object);

        return descs;
    }

    /**
     * Generate a string representation of a Script object.
     * @param object     Script object to represent.
     * @param all        true if to include non-enumerable values.
     * @param duplicates Duplication set to avoid cycles.
     * @return String representation.
     */
    private static String objectAsString(final ScriptObject object, final boolean all, final Set<Object> duplicates) {
        final StringBuilder sb = new StringBuilder();

        if (ScriptObject.isArray(object)) {
            sb.append('[');
            final long length = (long) object.getDouble("length", INVALID_PROGRAM_POINT);

            for (long i = 0; i < length; i++) {
                if (object.has(i)) {
                    final Object valueAsObject = object.get(i);
                    final boolean isUndefined = valueAsObject == ScriptRuntime.UNDEFINED;

                    if (isUndefined) {
                        if (i != 0) {
                            sb.append(",");
                        }
                    } else {
                        if (i != 0) {
                            sb.append(", ");
                        }

                        if (valueAsObject instanceof ScriptObject && !(valueAsObject instanceof ScriptFunction)) {
                            final String objectString = objectAsString((ScriptObject)valueAsObject, all, duplicates);
                            sb.append(objectString != null ? objectString : "{...}");
                        } else {
                            sb.append(valueAsString(valueAsObject));
                        }
                    }
                } else {
                    if (i != 0) {
                        sb.append(',');
                    }
                }
            }

            sb.append(']');
        } else {
            sb.append('{');
            final DebuggerValueDesc[] descs = getDebuggerValueDescs(object, all, duplicates);

            if (descs != null) {
                for (int i = 0; i < descs.length; i++) {
                    if (i != 0) {
                        sb.append(", ");
                    }

                    final String valueAsString = descs[i].valueAsString;
                    sb.append(descs[i].key);
                    sb.append(": ");
                    sb.append(valueAsString);
                }
            }

            sb.append('}');
        }

        return sb.toString();
    }

    /**
     * This method returns a string representation of a value.
     * @param value Arbitrary value to be displayed by the debugger.
     * @return A string representation of the value or an array of DebuggerValueDesc.
     */
    static String valueAsString(final Object value) {
        final JSType type = JSType.of(value);

        switch (type) {
        case BOOLEAN:
            return value.toString();

        case STRING:
            return escape(value.toString());

        case NUMBER:
            return JSType.toString(((Number)value).doubleValue());

        case NULL:
            return "null";

        case UNDEFINED:
            return "undefined";

        case OBJECT:
            return ScriptRuntime.safeToString(value);

        case FUNCTION:
            if (value instanceof ScriptFunction) {
                return ((ScriptFunction)value).toSource();
            }
            return value.toString();

        default:
            return value.toString();
        }
    }

    /**
     * Escape a string into a form that can be parsed by JavaScript.
     * @param value String to be escaped.
     * @return Escaped string.
     */
    private static String escape(final String value) {
        final StringBuilder sb = new StringBuilder();

        sb.append("\"");

        for (final char ch : value.toCharArray()) {
            switch (ch) {
            case '\\':
                sb.append("\\\\");
                break;
            case '"':
                sb.append("\\\"");
                break;
            case '\'':
                sb.append("\\\'");
                break;
            case '\b':
                sb.append("\\b");
                break;
            case '\f':
                sb.append("\\f");
                break;
            case '\n':
                sb.append("\\n");
                break;
            case '\r':
                sb.append("\\r");
                break;
            case '\t':
                sb.append("\\t");
                break;
            default:
                if (ch < ' ' || ch >= 0xFF) {
                    sb.append("\\u");

                    final String hex = Integer.toHexString(ch);
                    for (int i = hex.length(); i < 4; i++) {
                        sb.append('0');
                    }
                    sb.append(hex);
                } else {
                    sb.append(ch);
                }

                break;
            }
        }

        sb.append("\"");

        return sb.toString();
    }
}