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

Side by Side Diff: src/d8.js

Issue 18589: Added a simple wrapping of the debugger JSON responses.... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 case 'frame': 255 case 'frame':
256 case 'f': 256 case 'f':
257 this.request_ = this.frameCommandToJSONRequest_(args); 257 this.request_ = this.frameCommandToJSONRequest_(args);
258 break; 258 break;
259 259
260 case 'print': 260 case 'print':
261 case 'p': 261 case 'p':
262 this.request_ = this.printCommandToJSONRequest_(args); 262 this.request_ = this.printCommandToJSONRequest_(args);
263 break; 263 break;
264 264
265 case 'dir':
266 this.request_ = this.dirCommandToJSONRequest_(args);
267 break;
268
265 case 'source': 269 case 'source':
266 this.request_ = this.sourceCommandToJSONRequest_(args); 270 this.request_ = this.sourceCommandToJSONRequest_(args);
267 break; 271 break;
268 272
269 case 'scripts': 273 case 'scripts':
270 this.request_ = this.scriptsCommandToJSONRequest_(args); 274 this.request_ = this.scriptsCommandToJSONRequest_(args);
271 break; 275 break;
272 276
273 case 'break': 277 case 'break':
274 case 'b': 278 case 'b':
275 this.request_ = this.breakCommandToJSONRequest_(args); 279 this.request_ = this.breakCommandToJSONRequest_(args);
276 break; 280 break;
277 281
278 case 'clear': 282 case 'clear':
279 this.request_ = this.clearCommandToJSONRequest_(args); 283 this.request_ = this.clearCommandToJSONRequest_(args);
280 break; 284 break;
281 285
282 case 'help': 286 case 'help':
283 case '?': 287 case '?':
284 this.helpCommand_(args); 288 this.helpCommand_(args);
285 // Return null to indicate no JSON to send (command handled internally). 289 // Return null to indicate no JSON to send (command handled internally).
286 this.request_ = void 0; 290 this.request_ = void 0;
287 break; 291 break;
288 292
289 default: 293 default:
290 throw new Error('Unknown command "' + cmd + '"'); 294 throw new Error('Unknown command "' + cmd + '"');
291 } 295 }
296
297 last_cmd = cmd;
292 } 298 }
293 299
294 DebugRequest.prototype.JSONRequest = function() { 300 DebugRequest.prototype.JSONRequest = function() {
295 return this.request_; 301 return this.request_;
296 } 302 }
297 303
298 304
299 function RequestPacket(command) { 305 function RequestPacket(command) {
300 this.seq = 0; 306 this.seq = 0;
301 this.type = 'request'; 307 this.type = 'request';
(...skipping 21 matching lines...) Expand all
323 json += '}'; 329 json += '}';
324 return json; 330 return json;
325 } 331 }
326 332
327 333
328 DebugRequest.prototype.createRequest = function(command) { 334 DebugRequest.prototype.createRequest = function(command) {
329 return new RequestPacket(command); 335 return new RequestPacket(command);
330 }; 336 };
331 337
332 338
339 // Create a JSON request for the evaluation command.
340 DebugRequest.prototype.makeEvaluateJSONRequest_ = function(expression) {
341 // Check if the expression is a handle id in the form #<handle>#.
342 var handle_match = expression.match(/^#([0-9]*)#$/);
343 if (handle_match) {
344 // Build an evaluate request.
345 var request = this.createRequest('lookup');
346 request.arguments = {};
347 request.arguments.handle = parseInt(handle_match[1]);
348 return request.toJSONProtocol();
349 } else {
350 // Build an evaluate request.
351 var request = this.createRequest('evaluate');
352 request.arguments = {};
353 request.arguments.expression = expression;
354 return request.toJSONProtocol();
355 }
356 };
357
358
359
333 // Create a JSON request for the continue command. 360 // Create a JSON request for the continue command.
334 DebugRequest.prototype.continueCommandToJSONRequest_ = function(args) { 361 DebugRequest.prototype.continueCommandToJSONRequest_ = function(args) {
335 var request = this.createRequest('continue'); 362 var request = this.createRequest('continue');
336 return request.toJSONProtocol(); 363 return request.toJSONProtocol();
337 }; 364 };
338 365
339 366
340 // Create a JSON request for the step command. 367 // Create a JSON request for the step command.
341 DebugRequest.prototype.stepCommandToJSONRequest_ = function(args) { 368 DebugRequest.prototype.stepCommandToJSONRequest_ = function(args) {
342 // Requesting a step is through the continue command with additional 369 // Requesting a step is through the continue command with additional
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 if (args.length > 0 && args[0].length > 0) { 458 if (args.length > 0 && args[0].length > 0) {
432 request.arguments = {}; 459 request.arguments = {};
433 request.arguments.number = args[0]; 460 request.arguments.number = args[0];
434 } 461 }
435 return request.toJSONProtocol(); 462 return request.toJSONProtocol();
436 }; 463 };
437 464
438 465
439 // Create a JSON request for the print command. 466 // Create a JSON request for the print command.
440 DebugRequest.prototype.printCommandToJSONRequest_ = function(args) { 467 DebugRequest.prototype.printCommandToJSONRequest_ = function(args) {
441 // Build a evaluate request from the text command. 468 // Build an evaluate request from the text command.
442 var request = this.createRequest('evaluate');
443 if (args.length == 0) { 469 if (args.length == 0) {
444 throw new Error('Missing expression.'); 470 throw new Error('Missing expression.');
445 } 471 }
446 472 return this.makeEvaluateJSONRequest_(args);
447 request.arguments = {};
448 request.arguments.expression = args;
449
450 return request.toJSONProtocol();
451 }; 473 };
452 474
453 475
476 // Create a JSON request for the dir command.
477 DebugRequest.prototype.dirCommandToJSONRequest_ = function(args) {
478 // Build an evaluate request from the text command.
479 if (args.length == 0) {
480 throw new Error('Missing expression.');
481 }
482 return this.makeEvaluateJSONRequest_(args);
483 };
484
485
454 // Create a JSON request for the source command. 486 // Create a JSON request for the source command.
455 DebugRequest.prototype.sourceCommandToJSONRequest_ = function(args) { 487 DebugRequest.prototype.sourceCommandToJSONRequest_ = function(args) {
456 // Build a evaluate request from the text command. 488 // Build a evaluate request from the text command.
457 var request = this.createRequest('source'); 489 var request = this.createRequest('source');
458 490
459 // Default is ten lines starting five lines before the current location. 491 // Default is ten lines starting five lines before the current location.
460 var from = Debug.State.currentSourceLine - 5; 492 var from = Debug.State.currentSourceLine - 5;
461 var lines = 10; 493 var lines = 10;
462 494
463 // Parse the arguments. 495 // Parse the arguments.
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 print('frame <frame #>'); 610 print('frame <frame #>');
579 print('step [in | next | out| min [step count]]'); 611 print('step [in | next | out| min [step count]]');
580 print('print <expression>'); 612 print('print <expression>');
581 print('source [from line [num lines]]'); 613 print('source [from line [num lines]]');
582 print('scripts'); 614 print('scripts');
583 print('continue'); 615 print('continue');
584 print('help'); 616 print('help');
585 } 617 }
586 618
587 619
620 function formatHandleReference_(value) {
621 return '#' + value.handle() + '#';
622 }
623
624
588 // Convert a JSON response to text for display in a text based debugger. 625 // Convert a JSON response to text for display in a text based debugger.
589 function DebugResponseDetails(json_response) { 626 function DebugResponseDetails(json_response) {
590 details = {text:'', running:false} 627 details = {text:'', running:false}
591 628
592 try { 629 try {
593 // Convert the JSON string to an object. 630 // Convert the JSON string to an object.
594 response = eval('(' + json_response + ')'); 631 var response = new ProtocolPackage(json_response);
595 632
596 if (!response.success) { 633 if (!response.success()) {
597 details.text = response.message; 634 details.text = response.message();
598 return details; 635 return details;
599 } 636 }
600 637
601 // Get the running state. 638 // Get the running state.
602 details.running = response.running; 639 details.running = response.running();
603 640
604 switch (response.command) { 641 var body = response.body();
642 var result = '';
643 switch (response.command()) {
605 case 'setbreakpoint': 644 case 'setbreakpoint':
606 var body = response.body;
607 result = 'set breakpoint #'; 645 result = 'set breakpoint #';
608 result += body.breakpoint; 646 result += body.breakpoint;
609 details.text = result; 647 details.text = result;
610 break; 648 break;
611 649
612 case 'clearbreakpoint': 650 case 'clearbreakpoint':
613 var body = response.body;
614 result = 'cleared breakpoint #'; 651 result = 'cleared breakpoint #';
615 result += body.breakpoint; 652 result += body.breakpoint;
616 details.text = result; 653 details.text = result;
617 break; 654 break;
618 655
619 case 'backtrace': 656 case 'backtrace':
620 var body = response.body;
621 if (body.totalFrames == 0) { 657 if (body.totalFrames == 0) {
622 result = '(empty stack)'; 658 result = '(empty stack)';
623 } else { 659 } else {
624 var result = 'Frames #' + body.fromFrame + ' to #' + 660 var result = 'Frames #' + body.fromFrame + ' to #' +
625 (body.toFrame - 1) + ' of ' + body.totalFrames + '\n'; 661 (body.toFrame - 1) + ' of ' + body.totalFrames + '\n';
626 for (i = 0; i < body.frames.length; i++) { 662 for (i = 0; i < body.frames.length; i++) {
627 if (i != 0) result += '\n'; 663 if (i != 0) result += '\n';
628 result += body.frames[i].text; 664 result += body.frames[i].text;
629 } 665 }
630 } 666 }
631 details.text = result; 667 details.text = result;
632 break; 668 break;
633 669
634 case 'frame': 670 case 'frame':
635 details.text = SourceUnderline(response.body.sourceLineText, 671 details.text = SourceUnderline(body.sourceLineText,
636 response.body.column); 672 body.column);
637 Debug.State.currentSourceLine = response.body.line; 673 Debug.State.currentSourceLine = body.line;
638 Debug.State.currentFrame = response.body.index; 674 Debug.State.currentFrame = body.index;
639 break; 675 break;
640 676
641 case 'evaluate': 677 case 'evaluate':
642 details.text = response.body.text; 678 case 'lookup':
679 if (last_cmd == 'p' || last_cmd == 'print') {
680 details.text = body.text;
681 } else {
682 var value = response.bodyValue();
683 if (value.isObject()) {
684 result += formatHandleReference_(value);
685 result += ', type: object'
686 result += ', constructor ';
687 var ctor = value.constructorFunctionValue();
688 result += formatHandleReference_(ctor);
689 result += ', __proto__ ';
690 var proto = value.protoObjectValue();
691 result += formatHandleReference_(proto);
692 result += ', ';
693 result += value.propertyCount();
694 result += ' properties.\n';
695 for (var i = 0; i < value.propertyCount(); i++) {
696 result += ' ';
697 result += value.propertyName(i);
698 result += ': ';
699 var property_value = value.propertyValue(i);
700 if (property_value && property_value.type()) {
701 result += property_value.type();
702 } else {
703 result += '<no type>';
704 }
705 result += ' ';
706 result += formatHandleReference_(property_value);
707 result += '\n';
708 }
709 } else {
710 result += 'type: ';
711 result += value.type();
712 if (!value.isUndefined() && !value.isNull()) {
713 result += ', ';
714 if (value.isString()) {
715 result += '"';
716 }
717 result += value.value();
718 if (value.isString()) {
719 result += '"';
720 }
721 }
722 result += '\n';
723 }
724 }
725 details.text = result;
643 break; 726 break;
644 727
645 case 'source': 728 case 'source':
646 // Get the source from the response. 729 // Get the source from the response.
647 var source = response.body.source; 730 var source = body.source;
648 var from_line = response.body.fromLine + 1; 731 var from_line = body.fromLine + 1;
649 var lines = source.split('\n'); 732 var lines = source.split('\n');
650 var maxdigits = 1 + Math.floor(log10(from_line + lines.length)); 733 var maxdigits = 1 + Math.floor(log10(from_line + lines.length));
651 if (maxdigits < 3) { 734 if (maxdigits < 3) {
652 maxdigits = 3; 735 maxdigits = 3;
653 } 736 }
654 var result = ''; 737 var result = '';
655 for (var num = 0; num < lines.length; num++) { 738 for (var num = 0; num < lines.length; num++) {
656 // Check if there's an extra newline at the end. 739 // Check if there's an extra newline at the end.
657 if (num == (lines.length - 1) && lines[num].length == 0) { 740 if (num == (lines.length - 1) && lines[num].length == 0) {
658 break; 741 break;
(...skipping 13 matching lines...) Expand all
672 result += current_line + ': '; 755 result += current_line + ': ';
673 } 756 }
674 result += lines[num]; 757 result += lines[num];
675 result += '\n'; 758 result += '\n';
676 } 759 }
677 details.text = result; 760 details.text = result;
678 break; 761 break;
679 762
680 case 'scripts': 763 case 'scripts':
681 var result = ''; 764 var result = '';
682 for (i = 0; i < response.body.length; i++) { 765 for (i = 0; i < body.length; i++) {
683 if (i != 0) result += '\n'; 766 if (i != 0) result += '\n';
684 if (response.body[i].name) { 767 if (body[i].name) {
685 result += response.body[i].name; 768 result += body[i].name;
686 } else { 769 } else {
687 result += '[unnamed] '; 770 result += '[unnamed] ';
688 var sourceStart = response.body[i].sourceStart; 771 var sourceStart = body[i].sourceStart;
689 if (sourceStart.length > 40) { 772 if (sourceStart.length > 40) {
690 sourceStart = sourceStart.substring(0, 37) + '...'; 773 sourceStart = sourceStart.substring(0, 37) + '...';
691 } 774 }
692 result += sourceStart; 775 result += sourceStart;
693 } 776 }
694 result += ' (lines: '; 777 result += ' (lines: ';
695 result += response.body[i].sourceLines; 778 result += body[i].sourceLines;
696 result += ', length: '; 779 result += ', length: ';
697 result += response.body[i].sourceLength; 780 result += body[i].sourceLength;
698 if (response.body[i].type == Debug.ScriptType.Native) { 781 if (body[i].type == Debug.ScriptType.Native) {
699 result += ', native'; 782 result += ', native';
700 } else if (response.body[i].type == Debug.ScriptType.Extension) { 783 } else if (body[i].type == Debug.ScriptType.Extension) {
701 result += ', extension'; 784 result += ', extension';
702 } 785 }
703 result += ')'; 786 result += ')';
704 } 787 }
705 details.text = result; 788 details.text = result;
706 break; 789 break;
707 790
708 case 'continue': 791 case 'continue':
709 details.text = "(running)"; 792 details.text = "(running)";
710 break; 793 break;
711 794
712 default: 795 default:
713 details.text = 796 details.text =
714 'Response for unknown command \'' + response.command + '\'' + 797 'Response for unknown command \'' + response.command + '\'' +
715 ' (' + json_response + ')'; 798 ' (' + json_response + ')';
716 } 799 }
717 } catch (e) { 800 } catch (e) {
718 details.text = 'Error: "' + e + '" formatting response'; 801 details.text = 'Error: "' + e + '" formatting response';
719 } 802 }
720 803
721 return details; 804 return details;
722 }; 805 };
723 806
724 807
808 /**
809 * Protocol packages send from the debugger.
810 * @param {string} json - raw protocol packet as JSON string.
811 * @constructor
812 */
813 function ProtocolPackage(json) {
814 this.packet_ = eval('(' + json + ')');
815 this.refs_ = [];
816 if (this.packet_.refs) {
817 for (var i = 0; i < this.packet_.refs.length; i++) {
818 this.refs_[this.packet_.refs[i].handle] = this.packet_.refs[i];
819 }
820 }
821 }
822
823
824 /**
825 * Get the packet type.
826 * @return {String} the packet type
827 */
828 ProtocolPackage.prototype.type = function() {
829 return this.packet_.type;
830 }
831
832
833 /**
834 * Get the packet event.
835 * @return {Object} the packet event
836 */
837 ProtocolPackage.prototype.event = function() {
838 return this.packet_.event;
839 }
840
841
842 /**
843 * Get the packet request sequence.
844 * @return {number} the packet request sequence
845 */
846 ProtocolPackage.prototype.requestSeq = function() {
847 return this.packet_.request_seq;
848 }
849
850
851 /**
852 * Get the packet request sequence.
853 * @return {number} the packet request sequence
854 */
855 ProtocolPackage.prototype.running = function() {
856 return this.packet_.running ? true : false;
857 }
858
859
860 ProtocolPackage.prototype.success = function() {
861 return this.packet_.success ? true : false;
862 }
863
864
865 ProtocolPackage.prototype.message = function() {
866 return this.packet_.message;
867 }
868
869
870 ProtocolPackage.prototype.command = function() {
871 return this.packet_.command;
872 }
873
874
875 ProtocolPackage.prototype.body = function() {
876 return this.packet_.body;
877 }
878
879
880 ProtocolPackage.prototype.bodyValue = function() {
881 return new ProtocolValue(this.packet_.body, this);
882 }
883
884
885 ProtocolPackage.prototype.body = function() {
886 return this.packet_.body;
887 }
888
889
890 ProtocolPackage.prototype.lookup = function(handle) {
891 var value = this.refs_[handle];
892 if (value) {
893 return new ProtocolValue(value, this);
894 } else {
895 return new ProtocolReference(handle);
896 }
897 }
898
899
900 function ProtocolValue(value, packet) {
901 this.value_ = value;
902 this.packet_ = packet;
903 }
904
905
906 /**
907 * Get the value type.
908 * @return {String} the value type
909 */
910 ProtocolValue.prototype.type = function() {
911 return this.value_.type;
912 }
913
914
915 /**
916 * Check is the value is a primitive value.
917 * @return {boolean} true if the value is primitive
918 */
919 ProtocolValue.prototype.isPrimitive = function() {
920 return this.isUndefined() || this.isNull() || this.isBoolean() ||
921 this.isNumber() || this.isString();
922 }
923
924
925 /**
926 * Get the object handle.
927 * @return {number} the value handle
928 */
929 ProtocolValue.prototype.handle = function() {
930 return this.value_.handle;
931 }
932
933
934 /**
935 * Check is the value is undefined.
936 * @return {boolean} true if the value is undefined
937 */
938 ProtocolValue.prototype.isUndefined = function() {
939 return this.value_.type == 'undefined';
940 }
941
942
943 /**
944 * Check is the value is null.
945 * @return {boolean} true if the value is null
946 */
947 ProtocolValue.prototype.isNull = function() {
948 return this.value_.type == 'null';
949 }
950
951
952 /**
953 * Check is the value is a boolean.
954 * @return {boolean} true if the value is a boolean
955 */
956 ProtocolValue.prototype.isBoolean = function() {
957 return this.value_.type == 'boolean';
958 }
959
960
961 /**
962 * Check is the value is a number.
963 * @return {boolean} true if the value is a number
964 */
965 ProtocolValue.prototype.isNumber = function() {
966 return this.value_.type == 'number';
967 }
968
969
970 /**
971 * Check is the value is a string.
972 * @return {boolean} true if the value is a string
973 */
974 ProtocolValue.prototype.isString = function() {
975 return this.value_.type == 'string';
976 }
977
978
979 /**
980 * Check is the value is an object.
981 * @return {boolean} true if the value is an object
982 */
983 ProtocolValue.prototype.isObject = function() {
984 return this.value_.type == 'object' || this.value_.type == 'function' ||
985 this.value_.type == 'error' || this.value_.type == 'regexp';
986 }
987
988
989 /**
990 * Get the constructor function
991 * @return {ProtocolValue} constructor function
992 */
993 ProtocolValue.prototype.constructorFunctionValue = function() {
994 var ctor = this.value_.constructorFunction;
995 return this.packet_.lookup(ctor.ref);
996 }
997
998
999 /**
1000 * Get the __proto__ value
1001 * @return {ProtocolValue} __proto__ value
1002 */
1003 ProtocolValue.prototype.protoObjectValue = function() {
1004 var proto = this.value_.protoObject;
1005 return this.packet_.lookup(proto.ref);
1006 }
1007
1008
1009 /**
1010 * Get the number og properties.
1011 * @return {number} the number of properties
1012 */
1013 ProtocolValue.prototype.propertyCount = function() {
1014 return this.value_.properties ? this.value_.properties.length : 0;
1015 }
1016
1017
1018 /**
1019 * Get the specified property name.
1020 * @return {string} property name
1021 */
1022 ProtocolValue.prototype.propertyName = function(index) {
1023 var property = this.value_.properties[index];
1024 return property.name;
1025 }
1026
1027
1028 /**
1029 * Return index for the property name.
1030 * @param name The property name to look for
1031 * @return {number} index for the property name
1032 */
1033 ProtocolValue.prototype.propertyIndex = function(name) {
1034 for (var i = 0; i < this.propertyCount(); i++) {
1035 if (this.value_.properties[i].name == name) {
1036 return i;
1037 }
1038 }
1039 return null;
1040 }
1041
1042
1043 /**
1044 * Get the specified property value.
1045 * @return {ProtocolValue} property value
1046 */
1047 ProtocolValue.prototype.propertyValue = function(index) {
1048 var property = this.value_.properties[index];
1049 return this.packet_.lookup(property.ref);
1050 }
1051
1052
1053 /**
1054 * Check is the value is a string.
1055 * @return {boolean} true if the value is a string
1056 */
1057 ProtocolValue.prototype.value = function() {
1058 return this.value_.value;
1059 }
1060
1061
1062 function ProtocolReference(handle) {
1063 this.handle_ = handle;
1064 }
1065
1066
1067 ProtocolReference.prototype.handle = function() {
1068 return this.handle_;
1069 }
1070
1071
725 function MakeJSONPair_(name, value) { 1072 function MakeJSONPair_(name, value) {
726 return '"' + name + '":' + value; 1073 return '"' + name + '":' + value;
727 } 1074 }
728 1075
729 1076
730 function ArrayToJSONObject_(content) { 1077 function ArrayToJSONObject_(content) {
731 return '{' + content.join(',') + '}'; 1078 return '{' + content.join(',') + '}';
732 } 1079 }
733 1080
734 1081
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
919 json += NumberToJSON_(elem); 1266 json += NumberToJSON_(elem);
920 } else if (typeof(elem) === 'string') { 1267 } else if (typeof(elem) === 'string') {
921 json += StringToJSON_(elem); 1268 json += StringToJSON_(elem);
922 } else { 1269 } else {
923 json += elem; 1270 json += elem;
924 } 1271 }
925 } 1272 }
926 json += ']'; 1273 json += ']';
927 return json; 1274 return json;
928 } 1275 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698