blob: 3009037e788d4c0e8e601bbb60465659adab8fdb [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28String.prototype.startsWith = function (str) {
29 if (str.length > this.length)
30 return false;
31 return this.substr(0, str.length) == str;
32}
33
34function log10(num) {
35 return Math.log(num)/Math.log(10);
36}
37
38function ToInspectableObject(obj) {
39 if (!obj && typeof obj === 'object') {
40 return void 0;
41 } else {
42 return Object(obj);
43 }
44}
45
46function GetCompletions(global, last, full) {
47 var full_tokens = full.split();
48 full = full_tokens.pop();
49 var parts = full.split('.');
50 parts.pop();
51 var current = global;
52 for (var i = 0; i < parts.length; i++) {
53 var part = parts[i];
54 var next = current[part];
55 if (!next)
56 return [];
57 current = next;
58 }
59 var result = [];
60 current = ToInspectableObject(current);
61 while (typeof current !== 'undefined') {
62 var mirror = new $debug.ObjectMirror(current);
63 var properties = mirror.properties();
64 for (var i = 0; i < properties.length; i++) {
65 var name = properties[i].name();
66 if (typeof name === 'string' && name.startsWith(last))
67 result.push(name);
68 }
69 current = ToInspectableObject(current.__proto__);
70 }
71 return result;
72}
73
74
75// Global object holding debugger related constants and state.
76const Debug = {};
77
78
79// Debug events which can occour in the V8 JavaScript engine. These originate
80// from the API include file v8-debug.h.
81Debug.DebugEvent = { Break: 1,
82 Exception: 2,
83 NewFunction: 3,
84 BeforeCompile: 4,
85 AfterCompile: 5 };
86
87
88// The different types of scripts matching enum ScriptType in objects.h.
89Debug.ScriptType = { Native: 0,
90 Extension: 1,
91 Normal: 2 };
92
93
94// The different types of script compilations matching enum
95// Script::CompilationType in objects.h.
96Debug.ScriptCompilationType = { Host: 0,
97 Eval: 1,
98 JSON: 2 };
99
100
101// The different types of scopes matching constants runtime.cc.
102Debug.ScopeType = { Global: 0,
103 Local: 1,
104 With: 2,
105 Closure: 3,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000106 Catch: 4,
107 Block: 5 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000108
109
110// Current debug state.
111const kNoFrame = -1;
112Debug.State = {
113 currentFrame: kNoFrame,
Ben Murdoch086aeea2011-05-13 15:57:08 +0100114 displaySourceStartLine: -1,
115 displaySourceEndLine: -1,
Steve Blocka7e24c12009-10-30 11:49:00 +0000116 currentSourceLine: -1
117}
118var trace_compile = false; // Tracing all compile events?
Ben Murdoch086aeea2011-05-13 15:57:08 +0100119var trace_debug_json = false; // Tracing all debug json packets?
120var last_cmd_line = '';
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100121//var lol_is_enabled; // Set to true in d8.cc if LIVE_OBJECT_LIST is defined.
122var lol_next_dump_index = 0;
123const kDefaultLolLinesToPrintAtATime = 10;
124const kMaxLolLinesToPrintAtATime = 1000;
Ben Murdoch086aeea2011-05-13 15:57:08 +0100125var repeat_cmd_line = '';
126var is_running = true;
127
128// Copied from debug-delay.js. This is needed below:
129function ScriptTypeFlag(type) {
130 return (1 << type);
131}
Steve Blocka7e24c12009-10-30 11:49:00 +0000132
133
134// Process a debugger JSON message into a display text and a running status.
135// This function returns an object with properties "text" and "running" holding
136// this information.
137function DebugMessageDetails(message) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100138 if (trace_debug_json) {
139 print("received: '" + message + "'");
140 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000141 // Convert the JSON string to an object.
142 var response = new ProtocolPackage(message);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100143 is_running = response.running();
Steve Blocka7e24c12009-10-30 11:49:00 +0000144
145 if (response.type() == 'event') {
146 return DebugEventDetails(response);
147 } else {
148 return DebugResponseDetails(response);
149 }
150}
151
152function DebugEventDetails(response) {
153 details = {text:'', running:false}
154
155 // Get the running state.
156 details.running = response.running();
157
158 var body = response.body();
159 var result = '';
160 switch (response.event()) {
161 case 'break':
162 if (body.breakpoints) {
163 result += 'breakpoint';
164 if (body.breakpoints.length > 1) {
165 result += 's';
166 }
167 result += ' #';
168 for (var i = 0; i < body.breakpoints.length; i++) {
169 if (i > 0) {
170 result += ', #';
171 }
172 result += body.breakpoints[i];
173 }
174 } else {
175 result += 'break';
176 }
177 result += ' in ';
178 result += body.invocationText;
179 result += ', ';
180 result += SourceInfo(body);
181 result += '\n';
182 result += SourceUnderline(body.sourceLineText, body.sourceColumn);
183 Debug.State.currentSourceLine = body.sourceLine;
Ben Murdoch086aeea2011-05-13 15:57:08 +0100184 Debug.State.displaySourceStartLine = -1;
185 Debug.State.displaySourceEndLine = -1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000186 Debug.State.currentFrame = 0;
187 details.text = result;
188 break;
Steve Block6ded16b2010-05-10 14:33:55 +0100189
Steve Blocka7e24c12009-10-30 11:49:00 +0000190 case 'exception':
191 if (body.uncaught) {
192 result += 'Uncaught: ';
193 } else {
194 result += 'Exception: ';
195 }
196 result += '"';
197 result += body.exception.text;
198 result += '"';
199 if (body.sourceLine >= 0) {
200 result += ', ';
201 result += SourceInfo(body);
202 result += '\n';
203 result += SourceUnderline(body.sourceLineText, body.sourceColumn);
204 Debug.State.currentSourceLine = body.sourceLine;
Ben Murdoch086aeea2011-05-13 15:57:08 +0100205 Debug.State.displaySourceStartLine = -1;
206 Debug.State.displaySourceEndLine = -1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000207 Debug.State.currentFrame = 0;
208 } else {
209 result += ' (empty stack)';
210 Debug.State.currentSourceLine = -1;
Ben Murdoch086aeea2011-05-13 15:57:08 +0100211 Debug.State.displaySourceStartLine = -1;
212 Debug.State.displaySourceEndLine = -1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 Debug.State.currentFrame = kNoFrame;
214 }
215 details.text = result;
216 break;
217
218 case 'afterCompile':
219 if (trace_compile) {
220 result = 'Source ' + body.script.name + ' compiled:\n'
221 var source = body.script.source;
222 if (!(source[source.length - 1] == '\n')) {
223 result += source;
224 } else {
225 result += source.substring(0, source.length - 1);
226 }
227 }
228 details.text = result;
229 break;
230
Ben Murdoch086aeea2011-05-13 15:57:08 +0100231 case 'scriptCollected':
232 details.text = result;
233 break;
234
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 default:
236 details.text = 'Unknown debug event ' + response.event();
237 }
238
239 return details;
240};
241
242
243function SourceInfo(body) {
244 var result = '';
Steve Block6ded16b2010-05-10 14:33:55 +0100245
Steve Blocka7e24c12009-10-30 11:49:00 +0000246 if (body.script) {
247 if (body.script.name) {
248 result += body.script.name;
249 } else {
250 result += '[unnamed]';
251 }
252 }
253 result += ' line ';
254 result += body.sourceLine + 1;
255 result += ' column ';
256 result += body.sourceColumn + 1;
Steve Block6ded16b2010-05-10 14:33:55 +0100257
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 return result;
259}
260
261
262function SourceUnderline(source_text, position) {
263 if (!source_text) {
264 return;
265 }
266
267 // Create an underline with a caret pointing to the source position. If the
268 // source contains a tab character the underline will have a tab character in
269 // the same place otherwise the underline will have a space character.
270 var underline = '';
271 for (var i = 0; i < position; i++) {
272 if (source_text[i] == '\t') {
273 underline += '\t';
274 } else {
275 underline += ' ';
276 }
277 }
278 underline += '^';
279
280 // Return the source line text with the underline beneath.
281 return source_text + '\n' + underline;
282};
283
284
285// Converts a text command to a JSON request.
286function DebugCommandToJSONRequest(cmd_line) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100287 var result = new DebugRequest(cmd_line).JSONRequest();
288 if (trace_debug_json && result) {
289 print("sending: '" + result + "'");
290 }
291 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000292};
293
294
295function DebugRequest(cmd_line) {
296 // If the very first character is a { assume that a JSON request have been
297 // entered as a command. Converting that to a JSON request is trivial.
298 if (cmd_line && cmd_line.length > 0 && cmd_line.charAt(0) == '{') {
299 this.request_ = cmd_line;
300 return;
301 }
302
Ben Murdoch086aeea2011-05-13 15:57:08 +0100303 // Check for a simple carriage return to repeat the last command:
304 var is_repeating = false;
305 if (cmd_line == '\n') {
306 if (is_running) {
307 cmd_line = 'break'; // Not in debugger mode, break with a frame request.
308 } else {
309 cmd_line = repeat_cmd_line; // use command to repeat.
310 is_repeating = true;
311 }
312 }
313 if (!is_running) { // Only save the command if in debugger mode.
314 repeat_cmd_line = cmd_line; // save last command.
315 }
316
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 // Trim string for leading and trailing whitespace.
318 cmd_line = cmd_line.replace(/^\s+|\s+$/g, '');
319
320 // Find the command.
321 var pos = cmd_line.indexOf(' ');
322 var cmd;
323 var args;
324 if (pos == -1) {
325 cmd = cmd_line;
326 args = '';
327 } else {
328 cmd = cmd_line.slice(0, pos);
329 args = cmd_line.slice(pos).replace(/^\s+|\s+$/g, '');
330 }
331
Ben Murdoch086aeea2011-05-13 15:57:08 +0100332 if ((cmd === undefined) || !cmd) {
333 this.request_ = void 0;
334 return;
335 }
336
337 last_cmd = cmd;
338
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 // Switch on command.
340 switch (cmd) {
341 case 'continue':
342 case 'c':
343 this.request_ = this.continueCommandToJSONRequest_(args);
344 break;
345
346 case 'step':
347 case 's':
Ben Murdoch086aeea2011-05-13 15:57:08 +0100348 this.request_ = this.stepCommandToJSONRequest_(args, 'in');
349 break;
350
351 case 'stepi':
352 case 'si':
353 this.request_ = this.stepCommandToJSONRequest_(args, 'min');
354 break;
355
356 case 'next':
357 case 'n':
358 this.request_ = this.stepCommandToJSONRequest_(args, 'next');
359 break;
360
361 case 'finish':
362 case 'fin':
363 this.request_ = this.stepCommandToJSONRequest_(args, 'out');
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 break;
365
366 case 'backtrace':
367 case 'bt':
368 this.request_ = this.backtraceCommandToJSONRequest_(args);
369 break;
Steve Block6ded16b2010-05-10 14:33:55 +0100370
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 case 'frame':
372 case 'f':
373 this.request_ = this.frameCommandToJSONRequest_(args);
374 break;
Steve Block6ded16b2010-05-10 14:33:55 +0100375
Steve Blocka7e24c12009-10-30 11:49:00 +0000376 case 'scopes':
377 this.request_ = this.scopesCommandToJSONRequest_(args);
378 break;
Steve Block6ded16b2010-05-10 14:33:55 +0100379
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 case 'scope':
381 this.request_ = this.scopeCommandToJSONRequest_(args);
382 break;
Steve Block6ded16b2010-05-10 14:33:55 +0100383
Ben Murdoch086aeea2011-05-13 15:57:08 +0100384 case 'disconnect':
385 case 'exit':
386 case 'quit':
387 this.request_ = this.disconnectCommandToJSONRequest_(args);
388 break;
389
390 case 'up':
391 this.request_ =
392 this.frameCommandToJSONRequest_('' +
393 (Debug.State.currentFrame + 1));
394 break;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000395
Ben Murdoch086aeea2011-05-13 15:57:08 +0100396 case 'down':
397 case 'do':
398 this.request_ =
399 this.frameCommandToJSONRequest_('' +
400 (Debug.State.currentFrame - 1));
401 break;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000402
Ben Murdoch086aeea2011-05-13 15:57:08 +0100403 case 'set':
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 case 'print':
405 case 'p':
406 this.request_ = this.printCommandToJSONRequest_(args);
407 break;
408
409 case 'dir':
410 this.request_ = this.dirCommandToJSONRequest_(args);
411 break;
412
413 case 'references':
414 this.request_ = this.referencesCommandToJSONRequest_(args);
415 break;
416
417 case 'instances':
418 this.request_ = this.instancesCommandToJSONRequest_(args);
419 break;
420
Ben Murdoch086aeea2011-05-13 15:57:08 +0100421 case 'list':
422 case 'l':
423 this.request_ = this.listCommandToJSONRequest_(args);
424 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 case 'source':
426 this.request_ = this.sourceCommandToJSONRequest_(args);
427 break;
Steve Block6ded16b2010-05-10 14:33:55 +0100428
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 case 'scripts':
Ben Murdoch086aeea2011-05-13 15:57:08 +0100430 case 'script':
431 case 'scr':
Steve Blocka7e24c12009-10-30 11:49:00 +0000432 this.request_ = this.scriptsCommandToJSONRequest_(args);
433 break;
Steve Block6ded16b2010-05-10 14:33:55 +0100434
Steve Blocka7e24c12009-10-30 11:49:00 +0000435 case 'break':
436 case 'b':
437 this.request_ = this.breakCommandToJSONRequest_(args);
438 break;
Steve Block6ded16b2010-05-10 14:33:55 +0100439
Kristian Monsen25f61362010-05-21 11:50:48 +0100440 case 'breakpoints':
441 case 'bb':
442 this.request_ = this.breakpointsCommandToJSONRequest_(args);
443 break;
444
Steve Blocka7e24c12009-10-30 11:49:00 +0000445 case 'clear':
Ben Murdoch086aeea2011-05-13 15:57:08 +0100446 case 'delete':
447 case 'd':
Steve Blocka7e24c12009-10-30 11:49:00 +0000448 this.request_ = this.clearCommandToJSONRequest_(args);
449 break;
450
451 case 'threads':
452 this.request_ = this.threadsCommandToJSONRequest_(args);
453 break;
454
Ben Murdoch086aeea2011-05-13 15:57:08 +0100455 case 'cond':
456 this.request_ = this.changeBreakpointCommandToJSONRequest_(args, 'cond');
457 break;
458
459 case 'enable':
460 case 'en':
461 this.request_ =
462 this.changeBreakpointCommandToJSONRequest_(args, 'enable');
463 break;
464
465 case 'disable':
466 case 'dis':
467 this.request_ =
468 this.changeBreakpointCommandToJSONRequest_(args, 'disable');
469 break;
470
471 case 'ignore':
472 this.request_ =
473 this.changeBreakpointCommandToJSONRequest_(args, 'ignore');
474 break;
475
476 case 'info':
477 case 'inf':
478 this.request_ = this.infoCommandToJSONRequest_(args);
479 break;
480
481 case 'flags':
482 this.request_ = this.v8FlagsToJSONRequest_(args);
483 break;
484
485 case 'gc':
486 this.request_ = this.gcToJSONRequest_(args);
487 break;
488
Steve Blocka7e24c12009-10-30 11:49:00 +0000489 case 'trace':
Ben Murdoch086aeea2011-05-13 15:57:08 +0100490 case 'tr':
Steve Blocka7e24c12009-10-30 11:49:00 +0000491 // Return undefined to indicate command handled internally (no JSON).
492 this.request_ = void 0;
493 this.traceCommand_(args);
494 break;
495
496 case 'help':
497 case '?':
498 this.helpCommand_(args);
499 // Return undefined to indicate command handled internally (no JSON).
500 this.request_ = void 0;
501 break;
502
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100503 case 'liveobjectlist':
504 case 'lol':
505 if (lol_is_enabled) {
506 this.request_ = this.lolToJSONRequest_(args, is_repeating);
507 break;
508 }
509
Steve Blocka7e24c12009-10-30 11:49:00 +0000510 default:
511 throw new Error('Unknown command "' + cmd + '"');
512 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000513}
514
515DebugRequest.prototype.JSONRequest = function() {
516 return this.request_;
517}
518
519
520function RequestPacket(command) {
521 this.seq = 0;
522 this.type = 'request';
523 this.command = command;
524}
525
526
527RequestPacket.prototype.toJSONProtocol = function() {
528 // Encode the protocol header.
529 var json = '{';
530 json += '"seq":' + this.seq;
531 json += ',"type":"' + this.type + '"';
532 if (this.command) {
533 json += ',"command":' + StringToJSON_(this.command);
534 }
535 if (this.arguments) {
536 json += ',"arguments":';
537 // Encode the arguments part.
538 if (this.arguments.toJSONProtocol) {
539 json += this.arguments.toJSONProtocol()
540 } else {
541 json += SimpleObjectToJSON_(this.arguments);
542 }
543 }
544 json += '}';
545 return json;
546}
547
548
549DebugRequest.prototype.createRequest = function(command) {
550 return new RequestPacket(command);
551};
552
553
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100554// Note: we use detected command repetition as a signal for continuation here.
555DebugRequest.prototype.createLOLRequest = function(command,
556 start_index,
557 lines_to_dump,
558 is_continuation) {
559 if (is_continuation) {
560 start_index = lol_next_dump_index;
561 }
562
563 if (lines_to_dump) {
564 lines_to_dump = parseInt(lines_to_dump);
565 } else {
566 lines_to_dump = kDefaultLolLinesToPrintAtATime;
567 }
568 if (lines_to_dump > kMaxLolLinesToPrintAtATime) {
569 lines_to_dump = kMaxLolLinesToPrintAtATime;
570 }
571
572 // Save the next start_index to dump from:
573 lol_next_dump_index = start_index + lines_to_dump;
574
575 var request = this.createRequest(command);
576 request.arguments = {};
577 request.arguments.start = start_index;
578 request.arguments.count = lines_to_dump;
579
580 return request;
581};
582
583
Steve Blocka7e24c12009-10-30 11:49:00 +0000584// Create a JSON request for the evaluation command.
585DebugRequest.prototype.makeEvaluateJSONRequest_ = function(expression) {
586 // Global varaible used to store whether a handle was requested.
587 lookup_handle = null;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100588
589 if (lol_is_enabled) {
590 // Check if the expression is a obj id in the form @<obj id>.
591 var obj_id_match = expression.match(/^@([0-9]+)$/);
592 if (obj_id_match) {
593 var obj_id = parseInt(obj_id_match[1]);
594 // Build a dump request.
595 var request = this.createRequest('getobj');
596 request.arguments = {};
597 request.arguments.obj_id = obj_id;
598 return request.toJSONProtocol();
599 }
600 }
601
Steve Blocka7e24c12009-10-30 11:49:00 +0000602 // Check if the expression is a handle id in the form #<handle>#.
603 var handle_match = expression.match(/^#([0-9]*)#$/);
604 if (handle_match) {
605 // Remember the handle requested in a global variable.
606 lookup_handle = parseInt(handle_match[1]);
607 // Build a lookup request.
608 var request = this.createRequest('lookup');
609 request.arguments = {};
610 request.arguments.handles = [ lookup_handle ];
611 return request.toJSONProtocol();
612 } else {
613 // Build an evaluate request.
614 var request = this.createRequest('evaluate');
615 request.arguments = {};
616 request.arguments.expression = expression;
617 // Request a global evaluation if there is no current frame.
618 if (Debug.State.currentFrame == kNoFrame) {
619 request.arguments.global = true;
620 }
621 return request.toJSONProtocol();
622 }
623};
624
625
626// Create a JSON request for the references/instances command.
627DebugRequest.prototype.makeReferencesJSONRequest_ = function(handle, type) {
628 // Build a references request.
629 var handle_match = handle.match(/^#([0-9]*)#$/);
630 if (handle_match) {
631 var request = this.createRequest('references');
632 request.arguments = {};
633 request.arguments.type = type;
634 request.arguments.handle = parseInt(handle_match[1]);
635 return request.toJSONProtocol();
636 } else {
637 throw new Error('Invalid object id.');
638 }
639};
640
641
642// Create a JSON request for the continue command.
643DebugRequest.prototype.continueCommandToJSONRequest_ = function(args) {
644 var request = this.createRequest('continue');
645 return request.toJSONProtocol();
646};
647
648
649// Create a JSON request for the step command.
Ben Murdoch086aeea2011-05-13 15:57:08 +0100650DebugRequest.prototype.stepCommandToJSONRequest_ = function(args, type) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000651 // Requesting a step is through the continue command with additional
652 // arguments.
653 var request = this.createRequest('continue');
654 request.arguments = {};
655
656 // Process arguments if any.
Ben Murdoch086aeea2011-05-13 15:57:08 +0100657
658 // Only process args if the command is 'step' which is indicated by type being
659 // set to 'in'. For all other commands, ignore the args.
Steve Blocka7e24c12009-10-30 11:49:00 +0000660 if (args && args.length > 0) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100661 args = args.split(/\s+/g);
Steve Blocka7e24c12009-10-30 11:49:00 +0000662
663 if (args.length > 2) {
664 throw new Error('Invalid step arguments.');
665 }
666
667 if (args.length > 0) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100668 // Check if we have a gdb stype step command. If so, the 1st arg would
669 // be the step count. If it's not a number, then assume that we're
670 // parsing for the legacy v8 step command.
671 var stepcount = Number(args[0]);
672 if (stepcount == Number.NaN) {
673 // No step count at arg 1. Process as legacy d8 step command:
674 if (args.length == 2) {
675 var stepcount = parseInt(args[1]);
676 if (isNaN(stepcount) || stepcount <= 0) {
677 throw new Error('Invalid step count argument "' + args[0] + '".');
678 }
679 request.arguments.stepcount = stepcount;
Steve Blocka7e24c12009-10-30 11:49:00 +0000680 }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100681
682 // Get the step action.
683 switch (args[0]) {
684 case 'in':
685 case 'i':
686 request.arguments.stepaction = 'in';
687 break;
688
689 case 'min':
690 case 'm':
691 request.arguments.stepaction = 'min';
692 break;
693
694 case 'next':
695 case 'n':
696 request.arguments.stepaction = 'next';
697 break;
698
699 case 'out':
700 case 'o':
701 request.arguments.stepaction = 'out';
702 break;
703
704 default:
705 throw new Error('Invalid step argument "' + args[0] + '".');
706 }
707
708 } else {
709 // gdb style step commands:
710 request.arguments.stepaction = type;
Steve Blocka7e24c12009-10-30 11:49:00 +0000711 request.arguments.stepcount = stepcount;
712 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000713 }
714 } else {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100715 // Default is step of the specified type.
716 request.arguments.stepaction = type;
Steve Blocka7e24c12009-10-30 11:49:00 +0000717 }
718
719 return request.toJSONProtocol();
720};
721
722
723// Create a JSON request for the backtrace command.
724DebugRequest.prototype.backtraceCommandToJSONRequest_ = function(args) {
725 // Build a backtrace request from the text command.
726 var request = this.createRequest('backtrace');
Steve Block6ded16b2010-05-10 14:33:55 +0100727
Steve Blocka7e24c12009-10-30 11:49:00 +0000728 // Default is to show top 10 frames.
729 request.arguments = {};
730 request.arguments.fromFrame = 0;
731 request.arguments.toFrame = 10;
732
733 args = args.split(/\s*[ ]+\s*/g);
734 if (args.length == 1 && args[0].length > 0) {
735 var frameCount = parseInt(args[0]);
736 if (frameCount > 0) {
737 // Show top frames.
738 request.arguments.fromFrame = 0;
739 request.arguments.toFrame = frameCount;
740 } else {
741 // Show bottom frames.
742 request.arguments.fromFrame = 0;
743 request.arguments.toFrame = -frameCount;
744 request.arguments.bottom = true;
745 }
746 } else if (args.length == 2) {
747 var fromFrame = parseInt(args[0]);
748 var toFrame = parseInt(args[1]);
749 if (isNaN(fromFrame) || fromFrame < 0) {
750 throw new Error('Invalid start frame argument "' + args[0] + '".');
751 }
752 if (isNaN(toFrame) || toFrame < 0) {
753 throw new Error('Invalid end frame argument "' + args[1] + '".');
754 }
755 if (fromFrame > toFrame) {
756 throw new Error('Invalid arguments start frame cannot be larger ' +
757 'than end frame.');
758 }
759 // Show frame range.
760 request.arguments.fromFrame = fromFrame;
761 request.arguments.toFrame = toFrame + 1;
762 } else if (args.length > 2) {
763 throw new Error('Invalid backtrace arguments.');
764 }
765
766 return request.toJSONProtocol();
767};
768
769
770// Create a JSON request for the frame command.
771DebugRequest.prototype.frameCommandToJSONRequest_ = function(args) {
772 // Build a frame request from the text command.
773 var request = this.createRequest('frame');
774 args = args.split(/\s*[ ]+\s*/g);
775 if (args.length > 0 && args[0].length > 0) {
776 request.arguments = {};
777 request.arguments.number = args[0];
778 }
779 return request.toJSONProtocol();
780};
781
782
783// Create a JSON request for the scopes command.
784DebugRequest.prototype.scopesCommandToJSONRequest_ = function(args) {
785 // Build a scopes request from the text command.
786 var request = this.createRequest('scopes');
787 return request.toJSONProtocol();
788};
789
790
791// Create a JSON request for the scope command.
792DebugRequest.prototype.scopeCommandToJSONRequest_ = function(args) {
793 // Build a scope request from the text command.
794 var request = this.createRequest('scope');
795 args = args.split(/\s*[ ]+\s*/g);
796 if (args.length > 0 && args[0].length > 0) {
797 request.arguments = {};
798 request.arguments.number = args[0];
799 }
800 return request.toJSONProtocol();
801};
802
803
804// Create a JSON request for the print command.
805DebugRequest.prototype.printCommandToJSONRequest_ = function(args) {
806 // Build an evaluate request from the text command.
807 if (args.length == 0) {
808 throw new Error('Missing expression.');
809 }
810 return this.makeEvaluateJSONRequest_(args);
811};
812
813
814// Create a JSON request for the dir command.
815DebugRequest.prototype.dirCommandToJSONRequest_ = function(args) {
816 // Build an evaluate request from the text command.
817 if (args.length == 0) {
818 throw new Error('Missing expression.');
819 }
820 return this.makeEvaluateJSONRequest_(args);
821};
822
823
824// Create a JSON request for the references command.
825DebugRequest.prototype.referencesCommandToJSONRequest_ = function(args) {
826 // Build an evaluate request from the text command.
827 if (args.length == 0) {
828 throw new Error('Missing object id.');
829 }
Steve Block6ded16b2010-05-10 14:33:55 +0100830
Steve Blocka7e24c12009-10-30 11:49:00 +0000831 return this.makeReferencesJSONRequest_(args, 'referencedBy');
832};
833
834
835// Create a JSON request for the instances command.
836DebugRequest.prototype.instancesCommandToJSONRequest_ = function(args) {
837 // Build an evaluate request from the text command.
838 if (args.length == 0) {
839 throw new Error('Missing object id.');
840 }
Steve Block6ded16b2010-05-10 14:33:55 +0100841
Steve Blocka7e24c12009-10-30 11:49:00 +0000842 // Build a references request.
843 return this.makeReferencesJSONRequest_(args, 'constructedBy');
844};
845
846
Ben Murdoch086aeea2011-05-13 15:57:08 +0100847// Create a JSON request for the list command.
848DebugRequest.prototype.listCommandToJSONRequest_ = function(args) {
849
850 // Default is ten lines starting five lines before the current location.
851 if (Debug.State.displaySourceEndLine == -1) {
852 // If we list forwards, we will start listing after the last source end
853 // line. Set it to start from 5 lines before the current location.
854 Debug.State.displaySourceEndLine = Debug.State.currentSourceLine - 5;
855 // If we list backwards, we will start listing backwards from the last
856 // source start line. Set it to start from 1 lines before the current
857 // location.
858 Debug.State.displaySourceStartLine = Debug.State.currentSourceLine + 1;
859 }
860
861 var from = Debug.State.displaySourceEndLine + 1;
862 var lines = 10;
863
864 // Parse the arguments.
865 args = args.split(/\s*,\s*/g);
866 if (args == '') {
867 } else if ((args.length == 1) && (args[0] == '-')) {
868 from = Debug.State.displaySourceStartLine - lines;
869 } else if (args.length == 2) {
870 from = parseInt(args[0]);
871 lines = parseInt(args[1]) - from + 1; // inclusive of the ending line.
872 } else {
873 throw new Error('Invalid list arguments.');
874 }
875 Debug.State.displaySourceStartLine = from;
876 Debug.State.displaySourceEndLine = from + lines - 1;
877 var sourceArgs = '' + from + ' ' + lines;
878 return this.sourceCommandToJSONRequest_(sourceArgs);
879};
880
881
Steve Blocka7e24c12009-10-30 11:49:00 +0000882// Create a JSON request for the source command.
883DebugRequest.prototype.sourceCommandToJSONRequest_ = function(args) {
884 // Build a evaluate request from the text command.
885 var request = this.createRequest('source');
886
887 // Default is ten lines starting five lines before the current location.
888 var from = Debug.State.currentSourceLine - 5;
889 var lines = 10;
890
891 // Parse the arguments.
892 args = args.split(/\s*[ ]+\s*/g);
893 if (args.length > 1 && args[0].length > 0 && args[1].length > 0) {
894 from = parseInt(args[0]) - 1;
895 lines = parseInt(args[1]);
896 } else if (args.length > 0 && args[0].length > 0) {
897 from = parseInt(args[0]) - 1;
898 }
899
900 if (from < 0) from = 0;
901 if (lines < 0) lines = 10;
902
903 // Request source arround current source location.
904 request.arguments = {};
905 request.arguments.fromLine = from;
906 request.arguments.toLine = from + lines;
907
908 return request.toJSONProtocol();
909};
910
911
912// Create a JSON request for the scripts command.
913DebugRequest.prototype.scriptsCommandToJSONRequest_ = function(args) {
914 // Build a evaluate request from the text command.
915 var request = this.createRequest('scripts');
916
917 // Process arguments if any.
918 if (args && args.length > 0) {
919 args = args.split(/\s*[ ]+\s*/g);
920
921 if (args.length > 1) {
922 throw new Error('Invalid scripts arguments.');
923 }
924
925 request.arguments = {};
926 switch (args[0]) {
927 case 'natives':
928 request.arguments.types = ScriptTypeFlag(Debug.ScriptType.Native);
929 break;
Steve Block6ded16b2010-05-10 14:33:55 +0100930
Steve Blocka7e24c12009-10-30 11:49:00 +0000931 case 'extensions':
932 request.arguments.types = ScriptTypeFlag(Debug.ScriptType.Extension);
933 break;
Steve Block6ded16b2010-05-10 14:33:55 +0100934
Steve Blocka7e24c12009-10-30 11:49:00 +0000935 case 'all':
936 request.arguments.types =
937 ScriptTypeFlag(Debug.ScriptType.Normal) |
938 ScriptTypeFlag(Debug.ScriptType.Native) |
939 ScriptTypeFlag(Debug.ScriptType.Extension);
940 break;
Steve Block6ded16b2010-05-10 14:33:55 +0100941
Steve Blocka7e24c12009-10-30 11:49:00 +0000942 default:
Ben Murdoch086aeea2011-05-13 15:57:08 +0100943 // If the arg is not one of the know one aboves, then it must be a
944 // filter used for filtering the results:
945 request.arguments.filter = args[0];
946 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000947 }
948 }
949
950 return request.toJSONProtocol();
951};
952
953
954// Create a JSON request for the break command.
955DebugRequest.prototype.breakCommandToJSONRequest_ = function(args) {
956 // Build a evaluate request from the text command.
Steve Blocka7e24c12009-10-30 11:49:00 +0000957 // Process arguments if any.
958 if (args && args.length > 0) {
959 var target = args;
960 var type = 'function';
961 var line;
962 var column;
963 var condition;
964 var pos;
965
Steve Block6ded16b2010-05-10 14:33:55 +0100966 var request = this.createRequest('setbreakpoint');
967
Ben Murdoch086aeea2011-05-13 15:57:08 +0100968 // Break the args into target spec and condition if appropriate.
969
Steve Blocka7e24c12009-10-30 11:49:00 +0000970 // Check for breakpoint condition.
971 pos = args.indexOf(' ');
972 if (pos > 0) {
973 target = args.substring(0, pos);
974 condition = args.substring(pos + 1, args.length);
975 }
976
977 // Check for script breakpoint (name:line[:column]). If no ':' in break
978 // specification it is considered a function break point.
979 pos = target.indexOf(':');
980 if (pos > 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000981 var tmp = target.substring(pos + 1, target.length);
982 target = target.substring(0, pos);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000983 if (target[0] == '/' && target[target.length - 1] == '/') {
984 type = 'scriptRegExp';
985 target = target.substring(1, target.length - 1);
986 } else {
987 type = 'script';
988 }
Steve Block6ded16b2010-05-10 14:33:55 +0100989
Steve Blocka7e24c12009-10-30 11:49:00 +0000990 // Check for both line and column.
991 pos = tmp.indexOf(':');
992 if (pos > 0) {
993 column = parseInt(tmp.substring(pos + 1, tmp.length)) - 1;
994 line = parseInt(tmp.substring(0, pos)) - 1;
995 } else {
996 line = parseInt(tmp) - 1;
997 }
998 } else if (target[0] == '#' && target[target.length - 1] == '#') {
999 type = 'handle';
1000 target = target.substring(1, target.length - 1);
1001 } else {
1002 type = 'function';
1003 }
Steve Block6ded16b2010-05-10 14:33:55 +01001004
Steve Blocka7e24c12009-10-30 11:49:00 +00001005 request.arguments = {};
1006 request.arguments.type = type;
1007 request.arguments.target = target;
1008 request.arguments.line = line;
1009 request.arguments.column = column;
1010 request.arguments.condition = condition;
1011 } else {
Steve Block6ded16b2010-05-10 14:33:55 +01001012 var request = this.createRequest('suspend');
Steve Blocka7e24c12009-10-30 11:49:00 +00001013 }
1014
1015 return request.toJSONProtocol();
1016};
1017
1018
Kristian Monsen25f61362010-05-21 11:50:48 +01001019DebugRequest.prototype.breakpointsCommandToJSONRequest_ = function(args) {
1020 if (args && args.length > 0) {
1021 throw new Error('Unexpected arguments.');
1022 }
1023 var request = this.createRequest('listbreakpoints');
1024 return request.toJSONProtocol();
1025};
1026
1027
Steve Blocka7e24c12009-10-30 11:49:00 +00001028// Create a JSON request for the clear command.
1029DebugRequest.prototype.clearCommandToJSONRequest_ = function(args) {
1030 // Build a evaluate request from the text command.
1031 var request = this.createRequest('clearbreakpoint');
1032
1033 // Process arguments if any.
1034 if (args && args.length > 0) {
1035 request.arguments = {};
1036 request.arguments.breakpoint = parseInt(args);
1037 } else {
1038 throw new Error('Invalid break arguments.');
1039 }
1040
1041 return request.toJSONProtocol();
1042};
1043
1044
Ben Murdoch086aeea2011-05-13 15:57:08 +01001045// Create a JSON request for the change breakpoint command.
1046DebugRequest.prototype.changeBreakpointCommandToJSONRequest_ =
1047 function(args, command) {
1048
1049 var request;
1050
1051 // Check for exception breaks first:
1052 // en[able] exc[eptions] [all|unc[aught]]
1053 // en[able] [all|unc[aught]] exc[eptions]
1054 // dis[able] exc[eptions] [all|unc[aught]]
1055 // dis[able] [all|unc[aught]] exc[eptions]
1056 if ((command == 'enable' || command == 'disable') &&
1057 args && args.length > 1) {
1058 var nextPos = args.indexOf(' ');
1059 var arg1 = (nextPos > 0) ? args.substring(0, nextPos) : args;
1060 var excType = null;
1061
1062 // Check for:
1063 // en[able] exc[eptions] [all|unc[aught]]
1064 // dis[able] exc[eptions] [all|unc[aught]]
1065 if (arg1 == 'exc' || arg1 == 'exception' || arg1 == 'exceptions') {
1066
1067 var arg2 = (nextPos > 0) ?
1068 args.substring(nextPos + 1, args.length) : 'all';
1069 if (!arg2) {
1070 arg2 = 'all'; // if unspecified, set for all.
1071 } if (arg2 == 'unc') { // check for short cut.
1072 arg2 = 'uncaught';
1073 }
1074 excType = arg2;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001075
Ben Murdoch086aeea2011-05-13 15:57:08 +01001076 // Check for:
1077 // en[able] [all|unc[aught]] exc[eptions]
1078 // dis[able] [all|unc[aught]] exc[eptions]
1079 } else if (arg1 == 'all' || arg1 == 'unc' || arg1 == 'uncaught') {
1080
1081 var arg2 = (nextPos > 0) ?
1082 args.substring(nextPos + 1, args.length) : null;
1083 if (arg2 == 'exc' || arg1 == 'exception' || arg1 == 'exceptions') {
1084 excType = arg1;
1085 if (excType == 'unc') {
1086 excType = 'uncaught';
1087 }
1088 }
1089 }
1090
1091 // If we matched one of the command formats, then excType will be non-null:
1092 if (excType) {
1093 // Build a evaluate request from the text command.
1094 request = this.createRequest('setexceptionbreak');
1095
1096 request.arguments = {};
1097 request.arguments.type = excType;
1098 request.arguments.enabled = (command == 'enable');
1099
1100 return request.toJSONProtocol();
1101 }
1102 }
1103
1104 // Build a evaluate request from the text command.
1105 request = this.createRequest('changebreakpoint');
1106
1107 // Process arguments if any.
1108 if (args && args.length > 0) {
1109 request.arguments = {};
1110 var pos = args.indexOf(' ');
1111 var breakpointArg = args;
1112 var otherArgs;
1113 if (pos > 0) {
1114 breakpointArg = args.substring(0, pos);
1115 otherArgs = args.substring(pos + 1, args.length);
1116 }
1117
1118 request.arguments.breakpoint = parseInt(breakpointArg);
1119
1120 switch(command) {
1121 case 'cond':
1122 request.arguments.condition = otherArgs ? otherArgs : null;
1123 break;
1124 case 'enable':
1125 request.arguments.enabled = true;
1126 break;
1127 case 'disable':
1128 request.arguments.enabled = false;
1129 break;
1130 case 'ignore':
1131 request.arguments.ignoreCount = parseInt(otherArgs);
1132 break;
1133 default:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001134 throw new Error('Invalid arguments.');
Ben Murdoch086aeea2011-05-13 15:57:08 +01001135 }
1136 } else {
1137 throw new Error('Invalid arguments.');
1138 }
1139
1140 return request.toJSONProtocol();
1141};
1142
1143
1144// Create a JSON request for the disconnect command.
1145DebugRequest.prototype.disconnectCommandToJSONRequest_ = function(args) {
1146 var request;
1147 request = this.createRequest('disconnect');
1148 return request.toJSONProtocol();
1149};
1150
1151
1152// Create a JSON request for the info command.
1153DebugRequest.prototype.infoCommandToJSONRequest_ = function(args) {
1154 var request;
1155 if (args && (args == 'break' || args == 'br')) {
1156 // Build a evaluate request from the text command.
1157 request = this.createRequest('listbreakpoints');
1158 last_cmd = 'info break';
1159 } else if (args && (args == 'locals' || args == 'lo')) {
1160 // Build a evaluate request from the text command.
1161 request = this.createRequest('frame');
1162 last_cmd = 'info locals';
1163 } else if (args && (args == 'args' || args == 'ar')) {
1164 // Build a evaluate request from the text command.
1165 request = this.createRequest('frame');
1166 last_cmd = 'info args';
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001167 } else if (lol_is_enabled &&
1168 args && (args == 'liveobjectlist' || args == 'lol')) {
1169 // Build a evaluate request from the text command.
1170 return this.liveObjectListToJSONRequest_(null);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001171 } else {
1172 throw new Error('Invalid info arguments.');
1173 }
1174
1175 return request.toJSONProtocol();
1176};
1177
1178
1179DebugRequest.prototype.v8FlagsToJSONRequest_ = function(args) {
1180 var request;
1181 request = this.createRequest('v8flags');
1182 request.arguments = {};
1183 request.arguments.flags = args;
1184 return request.toJSONProtocol();
1185};
1186
1187
1188DebugRequest.prototype.gcToJSONRequest_ = function(args) {
1189 var request;
1190 if (!args) {
1191 args = 'all';
1192 }
1193 var args = args.split(/\s+/g);
1194 var cmd = args[0];
1195
1196 switch(cmd) {
1197 case 'all':
1198 case 'quick':
1199 case 'full':
1200 case 'young':
1201 case 'old':
1202 case 'compact':
1203 case 'sweep':
1204 case 'scavenge': {
1205 if (cmd == 'young') { cmd = 'quick'; }
1206 else if (cmd == 'old') { cmd = 'full'; }
1207
1208 request = this.createRequest('gc');
1209 request.arguments = {};
1210 request.arguments.type = cmd;
1211 break;
1212 }
1213 // Else fall thru to the default case below to report the error.
1214 default:
1215 throw new Error('Missing arguments after ' + cmd + '.');
1216 }
1217 return request.toJSONProtocol();
1218};
1219
1220
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001221// Args: [v[erbose]] [<N>] [i[ndex] <i>] [t[ype] <type>] [sp[ace] <space>]
1222DebugRequest.prototype.lolMakeListRequest =
1223 function(cmd, args, first_arg_index, is_repeating) {
1224
1225 var request;
1226 var start_index = 0;
1227 var dump_limit = void 0;
1228 var type_filter = void 0;
1229 var space_filter = void 0;
1230 var prop_filter = void 0;
1231 var is_verbose = false;
1232 var i;
1233
1234 for (i = first_arg_index; i < args.length; i++) {
1235 var arg = args[i];
1236 // Check for [v[erbose]]:
1237 if (arg === 'verbose' || arg === 'v') {
1238 // Nothing to do. This is already implied by args.length > 3.
1239 is_verbose = true;
1240
1241 // Check for [<N>]:
1242 } else if (arg.match(/^[0-9]+$/)) {
1243 dump_limit = arg;
1244 is_verbose = true;
1245
1246 // Check for i[ndex] <i>:
1247 } else if (arg === 'index' || arg === 'i') {
1248 i++;
1249 if (args.length < i) {
1250 throw new Error('Missing index after ' + arg + '.');
1251 }
1252 start_index = parseInt(args[i]);
1253 // The user input start index starts at 1:
1254 if (start_index <= 0) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001255 throw new Error('Invalid index ' + args[i] + '.');
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001256 }
1257 start_index -= 1;
1258 is_verbose = true;
1259
1260 // Check for t[ype] <type>:
1261 } else if (arg === 'type' || arg === 't') {
1262 i++;
1263 if (args.length < i) {
1264 throw new Error('Missing type after ' + arg + '.');
1265 }
1266 type_filter = args[i];
1267
1268 // Check for space <heap space name>:
1269 } else if (arg === 'space' || arg === 'sp') {
1270 i++;
1271 if (args.length < i) {
1272 throw new Error('Missing space name after ' + arg + '.');
1273 }
1274 space_filter = args[i];
1275
1276 // Check for property <prop name>:
1277 } else if (arg === 'property' || arg === 'prop') {
1278 i++;
1279 if (args.length < i) {
1280 throw new Error('Missing property name after ' + arg + '.');
1281 }
1282 prop_filter = args[i];
1283
1284 } else {
1285 throw new Error('Unknown args at ' + arg + '.');
1286 }
1287 }
1288
1289 // Build the verbose request:
1290 if (is_verbose) {
1291 request = this.createLOLRequest('lol-'+cmd,
1292 start_index,
1293 dump_limit,
1294 is_repeating);
1295 request.arguments.verbose = true;
1296 } else {
1297 request = this.createRequest('lol-'+cmd);
1298 request.arguments = {};
1299 }
1300
1301 request.arguments.filter = {};
1302 if (type_filter) {
1303 request.arguments.filter.type = type_filter;
1304 }
1305 if (space_filter) {
1306 request.arguments.filter.space = space_filter;
1307 }
1308 if (prop_filter) {
1309 request.arguments.filter.prop = prop_filter;
1310 }
1311
1312 return request;
1313}
1314
1315
1316function extractObjId(args) {
1317 var id = args;
1318 id = id.match(/^@([0-9]+)$/);
1319 if (id) {
1320 id = id[1];
1321 } else {
1322 throw new Error('Invalid obj id ' + args + '.');
1323 }
1324 return parseInt(id);
1325}
1326
1327
1328DebugRequest.prototype.lolToJSONRequest_ = function(args, is_repeating) {
1329 var request;
1330 // Use default command if one is not specified:
1331 if (!args) {
1332 args = 'info';
1333 }
1334
1335 var orig_args = args;
1336 var first_arg_index;
1337
1338 var arg, i;
1339 var args = args.split(/\s+/g);
1340 var cmd = args[0];
1341 var id;
1342
1343 // Command: <id> [v[erbose]] ...
1344 if (cmd.match(/^[0-9]+$/)) {
1345 // Convert to the padded list command:
1346 // Command: l[ist] <dummy> <id> [v[erbose]] ...
1347
1348 // Insert the implicit 'list' in front and process as normal:
1349 cmd = 'list';
1350 args.unshift(cmd);
1351 }
1352
1353 switch(cmd) {
1354 // Command: c[apture]
1355 case 'capture':
1356 case 'c':
1357 request = this.createRequest('lol-capture');
1358 break;
1359
1360 // Command: clear|d[elete] <id>|all
1361 case 'clear':
1362 case 'delete':
1363 case 'del': {
1364 if (args.length < 2) {
1365 throw new Error('Missing argument after ' + cmd + '.');
1366 } else if (args.length > 2) {
1367 throw new Error('Too many arguments after ' + cmd + '.');
1368 }
1369 id = args[1];
1370 if (id.match(/^[0-9]+$/)) {
1371 // Delete a specific lol record:
1372 request = this.createRequest('lol-delete');
1373 request.arguments = {};
1374 request.arguments.id = parseInt(id);
1375 } else if (id === 'all') {
1376 // Delete all:
1377 request = this.createRequest('lol-reset');
1378 } else {
1379 throw new Error('Invalid argument after ' + cmd + '.');
1380 }
1381 break;
1382 }
1383
1384 // Command: diff <id1> <id2> [<dump options>]
1385 case 'diff':
1386 first_arg_index = 3;
1387
1388 // Command: list <dummy> <id> [<dump options>]
1389 case 'list':
1390
1391 // Command: ret[ainers] <obj id> [<dump options>]
1392 case 'retainers':
1393 case 'ret':
1394 case 'retaining-paths':
1395 case 'rp': {
1396 if (cmd === 'ret') cmd = 'retainers';
1397 else if (cmd === 'rp') cmd = 'retaining-paths';
1398
1399 if (!first_arg_index) first_arg_index = 2;
1400
1401 if (args.length < first_arg_index) {
1402 throw new Error('Too few arguments after ' + cmd + '.');
1403 }
1404
1405 var request_cmd = (cmd === 'list') ? 'diff':cmd;
1406 request = this.lolMakeListRequest(request_cmd,
1407 args,
1408 first_arg_index,
1409 is_repeating);
1410
1411 if (cmd === 'diff') {
1412 request.arguments.id1 = parseInt(args[1]);
1413 request.arguments.id2 = parseInt(args[2]);
1414 } else if (cmd == 'list') {
1415 request.arguments.id1 = 0;
1416 request.arguments.id2 = parseInt(args[1]);
1417 } else {
1418 request.arguments.id = extractObjId(args[1]);
1419 }
1420 break;
1421 }
1422
1423 // Command: getid
1424 case 'getid': {
1425 request = this.createRequest('lol-getid');
1426 request.arguments = {};
1427 request.arguments.address = args[1];
1428 break;
1429 }
1430
1431 // Command: inf[o] [<N>]
1432 case 'info':
1433 case 'inf': {
1434 if (args.length > 2) {
1435 throw new Error('Too many arguments after ' + cmd + '.');
1436 }
1437 // Built the info request:
1438 request = this.createLOLRequest('lol-info', 0, args[1], is_repeating);
1439 break;
1440 }
1441
1442 // Command: path <obj id 1> <obj id 2>
1443 case 'path': {
1444 request = this.createRequest('lol-path');
1445 request.arguments = {};
1446 if (args.length > 2) {
1447 request.arguments.id1 = extractObjId(args[1]);
1448 request.arguments.id2 = extractObjId(args[2]);
1449 } else {
1450 request.arguments.id1 = 0;
1451 request.arguments.id2 = extractObjId(args[1]);
1452 }
1453 break;
1454 }
1455
1456 // Command: print
1457 case 'print': {
1458 request = this.createRequest('lol-print');
1459 request.arguments = {};
1460 request.arguments.id = extractObjId(args[1]);
1461 break;
1462 }
1463
1464 // Command: reset
1465 case 'reset': {
1466 request = this.createRequest('lol-reset');
1467 break;
1468 }
1469
1470 default:
1471 throw new Error('Invalid arguments.');
1472 }
1473 return request.toJSONProtocol();
1474};
1475
1476
Steve Blocka7e24c12009-10-30 11:49:00 +00001477// Create a JSON request for the threads command.
1478DebugRequest.prototype.threadsCommandToJSONRequest_ = function(args) {
1479 // Build a threads request from the text command.
1480 var request = this.createRequest('threads');
1481 return request.toJSONProtocol();
1482};
1483
1484
1485// Handle the trace command.
1486DebugRequest.prototype.traceCommand_ = function(args) {
1487 // Process arguments.
1488 if (args && args.length > 0) {
1489 if (args == 'compile') {
1490 trace_compile = !trace_compile;
1491 print('Tracing of compiled scripts ' + (trace_compile ? 'on' : 'off'));
Ben Murdoch086aeea2011-05-13 15:57:08 +01001492 } else if (args === 'debug json' || args === 'json' || args === 'packets') {
1493 trace_debug_json = !trace_debug_json;
1494 print('Tracing of debug json packets ' +
1495 (trace_debug_json ? 'on' : 'off'));
Steve Blocka7e24c12009-10-30 11:49:00 +00001496 } else {
1497 throw new Error('Invalid trace arguments.');
1498 }
1499 } else {
1500 throw new Error('Invalid trace arguments.');
1501 }
1502}
1503
1504// Handle the help command.
1505DebugRequest.prototype.helpCommand_ = function(args) {
1506 // Help os quite simple.
1507 if (args && args.length > 0) {
1508 print('warning: arguments to \'help\' are ignored');
1509 }
1510
Ben Murdoch086aeea2011-05-13 15:57:08 +01001511 print('Note: <> denotes symbollic values to be replaced with real values.');
1512 print('Note: [] denotes optional parts of commands, or optional options / arguments.');
1513 print(' e.g. d[elete] - you get the same command if you type d or delete.');
1514 print('');
1515 print('[break] - break as soon as possible');
1516 print('b[reak] location [condition]');
1517 print(' - break on named function: location is a function name');
1518 print(' - break on function: location is #<id>#');
1519 print(' - break on script position: location is name:line[:column]');
1520 print('');
1521 print('clear <breakpoint #> - deletes the specified user defined breakpoint');
1522 print('d[elete] <breakpoint #> - deletes the specified user defined breakpoint');
1523 print('dis[able] <breakpoint #> - disables the specified user defined breakpoint');
1524 print('dis[able] exc[eptions] [[all] | unc[aught]]');
1525 print(' - disables breaking on exceptions');
1526 print('en[able] <breakpoint #> - enables the specified user defined breakpoint');
1527 print('en[able] exc[eptions] [[all] | unc[aught]]');
1528 print(' - enables breaking on exceptions');
1529 print('');
1530 print('b[ack]t[race] [n] | [-n] | [from to]');
1531 print(' - prints the stack back trace');
1532 print('f[rame] - prints info about the current frame context');
1533 print('f[rame] <frame #> - set context to specified frame #');
Steve Blocka7e24c12009-10-30 11:49:00 +00001534 print('scopes');
1535 print('scope <scope #>');
Ben Murdoch086aeea2011-05-13 15:57:08 +01001536 print('');
1537 print('up - set context to caller of current frame');
1538 print('do[wn] - set context to callee of current frame');
1539 print('inf[o] br[eak] - prints info about breakpoints in use');
1540 print('inf[o] ar[gs] - prints info about arguments of the current function');
1541 print('inf[o] lo[cals] - prints info about locals in the current function');
1542 print('inf[o] liveobjectlist|lol - same as \'lol info\'');
1543 print('');
Steve Blocka7e24c12009-10-30 11:49:00 +00001544 print('step [in | next | out| min [step count]]');
Ben Murdoch086aeea2011-05-13 15:57:08 +01001545 print('c[ontinue] - continue executing after a breakpoint');
1546 print('s[tep] [<N>] - step into the next N callees (default N is 1)');
1547 print('s[tep]i [<N>] - step into the next N callees (default N is 1)');
1548 print('n[ext] [<N>] - step over the next N callees (default N is 1)');
1549 print('fin[ish] [<N>] - step out of N frames (default N is 1)');
1550 print('');
1551 print('p[rint] <expression> - prints the result of the specified expression');
1552 print('dir <expression> - prints the object structure of the result');
1553 print('set <var> = <expression> - executes the specified statement');
1554 print('');
1555 print('l[ist] - list the source code around for the current pc');
1556 print('l[ist] [- | <start>,<end>] - list the specified range of source code');
Steve Blocka7e24c12009-10-30 11:49:00 +00001557 print('source [from line [num lines]]');
Ben Murdoch086aeea2011-05-13 15:57:08 +01001558 print('scr[ipts] [native|extensions|all]');
1559 print('scr[ipts] [<filter text>] - list scripts with the specified text in its description');
1560 print('');
1561 print('gc - runs the garbage collector');
1562 print('');
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001563
1564 if (lol_is_enabled) {
1565 print('liveobjectlist|lol <command> - live object list tracking.');
1566 print(' where <command> can be:');
1567 print(' c[apture] - captures a LOL list.');
1568 print(' clear|del[ete] <id>|all - clears LOL of id <id>.');
1569 print(' If \'all\' is unspecified instead, will clear all.');
1570 print(' diff <id1> <id2> [<dump options>]');
1571 print(' - prints the diff between LOLs id1 and id2.');
1572 print(' - also see <dump options> below.');
1573 print(' getid <address> - gets the obj id for the specified address if available.');
1574 print(' The address must be in hex form prefixed with 0x.');
1575 print(' inf[o] [<N>] - lists summary info of all LOL lists.');
1576 print(' If N is specified, will print N items at a time.');
1577 print(' [l[ist]] <id> [<dump options>]');
1578 print(' - prints the listing of objects in LOL id.');
1579 print(' - also see <dump options> below.');
1580 print(' reset - clears all LOL lists.');
1581 print(' ret[ainers] <id> [<dump options>]');
1582 print(' - prints the list of retainers of obj id.');
1583 print(' - also see <dump options> below.');
1584 print(' path <id1> <id2> - prints the retaining path from obj id1 to id2.');
1585 print(' If only one id is specified, will print the path from');
1586 print(' roots to the specified object if available.');
1587 print(' print <id> - prints the obj for the specified obj id if available.');
1588 print('');
1589 print(' <dump options> includes:');
1590 print(' [v[erbose]] - do verbose dump.');
1591 print(' [<N>] - dump N items at a time. Implies verbose dump.');
1592 print(' If unspecified, N will default to '+
1593 kDefaultLolLinesToPrintAtATime+'. Max N is '+
1594 kMaxLolLinesToPrintAtATime+'.');
1595 print(' [i[ndex] <i>] - start dump from index i. Implies verbose dump.');
1596 print(' [t[ype] <type>] - filter by type.');
1597 print(' [sp[ace] <space name>] - filter by heap space where <space name> is one of');
1598 print(' { cell, code, lo, map, new, old-data, old-pointer }.');
1599 print('');
1600 print(' If the verbose option, or an option that implies a verbose dump');
1601 print(' is specified, then a verbose dump will requested. Else, a summary dump');
1602 print(' will be requested.');
1603 print('');
1604 }
1605
Steve Blocka7e24c12009-10-30 11:49:00 +00001606 print('trace compile');
Ben Murdoch086aeea2011-05-13 15:57:08 +01001607 // hidden command: trace debug json - toggles tracing of debug json packets
1608 print('');
1609 print('disconnect|exit|quit - disconnects and quits the debugger');
1610 print('help - prints this help information');
Steve Blocka7e24c12009-10-30 11:49:00 +00001611}
1612
1613
1614function formatHandleReference_(value) {
1615 if (value.handle() >= 0) {
1616 return '#' + value.handle() + '#';
1617 } else {
1618 return '#Transient#';
1619 }
1620}
1621
1622
1623function formatObject_(value, include_properties) {
1624 var result = '';
1625 result += formatHandleReference_(value);
1626 result += ', type: object'
1627 result += ', constructor ';
1628 var ctor = value.constructorFunctionValue();
1629 result += formatHandleReference_(ctor);
1630 result += ', __proto__ ';
1631 var proto = value.protoObjectValue();
1632 result += formatHandleReference_(proto);
1633 result += ', ';
1634 result += value.propertyCount();
1635 result += ' properties.';
1636 if (include_properties) {
1637 result += '\n';
1638 for (var i = 0; i < value.propertyCount(); i++) {
1639 result += ' ';
1640 result += value.propertyName(i);
1641 result += ': ';
1642 var property_value = value.propertyValue(i);
1643 if (property_value instanceof ProtocolReference) {
1644 result += '<no type>';
1645 } else {
1646 if (property_value && property_value.type()) {
1647 result += property_value.type();
1648 } else {
1649 result += '<no type>';
1650 }
1651 }
1652 result += ' ';
1653 result += formatHandleReference_(property_value);
1654 result += '\n';
1655 }
1656 }
1657 return result;
1658}
1659
1660
1661function formatScope_(scope) {
1662 var result = '';
1663 var index = scope.index;
1664 result += '#' + (index <= 9 ? '0' : '') + index;
1665 result += ' ';
1666 switch (scope.type) {
1667 case Debug.ScopeType.Global:
1668 result += 'Global, ';
1669 result += '#' + scope.object.ref + '#';
1670 break;
1671 case Debug.ScopeType.Local:
1672 result += 'Local';
1673 break;
1674 case Debug.ScopeType.With:
1675 result += 'With, ';
1676 result += '#' + scope.object.ref + '#';
1677 break;
1678 case Debug.ScopeType.Catch:
1679 result += 'Catch, ';
1680 result += '#' + scope.object.ref + '#';
1681 break;
1682 case Debug.ScopeType.Closure:
1683 result += 'Closure';
1684 break;
1685 default:
1686 result += 'UNKNOWN';
1687 }
1688 return result;
1689}
1690
1691
Ben Murdoch086aeea2011-05-13 15:57:08 +01001692function refObjectToString_(protocolPackage, handle) {
1693 var value = protocolPackage.lookup(handle);
1694 var result = '';
1695 if (value.isString()) {
1696 result = '"' + value.value() + '"';
1697 } else if (value.isPrimitive()) {
1698 result = value.valueString();
1699 } else if (value.isObject()) {
1700 result += formatObject_(value, true);
1701 }
1702 return result;
1703}
1704
1705
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001706function decodeLolCaptureResponse(body) {
1707 var result;
1708 result = 'Captured live object list '+ body.id +
1709 ': count '+ body.count + ' size ' + body.size;
1710 return result;
1711}
1712
1713
1714function decodeLolDeleteResponse(body) {
1715 var result;
1716 result = 'Deleted live object list '+ body.id;
1717 return result;
1718}
1719
1720
1721function digitsIn(value) {
1722 var digits = 0;
1723 if (value === 0) value = 1;
1724 while (value >= 1) {
1725 digits++;
1726 value /= 10;
1727 }
1728 return digits;
1729}
1730
1731
1732function padding(value, max_digits) {
1733 var padding_digits = max_digits - digitsIn(value);
1734 var padding = '';
1735 while (padding_digits > 0) {
1736 padding += ' ';
1737 padding_digits--;
1738 }
1739 return padding;
1740}
1741
1742
1743function decodeLolInfoResponse(body) {
1744 var result;
1745 var lists = body.lists;
1746 var length = lists.length;
1747 var first_index = body.first_index + 1;
1748 var has_more = ((first_index + length) <= body.count);
1749 result = 'captured live object lists';
1750 if (has_more || (first_index != 1)) {
1751 result += ' ['+ length +' of '+ body.count +
1752 ': starting from '+ first_index +']';
1753 }
1754 result += ':\n';
1755 var max_digits = digitsIn(body.count);
1756 var last_count = 0;
1757 var last_size = 0;
1758 for (var i = 0; i < length; i++) {
1759 var entry = lists[i];
1760 var count = entry.count;
1761 var size = entry.size;
1762 var index = first_index + i;
1763 result += ' [' + padding(index, max_digits) + index + '] id '+ entry.id +
1764 ': count '+ count;
1765 if (last_count > 0) {
1766 result += '(+' + (count - last_count) + ')';
1767 }
1768 result += ' size '+ size;
1769 if (last_size > 0) {
1770 result += '(+' + (size - last_size) + ')';
1771 }
1772 result += '\n';
1773 last_count = count;
1774 last_size = size;
1775 }
1776 result += ' total: '+length+' lists\n';
1777 if (has_more) {
1778 result += ' -- press <enter> for more --\n';
1779 } else {
1780 repeat_cmd_line = '';
1781 }
1782 if (length === 0) result += ' none\n';
1783
1784 return result;
1785}
1786
1787
1788function decodeLolListResponse(body, title) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001789
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001790 var result;
1791 var total_count = body.count;
1792 var total_size = body.size;
1793 var length;
1794 var max_digits;
1795 var i;
1796 var entry;
1797 var index;
1798
1799 var max_count_digits = digitsIn(total_count);
1800 var max_size_digits;
1801
1802 var summary = body.summary;
1803 if (summary) {
1804
1805 var roots_count = 0;
1806 var found_root = body.found_root || 0;
1807 var found_weak_root = body.found_weak_root || 0;
1808
1809 // Print the summary result:
1810 result = 'summary of objects:\n';
1811 length = summary.length;
1812 if (found_root !== 0) {
1813 roots_count++;
1814 }
1815 if (found_weak_root !== 0) {
1816 roots_count++;
1817 }
1818 max_digits = digitsIn(length + roots_count);
1819 max_size_digits = digitsIn(total_size);
1820
1821 index = 1;
1822 if (found_root !== 0) {
1823 result += ' [' + padding(index, max_digits) + index + '] ' +
1824 ' count '+ 1 + padding(0, max_count_digits) +
1825 ' '+ padding(0, max_size_digits+1) +
1826 ' : <root>\n';
1827 index++;
1828 }
1829 if (found_weak_root !== 0) {
1830 result += ' [' + padding(index, max_digits) + index + '] ' +
1831 ' count '+ 1 + padding(0, max_count_digits) +
1832 ' '+ padding(0, max_size_digits+1) +
1833 ' : <weak root>\n';
1834 index++;
1835 }
1836
1837 for (i = 0; i < length; i++) {
1838 entry = summary[i];
1839 var count = entry.count;
1840 var size = entry.size;
1841 result += ' [' + padding(index, max_digits) + index + '] ' +
1842 ' count '+ count + padding(count, max_count_digits) +
1843 ' size '+ size + padding(size, max_size_digits) +
1844 ' : <' + entry.desc + '>\n';
1845 index++;
1846 }
1847 result += '\n total count: '+(total_count+roots_count)+'\n';
1848 if (body.size) {
1849 result += ' total size: '+body.size+'\n';
1850 }
1851
1852 } else {
1853 // Print the full dump result:
1854 var first_index = body.first_index + 1;
1855 var elements = body.elements;
1856 length = elements.length;
1857 var has_more = ((first_index + length) <= total_count);
1858 result = title;
1859 if (has_more || (first_index != 1)) {
1860 result += ' ['+ length +' of '+ total_count +
1861 ': starting from '+ first_index +']';
1862 }
1863 result += ':\n';
1864 if (length === 0) result += ' none\n';
1865 max_digits = digitsIn(length);
1866
1867 var max_id = 0;
1868 var max_size = 0;
1869 for (i = 0; i < length; i++) {
1870 entry = elements[i];
1871 if (entry.id > max_id) max_id = entry.id;
1872 if (entry.size > max_size) max_size = entry.size;
1873 }
1874 var max_id_digits = digitsIn(max_id);
1875 max_size_digits = digitsIn(max_size);
1876
1877 for (i = 0; i < length; i++) {
1878 entry = elements[i];
1879 index = first_index + i;
1880 result += ' ['+ padding(index, max_digits) + index +']';
1881 if (entry.id !== 0) {
1882 result += ' @' + entry.id + padding(entry.id, max_id_digits) +
1883 ': size ' + entry.size + ', ' +
1884 padding(entry.size, max_size_digits) + entry.desc + '\n';
1885 } else {
1886 // Must be a root or weak root:
1887 result += ' ' + entry.desc + '\n';
1888 }
1889 }
1890 if (has_more) {
1891 result += ' -- press <enter> for more --\n';
1892 } else {
1893 repeat_cmd_line = '';
1894 }
1895 if (length === 0) result += ' none\n';
1896 }
1897
1898 return result;
1899}
1900
1901
1902function decodeLolDiffResponse(body) {
1903 var title = 'objects';
1904 return decodeLolListResponse(body, title);
1905}
1906
1907
1908function decodeLolRetainersResponse(body) {
1909 var title = 'retainers for @' + body.id;
1910 return decodeLolListResponse(body, title);
1911}
1912
1913
1914function decodeLolPathResponse(body) {
1915 return body.path;
1916}
1917
1918
1919function decodeLolResetResponse(body) {
1920 return 'Reset all live object lists.';
1921}
1922
1923
1924function decodeLolGetIdResponse(body) {
1925 if (body.id == 0) {
1926 return 'Address is invalid, or object has been moved or collected';
1927 }
1928 return 'obj id is @' + body.id;
1929}
1930
1931
1932function decodeLolPrintResponse(body) {
1933 return body.dump;
1934}
1935
1936
Ben Murdoch086aeea2011-05-13 15:57:08 +01001937// Rounds number 'num' to 'length' decimal places.
1938function roundNumber(num, length) {
1939 var factor = Math.pow(10, length);
1940 return Math.round(num * factor) / factor;
1941}
1942
1943
Steve Blocka7e24c12009-10-30 11:49:00 +00001944// Convert a JSON response to text for display in a text based debugger.
1945function DebugResponseDetails(response) {
1946 details = {text:'', running:false}
1947
1948 try {
1949 if (!response.success()) {
1950 details.text = response.message();
1951 return details;
1952 }
1953
1954 // Get the running state.
1955 details.running = response.running();
1956
1957 var body = response.body();
1958 var result = '';
1959 switch (response.command()) {
Steve Block6ded16b2010-05-10 14:33:55 +01001960 case 'suspend':
1961 details.text = 'stopped';
1962 break;
Ben Murdochf87a2032010-10-22 12:50:53 +01001963
Steve Blocka7e24c12009-10-30 11:49:00 +00001964 case 'setbreakpoint':
1965 result = 'set breakpoint #';
1966 result += body.breakpoint;
1967 details.text = result;
1968 break;
Steve Block6ded16b2010-05-10 14:33:55 +01001969
Steve Blocka7e24c12009-10-30 11:49:00 +00001970 case 'clearbreakpoint':
1971 result = 'cleared breakpoint #';
1972 result += body.breakpoint;
1973 details.text = result;
1974 break;
Ben Murdochf87a2032010-10-22 12:50:53 +01001975
Ben Murdoch086aeea2011-05-13 15:57:08 +01001976 case 'changebreakpoint':
1977 result = 'successfully changed breakpoint';
1978 details.text = result;
1979 break;
1980
Kristian Monsen25f61362010-05-21 11:50:48 +01001981 case 'listbreakpoints':
1982 result = 'breakpoints: (' + body.breakpoints.length + ')';
1983 for (var i = 0; i < body.breakpoints.length; i++) {
1984 var breakpoint = body.breakpoints[i];
1985 result += '\n id=' + breakpoint.number;
1986 result += ' type=' + breakpoint.type;
1987 if (breakpoint.script_id) {
1988 result += ' script_id=' + breakpoint.script_id;
1989 }
1990 if (breakpoint.script_name) {
1991 result += ' script_name=' + breakpoint.script_name;
1992 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001993 if (breakpoint.script_regexp) {
1994 result += ' script_regexp=' + breakpoint.script_regexp;
1995 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01001996 result += ' line=' + (breakpoint.line + 1);
Kristian Monsen25f61362010-05-21 11:50:48 +01001997 if (breakpoint.column != null) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01001998 result += ' column=' + (breakpoint.column + 1);
Kristian Monsen25f61362010-05-21 11:50:48 +01001999 }
2000 if (breakpoint.groupId) {
2001 result += ' groupId=' + breakpoint.groupId;
2002 }
2003 if (breakpoint.ignoreCount) {
2004 result += ' ignoreCount=' + breakpoint.ignoreCount;
2005 }
2006 if (breakpoint.active === false) {
2007 result += ' inactive';
2008 }
2009 if (breakpoint.condition) {
2010 result += ' condition=' + breakpoint.condition;
2011 }
2012 result += ' hit_count=' + breakpoint.hit_count;
2013 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01002014 if (body.breakpoints.length === 0) {
2015 result = "No user defined breakpoints\n";
2016 } else {
2017 result += '\n';
2018 }
2019 if (body.breakOnExceptions) {
2020 result += '* breaking on ALL exceptions is enabled\n';
2021 } else if (body.breakOnUncaughtExceptions) {
2022 result += '* breaking on UNCAUGHT exceptions is enabled\n';
2023 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002024 result += '* all exception breakpoints are disabled\n';
Ben Murdoch086aeea2011-05-13 15:57:08 +01002025 }
2026 details.text = result;
2027 break;
2028
2029 case 'setexceptionbreak':
2030 result = 'Break on ' + body.type + ' exceptions: ';
2031 result += body.enabled ? 'enabled' : 'disabled';
Kristian Monsen25f61362010-05-21 11:50:48 +01002032 details.text = result;
2033 break;
Steve Block6ded16b2010-05-10 14:33:55 +01002034
Steve Blocka7e24c12009-10-30 11:49:00 +00002035 case 'backtrace':
2036 if (body.totalFrames == 0) {
2037 result = '(empty stack)';
2038 } else {
2039 var result = 'Frames #' + body.fromFrame + ' to #' +
2040 (body.toFrame - 1) + ' of ' + body.totalFrames + '\n';
2041 for (i = 0; i < body.frames.length; i++) {
2042 if (i != 0) result += '\n';
2043 result += body.frames[i].text;
2044 }
2045 }
2046 details.text = result;
2047 break;
Steve Block6ded16b2010-05-10 14:33:55 +01002048
Steve Blocka7e24c12009-10-30 11:49:00 +00002049 case 'frame':
Ben Murdoch086aeea2011-05-13 15:57:08 +01002050 if (last_cmd === 'info locals') {
2051 var locals = body.locals;
2052 if (locals.length === 0) {
2053 result = 'No locals';
2054 } else {
2055 for (var i = 0; i < locals.length; i++) {
2056 var local = locals[i];
2057 result += local.name + ' = ';
2058 result += refObjectToString_(response, local.value.ref);
2059 result += '\n';
2060 }
2061 }
2062 } else if (last_cmd === 'info args') {
2063 var args = body.arguments;
2064 if (args.length === 0) {
2065 result = 'No arguments';
2066 } else {
2067 for (var i = 0; i < args.length; i++) {
2068 var arg = args[i];
2069 result += arg.name + ' = ';
2070 result += refObjectToString_(response, arg.value.ref);
2071 result += '\n';
2072 }
2073 }
2074 } else {
2075 result = SourceUnderline(body.sourceLineText,
2076 body.column);
2077 Debug.State.currentSourceLine = body.line;
2078 Debug.State.currentFrame = body.index;
2079 Debug.State.displaySourceStartLine = -1;
2080 Debug.State.displaySourceEndLine = -1;
2081 }
2082 details.text = result;
Steve Blocka7e24c12009-10-30 11:49:00 +00002083 break;
Steve Block6ded16b2010-05-10 14:33:55 +01002084
Steve Blocka7e24c12009-10-30 11:49:00 +00002085 case 'scopes':
2086 if (body.totalScopes == 0) {
2087 result = '(no scopes)';
2088 } else {
2089 result = 'Scopes #' + body.fromScope + ' to #' +
2090 (body.toScope - 1) + ' of ' + body.totalScopes + '\n';
2091 for (i = 0; i < body.scopes.length; i++) {
2092 if (i != 0) {
2093 result += '\n';
2094 }
2095 result += formatScope_(body.scopes[i]);
2096 }
2097 }
2098 details.text = result;
2099 break;
2100
2101 case 'scope':
2102 result += formatScope_(body);
2103 result += '\n';
2104 var scope_object_value = response.lookup(body.object.ref);
2105 result += formatObject_(scope_object_value, true);
2106 details.text = result;
2107 break;
Steve Block6ded16b2010-05-10 14:33:55 +01002108
Steve Blocka7e24c12009-10-30 11:49:00 +00002109 case 'evaluate':
2110 case 'lookup':
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002111 case 'getobj':
Steve Blocka7e24c12009-10-30 11:49:00 +00002112 if (last_cmd == 'p' || last_cmd == 'print') {
2113 result = body.text;
2114 } else {
2115 var value;
2116 if (lookup_handle) {
2117 value = response.bodyValue(lookup_handle);
2118 } else {
2119 value = response.bodyValue();
2120 }
2121 if (value.isObject()) {
2122 result += formatObject_(value, true);
2123 } else {
2124 result += 'type: ';
2125 result += value.type();
2126 if (!value.isUndefined() && !value.isNull()) {
2127 result += ', ';
2128 if (value.isString()) {
2129 result += '"';
2130 }
2131 result += value.value();
2132 if (value.isString()) {
2133 result += '"';
2134 }
2135 }
2136 result += '\n';
2137 }
2138 }
2139 details.text = result;
2140 break;
2141
2142 case 'references':
2143 var count = body.length;
2144 result += 'found ' + count + ' objects';
2145 result += '\n';
2146 for (var i = 0; i < count; i++) {
2147 var value = response.bodyValue(i);
2148 result += formatObject_(value, false);
2149 result += '\n';
2150 }
2151 details.text = result;
2152 break;
Steve Block6ded16b2010-05-10 14:33:55 +01002153
Steve Blocka7e24c12009-10-30 11:49:00 +00002154 case 'source':
2155 // Get the source from the response.
2156 var source = body.source;
2157 var from_line = body.fromLine + 1;
2158 var lines = source.split('\n');
2159 var maxdigits = 1 + Math.floor(log10(from_line + lines.length));
2160 if (maxdigits < 3) {
2161 maxdigits = 3;
2162 }
2163 var result = '';
2164 for (var num = 0; num < lines.length; num++) {
2165 // Check if there's an extra newline at the end.
2166 if (num == (lines.length - 1) && lines[num].length == 0) {
2167 break;
2168 }
2169
2170 var current_line = from_line + num;
2171 spacer = maxdigits - (1 + Math.floor(log10(current_line)));
2172 if (current_line == Debug.State.currentSourceLine + 1) {
2173 for (var i = 0; i < maxdigits; i++) {
2174 result += '>';
2175 }
2176 result += ' ';
2177 } else {
2178 for (var i = 0; i < spacer; i++) {
2179 result += ' ';
2180 }
2181 result += current_line + ': ';
2182 }
2183 result += lines[num];
2184 result += '\n';
2185 }
2186 details.text = result;
2187 break;
Steve Block6ded16b2010-05-10 14:33:55 +01002188
Steve Blocka7e24c12009-10-30 11:49:00 +00002189 case 'scripts':
2190 var result = '';
2191 for (i = 0; i < body.length; i++) {
2192 if (i != 0) result += '\n';
2193 if (body[i].id) {
2194 result += body[i].id;
2195 } else {
2196 result += '[no id]';
2197 }
2198 result += ', ';
2199 if (body[i].name) {
2200 result += body[i].name;
2201 } else {
Ben Murdoch086aeea2011-05-13 15:57:08 +01002202 if (body[i].compilationType == Debug.ScriptCompilationType.Eval
2203 && body[i].evalFromScript
2204 ) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002205 result += 'eval from ';
2206 var script_value = response.lookup(body[i].evalFromScript.ref);
2207 result += ' ' + script_value.field('name');
2208 result += ':' + (body[i].evalFromLocation.line + 1);
2209 result += ':' + body[i].evalFromLocation.column;
2210 } else if (body[i].compilationType ==
2211 Debug.ScriptCompilationType.JSON) {
2212 result += 'JSON ';
2213 } else { // body[i].compilation == Debug.ScriptCompilationType.Host
2214 result += '[unnamed] ';
2215 }
2216 }
2217 result += ' (lines: ';
2218 result += body[i].lineCount;
2219 result += ', length: ';
2220 result += body[i].sourceLength;
2221 if (body[i].type == Debug.ScriptType.Native) {
2222 result += ', native';
2223 } else if (body[i].type == Debug.ScriptType.Extension) {
2224 result += ', extension';
2225 }
2226 result += '), [';
2227 var sourceStart = body[i].sourceStart;
2228 if (sourceStart.length > 40) {
2229 sourceStart = sourceStart.substring(0, 37) + '...';
2230 }
2231 result += sourceStart;
2232 result += ']';
2233 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01002234 if (body.length == 0) {
2235 result = "no matching scripts found";
2236 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002237 details.text = result;
2238 break;
2239
2240 case 'threads':
2241 var result = 'Active V8 threads: ' + body.totalThreads + '\n';
2242 body.threads.sort(function(a, b) { return a.id - b.id; });
2243 for (i = 0; i < body.threads.length; i++) {
2244 result += body.threads[i].current ? '*' : ' ';
2245 result += ' ';
2246 result += body.threads[i].id;
2247 result += '\n';
2248 }
2249 details.text = result;
2250 break;
2251
2252 case 'continue':
2253 details.text = "(running)";
2254 break;
Steve Block6ded16b2010-05-10 14:33:55 +01002255
Ben Murdoch086aeea2011-05-13 15:57:08 +01002256 case 'v8flags':
2257 details.text = "flags set";
2258 break;
2259
2260 case 'gc':
2261 details.text = "GC " + body.before + " => " + body.after;
2262 if (body.after > (1024*1024)) {
2263 details.text +=
2264 " (" + roundNumber(body.before/(1024*1024), 1) + "M => " +
2265 roundNumber(body.after/(1024*1024), 1) + "M)";
2266 } else if (body.after > 1024) {
2267 details.text +=
2268 " (" + roundNumber(body.before/1024, 1) + "K => " +
2269 roundNumber(body.after/1024, 1) + "K)";
2270 }
2271 break;
2272
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002273 case 'lol-capture':
2274 details.text = decodeLolCaptureResponse(body);
2275 break;
2276 case 'lol-delete':
2277 details.text = decodeLolDeleteResponse(body);
2278 break;
2279 case 'lol-diff':
2280 details.text = decodeLolDiffResponse(body);
2281 break;
2282 case 'lol-getid':
2283 details.text = decodeLolGetIdResponse(body);
2284 break;
2285 case 'lol-info':
2286 details.text = decodeLolInfoResponse(body);
2287 break;
2288 case 'lol-print':
2289 details.text = decodeLolPrintResponse(body);
2290 break;
2291 case 'lol-reset':
2292 details.text = decodeLolResetResponse(body);
2293 break;
2294 case 'lol-retainers':
2295 details.text = decodeLolRetainersResponse(body);
2296 break;
2297 case 'lol-path':
2298 details.text = decodeLolPathResponse(body);
2299 break;
2300
Steve Blocka7e24c12009-10-30 11:49:00 +00002301 default:
2302 details.text =
Kristian Monsen25f61362010-05-21 11:50:48 +01002303 'Response for unknown command \'' + response.command() + '\'' +
2304 ' (' + response.raw_json() + ')';
Steve Blocka7e24c12009-10-30 11:49:00 +00002305 }
2306 } catch (e) {
2307 details.text = 'Error: "' + e + '" formatting response';
2308 }
Steve Block6ded16b2010-05-10 14:33:55 +01002309
Steve Blocka7e24c12009-10-30 11:49:00 +00002310 return details;
2311};
2312
2313
2314/**
2315 * Protocol packages send from the debugger.
2316 * @param {string} json - raw protocol packet as JSON string.
2317 * @constructor
2318 */
2319function ProtocolPackage(json) {
Kristian Monsen25f61362010-05-21 11:50:48 +01002320 this.raw_json_ = json;
Steve Blocka7e24c12009-10-30 11:49:00 +00002321 this.packet_ = JSON.parse(json);
2322 this.refs_ = [];
2323 if (this.packet_.refs) {
2324 for (var i = 0; i < this.packet_.refs.length; i++) {
2325 this.refs_[this.packet_.refs[i].handle] = this.packet_.refs[i];
2326 }
2327 }
2328}
2329
2330
2331/**
2332 * Get the packet type.
2333 * @return {String} the packet type
2334 */
2335ProtocolPackage.prototype.type = function() {
2336 return this.packet_.type;
2337}
2338
2339
2340/**
2341 * Get the packet event.
2342 * @return {Object} the packet event
2343 */
2344ProtocolPackage.prototype.event = function() {
2345 return this.packet_.event;
2346}
2347
2348
2349/**
2350 * Get the packet request sequence.
2351 * @return {number} the packet request sequence
2352 */
2353ProtocolPackage.prototype.requestSeq = function() {
2354 return this.packet_.request_seq;
2355}
2356
2357
2358/**
2359 * Get the packet request sequence.
2360 * @return {number} the packet request sequence
2361 */
2362ProtocolPackage.prototype.running = function() {
2363 return this.packet_.running ? true : false;
2364}
2365
2366
2367ProtocolPackage.prototype.success = function() {
2368 return this.packet_.success ? true : false;
2369}
2370
2371
2372ProtocolPackage.prototype.message = function() {
2373 return this.packet_.message;
2374}
2375
2376
2377ProtocolPackage.prototype.command = function() {
2378 return this.packet_.command;
2379}
2380
2381
2382ProtocolPackage.prototype.body = function() {
2383 return this.packet_.body;
2384}
2385
2386
2387ProtocolPackage.prototype.bodyValue = function(index) {
2388 if (index != null) {
2389 return new ProtocolValue(this.packet_.body[index], this);
2390 } else {
2391 return new ProtocolValue(this.packet_.body, this);
2392 }
2393}
2394
2395
2396ProtocolPackage.prototype.body = function() {
2397 return this.packet_.body;
2398}
2399
2400
2401ProtocolPackage.prototype.lookup = function(handle) {
2402 var value = this.refs_[handle];
2403 if (value) {
2404 return new ProtocolValue(value, this);
2405 } else {
2406 return new ProtocolReference(handle);
2407 }
2408}
2409
2410
Kristian Monsen25f61362010-05-21 11:50:48 +01002411ProtocolPackage.prototype.raw_json = function() {
2412 return this.raw_json_;
2413}
2414
2415
Steve Blocka7e24c12009-10-30 11:49:00 +00002416function ProtocolValue(value, packet) {
2417 this.value_ = value;
2418 this.packet_ = packet;
2419}
2420
2421
2422/**
2423 * Get the value type.
2424 * @return {String} the value type
2425 */
2426ProtocolValue.prototype.type = function() {
2427 return this.value_.type;
2428}
2429
2430
2431/**
Steve Block6ded16b2010-05-10 14:33:55 +01002432 * Get a metadata field from a protocol value.
Steve Blocka7e24c12009-10-30 11:49:00 +00002433 * @return {Object} the metadata field value
2434 */
2435ProtocolValue.prototype.field = function(name) {
2436 return this.value_[name];
2437}
2438
2439
2440/**
2441 * Check is the value is a primitive value.
2442 * @return {boolean} true if the value is primitive
2443 */
2444ProtocolValue.prototype.isPrimitive = function() {
2445 return this.isUndefined() || this.isNull() || this.isBoolean() ||
2446 this.isNumber() || this.isString();
2447}
2448
2449
2450/**
2451 * Get the object handle.
2452 * @return {number} the value handle
2453 */
2454ProtocolValue.prototype.handle = function() {
2455 return this.value_.handle;
2456}
2457
2458
2459/**
2460 * Check is the value is undefined.
2461 * @return {boolean} true if the value is undefined
2462 */
2463ProtocolValue.prototype.isUndefined = function() {
2464 return this.value_.type == 'undefined';
2465}
2466
2467
2468/**
2469 * Check is the value is null.
2470 * @return {boolean} true if the value is null
2471 */
2472ProtocolValue.prototype.isNull = function() {
2473 return this.value_.type == 'null';
2474}
2475
2476
2477/**
2478 * Check is the value is a boolean.
2479 * @return {boolean} true if the value is a boolean
2480 */
2481ProtocolValue.prototype.isBoolean = function() {
2482 return this.value_.type == 'boolean';
2483}
2484
2485
2486/**
2487 * Check is the value is a number.
2488 * @return {boolean} true if the value is a number
2489 */
2490ProtocolValue.prototype.isNumber = function() {
2491 return this.value_.type == 'number';
2492}
2493
2494
2495/**
2496 * Check is the value is a string.
2497 * @return {boolean} true if the value is a string
2498 */
2499ProtocolValue.prototype.isString = function() {
2500 return this.value_.type == 'string';
2501}
2502
2503
2504/**
2505 * Check is the value is an object.
2506 * @return {boolean} true if the value is an object
2507 */
2508ProtocolValue.prototype.isObject = function() {
2509 return this.value_.type == 'object' || this.value_.type == 'function' ||
2510 this.value_.type == 'error' || this.value_.type == 'regexp';
2511}
2512
2513
2514/**
2515 * Get the constructor function
2516 * @return {ProtocolValue} constructor function
2517 */
2518ProtocolValue.prototype.constructorFunctionValue = function() {
2519 var ctor = this.value_.constructorFunction;
2520 return this.packet_.lookup(ctor.ref);
2521}
2522
2523
2524/**
2525 * Get the __proto__ value
2526 * @return {ProtocolValue} __proto__ value
2527 */
2528ProtocolValue.prototype.protoObjectValue = function() {
2529 var proto = this.value_.protoObject;
2530 return this.packet_.lookup(proto.ref);
2531}
2532
2533
2534/**
2535 * Get the number og properties.
2536 * @return {number} the number of properties
2537 */
2538ProtocolValue.prototype.propertyCount = function() {
2539 return this.value_.properties ? this.value_.properties.length : 0;
2540}
2541
2542
2543/**
2544 * Get the specified property name.
2545 * @return {string} property name
2546 */
2547ProtocolValue.prototype.propertyName = function(index) {
2548 var property = this.value_.properties[index];
2549 return property.name;
2550}
2551
2552
2553/**
2554 * Return index for the property name.
2555 * @param name The property name to look for
2556 * @return {number} index for the property name
2557 */
2558ProtocolValue.prototype.propertyIndex = function(name) {
2559 for (var i = 0; i < this.propertyCount(); i++) {
2560 if (this.value_.properties[i].name == name) {
2561 return i;
2562 }
2563 }
2564 return null;
2565}
2566
2567
2568/**
2569 * Get the specified property value.
2570 * @return {ProtocolValue} property value
2571 */
2572ProtocolValue.prototype.propertyValue = function(index) {
2573 var property = this.value_.properties[index];
2574 return this.packet_.lookup(property.ref);
2575}
2576
2577
2578/**
2579 * Check is the value is a string.
2580 * @return {boolean} true if the value is a string
2581 */
2582ProtocolValue.prototype.value = function() {
2583 return this.value_.value;
2584}
2585
2586
Ben Murdoch086aeea2011-05-13 15:57:08 +01002587ProtocolValue.prototype.valueString = function() {
2588 return this.value_.text;
2589}
2590
2591
Steve Blocka7e24c12009-10-30 11:49:00 +00002592function ProtocolReference(handle) {
2593 this.handle_ = handle;
2594}
2595
2596
2597ProtocolReference.prototype.handle = function() {
2598 return this.handle_;
2599}
2600
2601
2602function MakeJSONPair_(name, value) {
2603 return '"' + name + '":' + value;
2604}
2605
2606
2607function ArrayToJSONObject_(content) {
2608 return '{' + content.join(',') + '}';
2609}
2610
2611
2612function ArrayToJSONArray_(content) {
2613 return '[' + content.join(',') + ']';
2614}
2615
2616
2617function BooleanToJSON_(value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002618 return String(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002619}
2620
2621
2622function NumberToJSON_(value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002623 return String(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002624}
2625
2626
2627// Mapping of some control characters to avoid the \uXXXX syntax for most
2628// commonly used control cahracters.
2629const ctrlCharMap_ = {
2630 '\b': '\\b',
2631 '\t': '\\t',
2632 '\n': '\\n',
2633 '\f': '\\f',
2634 '\r': '\\r',
2635 '"' : '\\"',
2636 '\\': '\\\\'
2637};
2638
2639
2640// Regular expression testing for ", \ and control characters (0x00 - 0x1F).
2641const ctrlCharTest_ = new RegExp('["\\\\\x00-\x1F]');
2642
2643
2644// Regular expression matching ", \ and control characters (0x00 - 0x1F)
2645// globally.
2646const ctrlCharMatch_ = new RegExp('["\\\\\x00-\x1F]', 'g');
2647
2648
2649/**
2650 * Convert a String to its JSON representation (see http://www.json.org/). To
2651 * avoid depending on the String object this method calls the functions in
2652 * string.js directly and not through the value.
2653 * @param {String} value The String value to format as JSON
2654 * @return {string} JSON formatted String value
2655 */
2656function StringToJSON_(value) {
2657 // Check for" , \ and control characters (0x00 - 0x1F). No need to call
2658 // RegExpTest as ctrlchar is constructed using RegExp.
2659 if (ctrlCharTest_.test(value)) {
2660 // Replace ", \ and control characters (0x00 - 0x1F).
2661 return '"' +
2662 value.replace(ctrlCharMatch_, function (char) {
2663 // Use charmap if possible.
2664 var mapped = ctrlCharMap_[char];
2665 if (mapped) return mapped;
2666 mapped = char.charCodeAt();
2667 // Convert control character to unicode escape sequence.
2668 return '\\u00' +
2669 '0' + // TODO %NumberToRadixString(Math.floor(mapped / 16), 16) +
2670 '0' // TODO %NumberToRadixString(mapped % 16, 16);
2671 })
2672 + '"';
2673 }
2674
2675 // Simple string with no special characters.
2676 return '"' + value + '"';
2677}
2678
2679
2680/**
2681 * Convert a Date to ISO 8601 format. To avoid depending on the Date object
2682 * this method calls the functions in date.js directly and not through the
2683 * value.
2684 * @param {Date} value The Date value to format as JSON
2685 * @return {string} JSON formatted Date value
2686 */
2687function DateToISO8601_(value) {
2688 function f(n) {
2689 return n < 10 ? '0' + n : n;
2690 }
2691 function g(n) {
2692 return n < 10 ? '00' + n : n < 100 ? '0' + n : n;
2693 }
2694 return builtins.GetUTCFullYearFrom(value) + '-' +
2695 f(builtins.GetUTCMonthFrom(value) + 1) + '-' +
2696 f(builtins.GetUTCDateFrom(value)) + 'T' +
2697 f(builtins.GetUTCHoursFrom(value)) + ':' +
2698 f(builtins.GetUTCMinutesFrom(value)) + ':' +
2699 f(builtins.GetUTCSecondsFrom(value)) + '.' +
2700 g(builtins.GetUTCMillisecondsFrom(value)) + 'Z';
2701}
2702
2703
2704/**
2705 * Convert a Date to ISO 8601 format. To avoid depending on the Date object
2706 * this method calls the functions in date.js directly and not through the
2707 * value.
2708 * @param {Date} value The Date value to format as JSON
2709 * @return {string} JSON formatted Date value
2710 */
2711function DateToJSON_(value) {
2712 return '"' + DateToISO8601_(value) + '"';
2713}
2714
2715
2716/**
2717 * Convert an Object to its JSON representation (see http://www.json.org/).
2718 * This implementation simply runs through all string property names and adds
2719 * each property to the JSON representation for some predefined types. For type
2720 * "object" the function calls itself recursively unless the object has the
2721 * function property "toJSONProtocol" in which case that is used. This is not
2722 * a general implementation but sufficient for the debugger. Note that circular
2723 * structures will cause infinite recursion.
2724 * @param {Object} object The object to format as JSON
2725 * @return {string} JSON formatted object value
2726 */
2727function SimpleObjectToJSON_(object) {
2728 var content = [];
2729 for (var key in object) {
2730 // Only consider string keys.
2731 if (typeof key == 'string') {
2732 var property_value = object[key];
2733
2734 // Format the value based on its type.
2735 var property_value_json;
2736 switch (typeof property_value) {
2737 case 'object':
Ben Murdoch086aeea2011-05-13 15:57:08 +01002738 if (property_value === null) {
2739 property_value_json = 'null';
2740 } else if (typeof property_value.toJSONProtocol == 'function') {
Steve Blocka7e24c12009-10-30 11:49:00 +00002741 property_value_json = property_value.toJSONProtocol(true)
2742 } else if (property_value.constructor.name == 'Array'){
2743 property_value_json = SimpleArrayToJSON_(property_value);
2744 } else {
2745 property_value_json = SimpleObjectToJSON_(property_value);
2746 }
2747 break;
2748
2749 case 'boolean':
2750 property_value_json = BooleanToJSON_(property_value);
2751 break;
2752
2753 case 'number':
2754 property_value_json = NumberToJSON_(property_value);
2755 break;
2756
2757 case 'string':
2758 property_value_json = StringToJSON_(property_value);
2759 break;
2760
2761 default:
2762 property_value_json = null;
2763 }
2764
2765 // Add the property if relevant.
2766 if (property_value_json) {
2767 content.push(StringToJSON_(key) + ':' + property_value_json);
2768 }
2769 }
2770 }
2771
2772 // Make JSON object representation.
2773 return '{' + content.join(',') + '}';
2774}
2775
2776
2777/**
2778 * Convert an array to its JSON representation. This is a VERY simple
2779 * implementation just to support what is needed for the debugger.
2780 * @param {Array} arrya The array to format as JSON
2781 * @return {string} JSON formatted array value
2782 */
2783function SimpleArrayToJSON_(array) {
2784 // Make JSON array representation.
2785 var json = '[';
2786 for (var i = 0; i < array.length; i++) {
2787 if (i != 0) {
2788 json += ',';
2789 }
2790 var elem = array[i];
2791 if (elem.toJSONProtocol) {
2792 json += elem.toJSONProtocol(true)
2793 } else if (typeof(elem) === 'object') {
2794 json += SimpleObjectToJSON_(elem);
2795 } else if (typeof(elem) === 'boolean') {
2796 json += BooleanToJSON_(elem);
2797 } else if (typeof(elem) === 'number') {
2798 json += NumberToJSON_(elem);
2799 } else if (typeof(elem) === 'string') {
2800 json += StringToJSON_(elem);
2801 } else {
2802 json += elem;
2803 }
2804 }
2805 json += ']';
2806 return json;
2807}