Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: src/debug/mirrors.js

Issue 2642253005: [debugger api] remove legacy JSON debug protocol. (Closed)
Patch Set: fix cctest Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/debug/debug.js ('k') | src/execution.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2012 the V8 project authors. All rights reserved. 1 // Copyright 2006-2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, utils) { 5 (function(global, utils) {
6 "use strict"; 6 "use strict";
7 7
8 // ---------------------------------------------------------------------------- 8 // ----------------------------------------------------------------------------
9 // Imports 9 // Imports
10 10
(...skipping 2445 matching lines...) Expand 10 before | Expand all | Expand 10 after
2456 this.data_ = data; 2456 this.data_ = data;
2457 this.allocateHandle_(); 2457 this.allocateHandle_();
2458 } 2458 }
2459 inherits(ContextMirror, Mirror); 2459 inherits(ContextMirror, Mirror);
2460 2460
2461 2461
2462 ContextMirror.prototype.data = function() { 2462 ContextMirror.prototype.data = function() {
2463 return this.data_; 2463 return this.data_;
2464 }; 2464 };
2465 2465
2466
2467 /**
2468 * Returns a mirror serializer
2469 *
2470 * @param {boolean} details Set to true to include details
2471 * @param {Object} options Options comtrolling the serialization
2472 * The following options can be set:
2473 * includeSource: include ths full source of scripts
2474 * @returns {MirrorSerializer} mirror serializer
2475 */
2476 function MakeMirrorSerializer(details, options) {
2477 return new JSONProtocolSerializer(details, options);
2478 }
2479
2480
2481 /**
2482 * Object for serializing a mirror objects and its direct references.
2483 * @param {boolean} details Indicates whether to include details for the mirror
2484 * serialized
2485 * @constructor
2486 */
2487 function JSONProtocolSerializer(details, options) {
2488 this.details_ = details;
2489 this.options_ = options;
2490 this.mirrors_ = [ ];
2491 }
2492
2493
2494 /**
2495 * Returns a serialization of an object reference. The referenced object are
2496 * added to the serialization state.
2497 *
2498 * @param {Mirror} mirror The mirror to serialize
2499 * @returns {String} JSON serialization
2500 */
2501 JSONProtocolSerializer.prototype.serializeReference = function(mirror) {
2502 return this.serialize_(mirror, true, true);
2503 };
2504
2505
2506 /**
2507 * Returns a serialization of an object value. The referenced objects are
2508 * added to the serialization state.
2509 *
2510 * @param {Mirror} mirror The mirror to serialize
2511 * @returns {String} JSON serialization
2512 */
2513 JSONProtocolSerializer.prototype.serializeValue = function(mirror) {
2514 var json = this.serialize_(mirror, false, true);
2515 return json;
2516 };
2517
2518
2519 /**
2520 * Returns a serialization of all the objects referenced.
2521 *
2522 * @param {Mirror} mirror The mirror to serialize.
2523 * @returns {Array.<Object>} Array of the referenced objects converted to
2524 * protcol objects.
2525 */
2526 JSONProtocolSerializer.prototype.serializeReferencedObjects = function() {
2527 // Collect the protocol representation of the referenced objects in an array.
2528 var content = [];
2529
2530 // Get the number of referenced objects.
2531 var count = this.mirrors_.length;
2532
2533 for (var i = 0; i < count; i++) {
2534 content.push(this.serialize_(this.mirrors_[i], false, false));
2535 }
2536
2537 return content;
2538 };
2539
2540
2541 JSONProtocolSerializer.prototype.includeSource_ = function() {
2542 return this.options_ && this.options_.includeSource;
2543 };
2544
2545
2546 JSONProtocolSerializer.prototype.inlineRefs_ = function() {
2547 return this.options_ && this.options_.inlineRefs;
2548 };
2549
2550
2551 JSONProtocolSerializer.prototype.maxStringLength_ = function() {
2552 if (IS_UNDEFINED(this.options_) ||
2553 IS_UNDEFINED(this.options_.maxStringLength)) {
2554 return kMaxProtocolStringLength;
2555 }
2556 return this.options_.maxStringLength;
2557 };
2558
2559
2560 JSONProtocolSerializer.prototype.add_ = function(mirror) {
2561 // If this mirror is already in the list just return.
2562 for (var i = 0; i < this.mirrors_.length; i++) {
2563 if (this.mirrors_[i] === mirror) {
2564 return;
2565 }
2566 }
2567
2568 // Add the mirror to the list of mirrors to be serialized.
2569 this.mirrors_.push(mirror);
2570 };
2571
2572
2573 /**
2574 * Formats mirror object to protocol reference object with some data that can
2575 * be used to display the value in debugger.
2576 * @param {Mirror} mirror Mirror to serialize.
2577 * @return {Object} Protocol reference object.
2578 */
2579 JSONProtocolSerializer.prototype.serializeReferenceWithDisplayData_ =
2580 function(mirror) {
2581 var o = {};
2582 o.ref = mirror.handle();
2583 o.type = mirror.type();
2584 switch (mirror.type()) {
2585 case MirrorType.UNDEFINED_TYPE:
2586 case MirrorType.NULL_TYPE:
2587 case MirrorType.BOOLEAN_TYPE:
2588 case MirrorType.NUMBER_TYPE:
2589 o.value = mirror.value();
2590 break;
2591 case MirrorType.STRING_TYPE:
2592 o.value = mirror.getTruncatedValue(this.maxStringLength_());
2593 break;
2594 case MirrorType.SYMBOL_TYPE:
2595 o.description = mirror.description();
2596 break;
2597 case MirrorType.FUNCTION_TYPE:
2598 o.name = mirror.name();
2599 o.inferredName = mirror.inferredName();
2600 if (mirror.script()) {
2601 o.scriptId = mirror.script().id();
2602 }
2603 break;
2604 case MirrorType.ERROR_TYPE:
2605 case MirrorType.REGEXP_TYPE:
2606 o.value = mirror.toText();
2607 break;
2608 case MirrorType.OBJECT_TYPE:
2609 o.className = mirror.className();
2610 break;
2611 }
2612 return o;
2613 };
2614
2615
2616 JSONProtocolSerializer.prototype.serialize_ = function(mirror, reference,
2617 details) {
2618 // If serializing a reference to a mirror just return the reference and add
2619 // the mirror to the referenced mirrors.
2620 if (reference &&
2621 (mirror.isValue() || mirror.isScript() || mirror.isContext())) {
2622 if (this.inlineRefs_() && mirror.isValue()) {
2623 return this.serializeReferenceWithDisplayData_(mirror);
2624 } else {
2625 this.add_(mirror);
2626 return {'ref' : mirror.handle()};
2627 }
2628 }
2629
2630 // Collect the JSON property/value pairs.
2631 var content = {};
2632
2633 // Add the mirror handle.
2634 if (mirror.isValue() || mirror.isScript() || mirror.isContext()) {
2635 content.handle = mirror.handle();
2636 }
2637
2638 // Always add the type.
2639 content.type = mirror.type();
2640
2641 switch (mirror.type()) {
2642 case MirrorType.UNDEFINED_TYPE:
2643 case MirrorType.NULL_TYPE:
2644 // Undefined and null are represented just by their type.
2645 break;
2646
2647 case MirrorType.BOOLEAN_TYPE:
2648 // Boolean values are simply represented by their value.
2649 content.value = mirror.value();
2650 break;
2651
2652 case MirrorType.NUMBER_TYPE:
2653 // Number values are simply represented by their value.
2654 content.value = NumberToJSON_(mirror.value());
2655 break;
2656
2657 case MirrorType.STRING_TYPE:
2658 // String values might have their value cropped to keep down size.
2659 if (this.maxStringLength_() != -1 &&
2660 mirror.length() > this.maxStringLength_()) {
2661 var str = mirror.getTruncatedValue(this.maxStringLength_());
2662 content.value = str;
2663 content.fromIndex = 0;
2664 content.toIndex = this.maxStringLength_();
2665 } else {
2666 content.value = mirror.value();
2667 }
2668 content.length = mirror.length();
2669 break;
2670
2671 case MirrorType.SYMBOL_TYPE:
2672 content.description = mirror.description();
2673 break;
2674
2675 case MirrorType.OBJECT_TYPE:
2676 case MirrorType.FUNCTION_TYPE:
2677 case MirrorType.ERROR_TYPE:
2678 case MirrorType.REGEXP_TYPE:
2679 case MirrorType.PROMISE_TYPE:
2680 case MirrorType.GENERATOR_TYPE:
2681 // Add object representation.
2682 this.serializeObject_(mirror, content, details);
2683 break;
2684
2685 case MirrorType.PROPERTY_TYPE:
2686 case MirrorType.INTERNAL_PROPERTY_TYPE:
2687 throw %make_error(kDebugger,
2688 'PropertyMirror cannot be serialized independently');
2689 break;
2690
2691 case MirrorType.FRAME_TYPE:
2692 // Add object representation.
2693 this.serializeFrame_(mirror, content);
2694 break;
2695
2696 case MirrorType.SCOPE_TYPE:
2697 // Add object representation.
2698 this.serializeScope_(mirror, content);
2699 break;
2700
2701 case MirrorType.SCRIPT_TYPE:
2702 // Script is represented by id, name and source attributes.
2703 if (mirror.name()) {
2704 content.name = mirror.name();
2705 }
2706 content.id = mirror.id();
2707 content.lineOffset = mirror.lineOffset();
2708 content.columnOffset = mirror.columnOffset();
2709 content.lineCount = mirror.lineCount();
2710 if (mirror.data()) {
2711 content.data = mirror.data();
2712 }
2713 if (this.includeSource_()) {
2714 content.source = mirror.source();
2715 } else {
2716 var sourceStart = mirror.source().substring(0, 80);
2717 content.sourceStart = sourceStart;
2718 }
2719 content.sourceLength = mirror.source().length;
2720 content.scriptType = mirror.scriptType();
2721 content.compilationType = mirror.compilationType();
2722 // For compilation type eval emit information on the script from which
2723 // eval was called if a script is present.
2724 if (mirror.compilationType() == 1 &&
2725 mirror.evalFromScript()) {
2726 content.evalFromScript =
2727 this.serializeReference(mirror.evalFromScript());
2728 var evalFromLocation = mirror.evalFromLocation();
2729 if (evalFromLocation) {
2730 content.evalFromLocation = { line: evalFromLocation.line,
2731 column: evalFromLocation.column };
2732 }
2733 if (mirror.evalFromFunctionName()) {
2734 content.evalFromFunctionName = mirror.evalFromFunctionName();
2735 }
2736 }
2737 if (mirror.context()) {
2738 content.context = this.serializeReference(mirror.context());
2739 }
2740 break;
2741
2742 case MirrorType.CONTEXT_TYPE:
2743 content.data = mirror.data();
2744 break;
2745 }
2746
2747 // Always add the text representation.
2748 content.text = mirror.toText();
2749
2750 // Create and return the JSON string.
2751 return content;
2752 };
2753
2754
2755 /**
2756 * Serialize object information to the following JSON format.
2757 *
2758 * {"className":"<class name>",
2759 * "constructorFunction":{"ref":<number>},
2760 * "protoObject":{"ref":<number>},
2761 * "prototypeObject":{"ref":<number>},
2762 * "namedInterceptor":<boolean>,
2763 * "indexedInterceptor":<boolean>,
2764 * "properties":[<properties>],
2765 * "internalProperties":[<internal properties>]}
2766 */
2767 JSONProtocolSerializer.prototype.serializeObject_ = function(mirror, content,
2768 details) {
2769 // Add general object properties.
2770 content.className = mirror.className();
2771 content.constructorFunction =
2772 this.serializeReference(mirror.constructorFunction());
2773 content.protoObject = this.serializeReference(mirror.protoObject());
2774 content.prototypeObject = this.serializeReference(mirror.prototypeObject());
2775
2776 // Add flags to indicate whether there are interceptors.
2777 if (mirror.hasNamedInterceptor()) {
2778 content.namedInterceptor = true;
2779 }
2780 if (mirror.hasIndexedInterceptor()) {
2781 content.indexedInterceptor = true;
2782 }
2783
2784 if (mirror.isFunction()) {
2785 // Add function specific properties.
2786 content.name = mirror.name();
2787 if (!IS_UNDEFINED(mirror.inferredName())) {
2788 content.inferredName = mirror.inferredName();
2789 }
2790 content.resolved = mirror.resolved();
2791 if (mirror.resolved()) {
2792 content.source = mirror.source();
2793 }
2794 if (mirror.script()) {
2795 content.script = this.serializeReference(mirror.script());
2796 content.scriptId = mirror.script().id();
2797
2798 serializeLocationFields(mirror.sourceLocation(), content);
2799 }
2800
2801 content.scopes = [];
2802 for (var i = 0; i < mirror.scopeCount(); i++) {
2803 var scope = mirror.scope(i);
2804 content.scopes.push({
2805 type: scope.scopeType(),
2806 index: i
2807 });
2808 }
2809 }
2810
2811 if (mirror.isGenerator()) {
2812 // Add generator specific properties.
2813
2814 // Either 'running', 'closed', or 'suspended'.
2815 content.status = mirror.status();
2816
2817 content.func = this.serializeReference(mirror.func())
2818 content.receiver = this.serializeReference(mirror.receiver())
2819
2820 // If the generator is suspended, the content add line/column properties.
2821 serializeLocationFields(mirror.sourceLocation(), content);
2822
2823 // TODO(wingo): Also serialize a reference to the context (scope chain).
2824 }
2825
2826 if (mirror.isDate()) {
2827 // Add date specific properties.
2828 content.value = mirror.value();
2829 }
2830
2831 if (mirror.isPromise()) {
2832 // Add promise specific properties.
2833 content.status = mirror.status();
2834 content.promiseValue = this.serializeReference(mirror.promiseValue());
2835 }
2836
2837 // Add actual properties - named properties followed by indexed properties.
2838 var properties = mirror.propertyNames();
2839 for (var i = 0; i < properties.length; i++) {
2840 var propertyMirror = mirror.property(properties[i]);
2841 properties[i] = this.serializeProperty_(propertyMirror);
2842 if (details) {
2843 this.add_(propertyMirror.value());
2844 }
2845 }
2846 content.properties = properties;
2847
2848 var internalProperties = mirror.internalProperties();
2849 if (internalProperties.length > 0) {
2850 var ip = [];
2851 for (var i = 0; i < internalProperties.length; i++) {
2852 ip.push(this.serializeInternalProperty_(internalProperties[i]));
2853 }
2854 content.internalProperties = ip;
2855 }
2856 };
2857
2858
2859 /**
2860 * Serialize location information to the following JSON format:
2861 *
2862 * "position":"<position>",
2863 * "line":"<line>",
2864 * "column":"<column>",
2865 *
2866 * @param {SourceLocation} location The location to serialize, may be undefined.
2867 */
2868 function serializeLocationFields (location, content) {
2869 if (!location) {
2870 return;
2871 }
2872 content.position = location.position;
2873 var line = location.line;
2874 if (!IS_UNDEFINED(line)) {
2875 content.line = line;
2876 }
2877 var column = location.column;
2878 if (!IS_UNDEFINED(column)) {
2879 content.column = column;
2880 }
2881 }
2882
2883
2884 /**
2885 * Serialize property information to the following JSON format for building the
2886 * array of properties.
2887 *
2888 * {"name":"<property name>",
2889 * "attributes":<number>,
2890 * "propertyType":<number>,
2891 * "ref":<number>}
2892 *
2893 * If the attribute for the property is PropertyAttribute.None it is not added.
2894 * Here are a couple of examples.
2895 *
2896 * {"name":"hello","propertyType":0,"ref":1}
2897 * {"name":"length","attributes":7,"propertyType":3,"ref":2}
2898 *
2899 * @param {PropertyMirror} propertyMirror The property to serialize.
2900 * @returns {Object} Protocol object representing the property.
2901 */
2902 JSONProtocolSerializer.prototype.serializeProperty_ = function(propertyMirror) {
2903 var result = {};
2904
2905 result.name = propertyMirror.name();
2906 var propertyValue = propertyMirror.value();
2907 if (this.inlineRefs_() && propertyValue.isValue()) {
2908 result.value = this.serializeReferenceWithDisplayData_(propertyValue);
2909 } else {
2910 if (propertyMirror.attributes() != PropertyAttribute.None) {
2911 result.attributes = propertyMirror.attributes();
2912 }
2913 result.propertyType = propertyMirror.propertyType();
2914 result.ref = propertyValue.handle();
2915 }
2916 return result;
2917 };
2918
2919
2920 /**
2921 * Serialize internal property information to the following JSON format for
2922 * building the array of properties.
2923 *
2924 * {"name":"<property name>",
2925 * "ref":<number>}
2926 *
2927 * {"name":"[[BoundThis]]","ref":117}
2928 *
2929 * @param {InternalPropertyMirror} propertyMirror The property to serialize.
2930 * @returns {Object} Protocol object representing the property.
2931 */
2932 JSONProtocolSerializer.prototype.serializeInternalProperty_ =
2933 function(propertyMirror) {
2934 var result = {};
2935
2936 result.name = propertyMirror.name();
2937 var propertyValue = propertyMirror.value();
2938 if (this.inlineRefs_() && propertyValue.isValue()) {
2939 result.value = this.serializeReferenceWithDisplayData_(propertyValue);
2940 } else {
2941 result.ref = propertyValue.handle();
2942 }
2943 return result;
2944 };
2945
2946
2947 JSONProtocolSerializer.prototype.serializeFrame_ = function(mirror, content) {
2948 content.index = mirror.index();
2949 content.receiver = this.serializeReference(mirror.receiver());
2950 var func = mirror.func();
2951 content.func = this.serializeReference(func);
2952 var script = func.script();
2953 if (script) {
2954 content.script = this.serializeReference(script);
2955 }
2956 content.constructCall = mirror.isConstructCall();
2957 content.atReturn = mirror.isAtReturn();
2958 if (mirror.isAtReturn()) {
2959 content.returnValue = this.serializeReference(mirror.returnValue());
2960 }
2961 content.debuggerFrame = mirror.isDebuggerFrame();
2962 var x = new GlobalArray(mirror.argumentCount());
2963 for (var i = 0; i < mirror.argumentCount(); i++) {
2964 var arg = {};
2965 var argument_name = mirror.argumentName(i);
2966 if (argument_name) {
2967 arg.name = argument_name;
2968 }
2969 arg.value = this.serializeReference(mirror.argumentValue(i));
2970 x[i] = arg;
2971 }
2972 content.arguments = x;
2973 var x = new GlobalArray(mirror.localCount());
2974 for (var i = 0; i < mirror.localCount(); i++) {
2975 var local = {};
2976 local.name = mirror.localName(i);
2977 local.value = this.serializeReference(mirror.localValue(i));
2978 x[i] = local;
2979 }
2980 content.locals = x;
2981 serializeLocationFields(mirror.sourceLocation(), content);
2982 var source_line_text = mirror.sourceLineText();
2983 if (!IS_UNDEFINED(source_line_text)) {
2984 content.sourceLineText = source_line_text;
2985 }
2986
2987 content.scopes = [];
2988 for (var i = 0; i < mirror.scopeCount(); i++) {
2989 var scope = mirror.scope(i);
2990 content.scopes.push({
2991 type: scope.scopeType(),
2992 index: i
2993 });
2994 }
2995 };
2996
2997
2998 JSONProtocolSerializer.prototype.serializeScope_ = function(mirror, content) {
2999 content.index = mirror.scopeIndex();
3000 content.frameIndex = mirror.frameIndex();
3001 content.type = mirror.scopeType();
3002 content.object = this.inlineRefs_() ?
3003 this.serializeValue(mirror.scopeObject()) :
3004 this.serializeReference(mirror.scopeObject());
3005 };
3006
3007
3008 /**
3009 * Convert a number to a protocol value. For all finite numbers the number
3010 * itself is returned. For non finite numbers NaN, Infinite and
3011 * -Infinite the string representation "NaN", "Infinite" or "-Infinite"
3012 * (not including the quotes) is returned.
3013 *
3014 * @param {number} value The number value to convert to a protocol value.
3015 * @returns {number|string} Protocol value.
3016 */
3017 function NumberToJSON_(value) {
3018 if (IsNaN(value)) {
3019 return 'NaN';
3020 }
3021 if (!NUMBER_IS_FINITE(value)) {
3022 if (value > 0) {
3023 return 'Infinity';
3024 } else {
3025 return '-Infinity';
3026 }
3027 }
3028 return value;
3029 }
3030
3031 // ---------------------------------------------------------------------------- 2466 // ----------------------------------------------------------------------------
3032 // Exports 2467 // Exports
3033 2468
3034 utils.InstallFunctions(global, DONT_ENUM, [ 2469 utils.InstallFunctions(global, DONT_ENUM, [
3035 "MakeMirror", MakeMirror, 2470 "MakeMirror", MakeMirror,
3036 "MakeMirrorSerializer", MakeMirrorSerializer,
3037 "LookupMirror", LookupMirror, 2471 "LookupMirror", LookupMirror,
3038 "ToggleMirrorCache", ToggleMirrorCache, 2472 "ToggleMirrorCache", ToggleMirrorCache,
3039 "MirrorCacheIsEmpty", MirrorCacheIsEmpty, 2473 "MirrorCacheIsEmpty", MirrorCacheIsEmpty,
3040 ]); 2474 ]);
3041 2475
3042 utils.InstallConstants(global, [ 2476 utils.InstallConstants(global, [
3043 "ScopeType", ScopeType, 2477 "ScopeType", ScopeType,
3044 "PropertyType", PropertyType, 2478 "PropertyType", PropertyType,
3045 "PropertyAttribute", PropertyAttribute, 2479 "PropertyAttribute", PropertyAttribute,
3046 "Mirror", Mirror, 2480 "Mirror", Mirror,
(...skipping 27 matching lines...) Expand all
3074 // Functions needed by the debugger runtime. 2508 // Functions needed by the debugger runtime.
3075 utils.InstallFunctions(utils, DONT_ENUM, [ 2509 utils.InstallFunctions(utils, DONT_ENUM, [
3076 "ClearMirrorCache", ClearMirrorCache 2510 "ClearMirrorCache", ClearMirrorCache
3077 ]); 2511 ]);
3078 2512
3079 // Export to debug.js 2513 // Export to debug.js
3080 utils.Export(function(to) { 2514 utils.Export(function(to) {
3081 to.MirrorType = MirrorType; 2515 to.MirrorType = MirrorType;
3082 }); 2516 });
3083 }) 2517 })
OLDNEW
« no previous file with comments | « src/debug/debug.js ('k') | src/execution.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698