debug-debugger.js revision 257744e915dfc84d6d07a6b2accf8402d9ffc708
13100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Copyright 2006-2008 the V8 project authors. All rights reserved.
23100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Redistribution and use in source and binary forms, with or without
33100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// modification, are permitted provided that the following conditions are
43100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// met:
53100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//
63100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//     * Redistributions of source code must retain the above copyright
73100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       notice, this list of conditions and the following disclaimer.
83100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//     * Redistributions in binary form must reproduce the above
93100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       copyright notice, this list of conditions and the following
103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       disclaimer in the documentation and/or other materials provided
113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       with the distribution.
123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//     * Neither the name of Google Inc. nor the names of its
133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       contributors may be used to endorse or promote products derived
143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//       from this software without specific prior written permission.
153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu//
163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Default number of frames to include in the response to backtrace request.
293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescuconst kDefaultBacktraceLength = 10;
303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescuconst Debug = {};
323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Regular expression to skip "crud" at the beginning of a source line which is
343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// not really code. Currently the regular expression matches whitespace and
353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// comments.
363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescuconst sourceLineBeginningSkip = /^(?:\s*(?:\/\*.*?\*\/)*)*/;
373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Debug events which can occour in the V8 JavaScript engine. These originate
393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// from the API include file debug.h.
403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.DebugEvent = { Break: 1,
413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                     Exception: 2,
423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                     NewFunction: 3,
433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                     BeforeCompile: 4,
443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                     AfterCompile: 5,
453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                     ScriptCollected: 6 };
463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Types of exceptions that can be broken upon.
480d5e116f6aee03185f237311a943491bb079a768Kristian MonsenDebug.ExceptionBreak = { Caught : 0,
493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                         Uncaught: 1 };
503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// The different types of steps.
523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.StepAction = { StepOut: 0,
533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                     StepNext: 1,
543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                     StepIn: 2,
553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                     StepMin: 3,
563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                     StepInMin: 4 };
573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// The different types of scripts matching enum ScriptType in objects.h.
593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.ScriptType = { Native: 0,
603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                     Extension: 1,
613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                     Normal: 2 };
623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// The different types of script compilations matching enum
643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Script::CompilationType in objects.h.
653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.ScriptCompilationType = { Host: 0,
663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                Eval: 1,
673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                JSON: 2 };
683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// The different script break point types.
703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.ScriptBreakPointType = { ScriptId: 0,
713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                               ScriptName: 1 };
723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction ScriptTypeFlag(type) {
743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return (1 << type);
753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Globals.
783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescuvar next_response_seq = 0;
793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescuvar next_break_point_number = 1;
803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescuvar break_points = [];
813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescuvar script_break_points = [];
82bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdochvar debugger_flags = {
83bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  breakPointsActive: {
84bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch    value: true,
85bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch    getValue: function() { return this.value; },
86bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch    setValue: function(value) {
87bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      this.value = !!value;
88bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      %SetDisableBreak(!this.value);
89bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch    }
900d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  },
910d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  breakOnCaughtException: {
920d5e116f6aee03185f237311a943491bb079a768Kristian Monsen    getValue: function() { return Debug.isBreakOnException(); },
930d5e116f6aee03185f237311a943491bb079a768Kristian Monsen    setValue: function(value) {
940d5e116f6aee03185f237311a943491bb079a768Kristian Monsen      if (value) {
950d5e116f6aee03185f237311a943491bb079a768Kristian Monsen        Debug.setBreakOnException();
960d5e116f6aee03185f237311a943491bb079a768Kristian Monsen      } else {
970d5e116f6aee03185f237311a943491bb079a768Kristian Monsen        Debug.clearBreakOnException();
980d5e116f6aee03185f237311a943491bb079a768Kristian Monsen      }
990d5e116f6aee03185f237311a943491bb079a768Kristian Monsen    }
1000d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  },
1010d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  breakOnUncaughtException: {
1020d5e116f6aee03185f237311a943491bb079a768Kristian Monsen    getValue: function() { return Debug.isBreakOnUncaughtException(); },
1030d5e116f6aee03185f237311a943491bb079a768Kristian Monsen    setValue: function(value) {
1040d5e116f6aee03185f237311a943491bb079a768Kristian Monsen      if (value) {
1050d5e116f6aee03185f237311a943491bb079a768Kristian Monsen        Debug.setBreakOnUncaughtException();
1060d5e116f6aee03185f237311a943491bb079a768Kristian Monsen      } else {
1070d5e116f6aee03185f237311a943491bb079a768Kristian Monsen        Debug.clearBreakOnUncaughtException();
1080d5e116f6aee03185f237311a943491bb079a768Kristian Monsen      }
1090d5e116f6aee03185f237311a943491bb079a768Kristian Monsen    }
1100d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  },
111bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch};
112e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdochvar lol_is_enabled = %HasLOLEnabled();
1133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Create a new break point object and add it to the list of break points.
116b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdochfunction MakeBreakPoint(source_position, opt_script_break_point) {
117b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  var break_point = new BreakPoint(source_position, opt_script_break_point);
1183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  break_points.push(break_point);
1193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return break_point;
1203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
1213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Object representing a break point.
1243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// NOTE: This object does not have a reference to the function having break
1253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// point as this would cause function not to be garbage collected when it is
1263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// not used any more. We do not want break points to keep functions alive.
127b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdochfunction BreakPoint(source_position, opt_script_break_point) {
1283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.source_position_ = source_position;
1293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (opt_script_break_point) {
1303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.script_break_point_ = opt_script_break_point;
1313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
1323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.number_ = next_break_point_number++;
1333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
1343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.hit_count_ = 0;
1353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.active_ = true;
1363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.condition_ = null;
1373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.ignoreCount_ = 0;
1383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
1393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.number = function() {
1423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.number_;
1433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
1443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.func = function() {
1473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.func_;
1483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
1493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.source_position = function() {
1523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.source_position_;
1533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
1543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.hit_count = function() {
1573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.hit_count_;
1583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
1593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.active = function() {
1623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.script_break_point()) {
1633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return this.script_break_point().active();
1643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
1653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.active_;
1663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
1673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.condition = function() {
1703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.script_break_point() && this.script_break_point().condition()) {
1713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return this.script_break_point().condition();
1723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
1733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.condition_;
1743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
1753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.ignoreCount = function() {
1783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.ignoreCount_;
1793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
1803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.script_break_point = function() {
1833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.script_break_point_;
1843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
1853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.enable = function() {
1883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.active_ = true;
1893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
1903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.disable = function() {
1933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.active_ = false;
1943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
1953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.setCondition = function(condition) {
1983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.condition_ = condition;
1993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
2003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.setIgnoreCount = function(ignoreCount) {
2033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.ignoreCount_ = ignoreCount;
2043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
2053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakPoint.prototype.isTriggered = function(exec_state) {
2083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Break point not active - not triggered.
2093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!this.active()) return false;
2103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for conditional break point.
2123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.condition()) {
2133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // If break point has condition try to evaluate it in the top frame.
2143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    try {
2153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      var mirror = exec_state.frame(0).evaluate(this.condition());
2163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // If no sensible mirror or non true value break point not triggered.
2173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (!(mirror instanceof ValueMirror) || !%ToBoolean(mirror.value_)) {
2183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        return false;
2193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
2203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } catch (e) {
2213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // Exception evaluating condition counts as not triggered.
2223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return false;
2233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
2243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
2253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Update the hit count.
2273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.hit_count_++;
2283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.script_break_point_) {
2293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.script_break_point_.hit_count_++;
2303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
2313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // If the break point has an ignore count it is not triggered.
2333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.ignoreCount_ > 0) {
2343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.ignoreCount_--;
2353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return false;
2363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
2373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Break point triggered.
2393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return true;
2403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
2413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Function called from the runtime when a break point is hit. Returns true if
2443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// the break point is triggered and supposed to break execution.
2453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction IsBreakPointTriggered(break_id, break_point) {
2463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return break_point.isTriggered(MakeExecutionState(break_id));
2473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
2483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Object representing a script break point. The script is referenced by its
2513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// script name or script id and the break point is represented as line and
2523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// column.
2533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction ScriptBreakPoint(type, script_id_or_name, opt_line, opt_column,
2543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                          opt_groupId) {
2553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.type_ = type;
2563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (type == Debug.ScriptBreakPointType.ScriptId) {
2573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.script_id_ = script_id_or_name;
2583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {  // type == Debug.ScriptBreakPointType.ScriptName
2593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.script_name_ = script_id_or_name;
2603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
2613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.line_ = opt_line || 0;
2623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.column_ = opt_column;
2633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.groupId_ = opt_groupId;
2643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.hit_count_ = 0;
2653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.active_ = true;
2663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.condition_ = null;
2673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.ignoreCount_ = 0;
2688defd9ff6930b4e24729971a61cf7469daf119beSteve Block  this.break_points_ = [];
2693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
2703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2726ded16be15dd865a9b21ea304d5273c8be299c87Steve Block//Creates a clone of script breakpoint that is linked to another script.
2736ded16be15dd865a9b21ea304d5273c8be299c87Steve BlockScriptBreakPoint.prototype.cloneForOtherScript = function (other_script) {
2746ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  var copy = new ScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId,
2756ded16be15dd865a9b21ea304d5273c8be299c87Steve Block      other_script.id, this.line_, this.column_, this.groupId_);
2766ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  copy.number_ = next_break_point_number++;
2776ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  script_break_points.push(copy);
278bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
2796ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  copy.hit_count_ = this.hit_count_;
2806ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  copy.active_ = this.active_;
2816ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  copy.condition_ = this.condition_;
2826ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  copy.ignoreCount_ = this.ignoreCount_;
2836ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  return copy;
2846ded16be15dd865a9b21ea304d5273c8be299c87Steve Block}
2856ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
2866ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
2873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.number = function() {
2883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.number_;
2893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
2903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.groupId = function() {
2933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.groupId_;
2943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
2953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.type = function() {
2983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.type_;
2993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.script_id = function() {
3033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.script_id_;
3043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.script_name = function() {
3083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.script_name_;
3093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.line = function() {
3133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.line_;
3143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.column = function() {
3183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.column_;
3193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3228defd9ff6930b4e24729971a61cf7469daf119beSteve BlockScriptBreakPoint.prototype.actual_locations = function() {
3238defd9ff6930b4e24729971a61cf7469daf119beSteve Block  var locations = [];
3248defd9ff6930b4e24729971a61cf7469daf119beSteve Block  for (var i = 0; i < this.break_points_.length; i++) {
3258defd9ff6930b4e24729971a61cf7469daf119beSteve Block    locations.push(this.break_points_[i].actual_location);
3268defd9ff6930b4e24729971a61cf7469daf119beSteve Block  }
3278defd9ff6930b4e24729971a61cf7469daf119beSteve Block  return locations;
3288defd9ff6930b4e24729971a61cf7469daf119beSteve Block}
3298defd9ff6930b4e24729971a61cf7469daf119beSteve Block
3308defd9ff6930b4e24729971a61cf7469daf119beSteve Block
3316ded16be15dd865a9b21ea304d5273c8be299c87Steve BlockScriptBreakPoint.prototype.update_positions = function(line, column) {
3326ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  this.line_ = line;
3336ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  this.column_ = column;
3346ded16be15dd865a9b21ea304d5273c8be299c87Steve Block}
3356ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
3366ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
3373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.hit_count = function() {
3383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.hit_count_;
3393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.active = function() {
3433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.active_;
3443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.condition = function() {
3483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.condition_;
3493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.ignoreCount = function() {
3533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.ignoreCount_;
3543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.enable = function() {
3583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.active_ = true;
3593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.disable = function() {
3633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.active_ = false;
3643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.setCondition = function(condition) {
3683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.condition_ = condition;
3693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.setIgnoreCount = function(ignoreCount) {
3733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.ignoreCount_ = ignoreCount;
3743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Set ignore count on all break points created from this script break point.
3768defd9ff6930b4e24729971a61cf7469daf119beSteve Block  for (var i = 0; i < this.break_points_.length; i++) {
3778defd9ff6930b4e24729971a61cf7469daf119beSteve Block    this.break_points_[i].setIgnoreCount(ignoreCount);
3783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
3793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Check whether a script matches this script break point. Currently this is
3833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// only based on script name.
3843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.matchesScript = function(script) {
3853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.type_ == Debug.ScriptBreakPointType.ScriptId) {
3863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return this.script_id_ == script.id;
3873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {  // this.type_ == Debug.ScriptBreakPointType.ScriptName
3886ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    return this.script_name_ == script.nameOrSourceURL() &&
3893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu           script.line_offset <= this.line_  &&
3903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu           this.line_ < script.line_offset + script.lineCount();
3913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
3923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
3933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
3953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Set the script break point in a script.
3963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.set = function (script) {
3973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var column = this.column();
3983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var line = this.line();
3993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // If the column is undefined the break is on the line. To help locate the
4003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // first piece of breakable code on the line try to find the column on the
4013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // line which contains some source.
4023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (IS_UNDEFINED(column)) {
4033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var source_line = script.sourceLine(this.line());
4043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Allocate array for caching the columns where the actual source starts.
4063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (!script.sourceColumnStart_) {
4073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      script.sourceColumnStart_ = new Array(script.lineCount());
4083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
4093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Fill cache if needed and get column where the actual source starts.
4113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (IS_UNDEFINED(script.sourceColumnStart_[line])) {
4123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      script.sourceColumnStart_[line] =
4133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu          source_line.match(sourceLineBeginningSkip)[0].length;
4143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
4153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    column = script.sourceColumnStart_[line];
4163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
4173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Convert the line and column into an absolute position within the script.
4198defd9ff6930b4e24729971a61cf7469daf119beSteve Block  var position = Debug.findScriptSourcePosition(script, this.line(), column);
4203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // If the position is not found in the script (the script might be shorter
4223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // than it used to be) just ignore it.
4238defd9ff6930b4e24729971a61cf7469daf119beSteve Block  if (position === null) return;
4243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Create a break point object and set the break point.
426b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  break_point = MakeBreakPoint(position, this);
4273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  break_point.setIgnoreCount(this.ignoreCount());
4288defd9ff6930b4e24729971a61cf7469daf119beSteve Block  var actual_position = %SetScriptBreakPoint(script, position, break_point);
4298defd9ff6930b4e24729971a61cf7469daf119beSteve Block  if (IS_UNDEFINED(actual_position)) {
4308defd9ff6930b4e24729971a61cf7469daf119beSteve Block    actual_position = position;
4318defd9ff6930b4e24729971a61cf7469daf119beSteve Block  }
4328defd9ff6930b4e24729971a61cf7469daf119beSteve Block  var actual_location = script.locationFromPosition(actual_position, true);
4338defd9ff6930b4e24729971a61cf7469daf119beSteve Block  break_point.actual_location = { line: actual_location.line,
4348defd9ff6930b4e24729971a61cf7469daf119beSteve Block                                  column: actual_location.column };
4358defd9ff6930b4e24729971a61cf7469daf119beSteve Block  this.break_points_.push(break_point);
4363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return break_point;
4373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
4383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Clear all the break points created from this script break point
4413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptBreakPoint.prototype.clear = function () {
4423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var remaining_break_points = [];
4433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < break_points.length; i++) {
4443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (break_points[i].script_break_point() &&
4453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        break_points[i].script_break_point() === this) {
4463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      %ClearBreakPoint(break_points[i]);
4473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } else {
4483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      remaining_break_points.push(break_points[i]);
4493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
4503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
4513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  break_points = remaining_break_points;
4528defd9ff6930b4e24729971a61cf7469daf119beSteve Block  this.break_points_ = [];
4533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
4543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Function called from runtime when a new script is compiled to set any script
4573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// break points set in this script.
4583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction UpdateScriptBreakPoints(script) {
4593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < script_break_points.length; i++) {
4603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (script_break_points[i].type() == Debug.ScriptBreakPointType.ScriptName &&
4613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        script_break_points[i].matchesScript(script)) {
4623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      script_break_points[i].set(script);
4633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
4643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
4653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
4663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4686ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockfunction GetScriptBreakPoints(script) {
4696ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  var result = [];
4706ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  for (var i = 0; i < script_break_points.length; i++) {
4716ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    if (script_break_points[i].matchesScript(script)) {
4726ded16be15dd865a9b21ea304d5273c8be299c87Steve Block      result.push(script_break_points[i]);
4736ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    }
4746ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  }
4756ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  return result;
4766ded16be15dd865a9b21ea304d5273c8be299c87Steve Block}
4776ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
4786ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
4793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.setListener = function(listener, opt_data) {
4803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_FUNCTION(listener) && !IS_UNDEFINED(listener) && !IS_NULL(listener)) {
4813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    throw new Error('Parameters have wrong types.');
4823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
4833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  %SetDebugEventListener(listener, opt_data);
4843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
4853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.breakExecution = function(f) {
4883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  %Break();
4893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
4903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.breakLocations = function(f) {
4923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
4933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %GetBreakLocations(f);
4943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
4953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
4963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Returns a Script object. If the parameter is a function the return value
4973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// is the script in which the function is defined. If the parameter is a string
4983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// the return value is the script for which the script name has that string
4993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// value.  If it is a regexp and there is a unique script whose name matches
5003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// we return that, otherwise undefined.
5013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.findScript = function(func_or_script_name) {
5023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (IS_FUNCTION(func_or_script_name)) {
5033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return %FunctionGetScript(func_or_script_name);
5043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else if (IS_REGEXP(func_or_script_name)) {
5053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var scripts = Debug.scripts();
5063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var last_result = null;
5073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var result_count = 0;
5083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    for (var i in scripts) {
5093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      var script = scripts[i];
5103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (func_or_script_name.test(script.name)) {
5113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        last_result = script;
5123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        result_count++;
5133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
5143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
5153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Return the unique script matching the regexp.  If there are more
5163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // than one we don't return a value since there is no good way to
5173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // decide which one to return.  Returning a "random" one, say the
5183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // first, would introduce nondeterminism (or something close to it)
5193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // because the order is the heap iteration order.
5203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (result_count == 1) {
5213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return last_result;
5223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } else {
5233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return undefined;
5243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
5253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
5263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return %GetScript(func_or_script_name);
5273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
5283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
5293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Returns the script source. If the parameter is a function the return value
5313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// is the script source for the script in which the function is defined. If the
5323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// parameter is a string the return value is the script for which the script
5333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// name has that string value.
5343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.scriptSource = function(func_or_script_name) {
5353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.findScript(func_or_script_name).source;
5363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
5373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.source = function(f) {
5393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
5403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %FunctionGetSourceCode(f);
5413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
5423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.disassemble = function(f) {
5443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
5453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %DebugDisassembleFunction(f);
5463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
5473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.disassembleConstructor = function(f) {
5493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
5503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %DebugDisassembleConstructor(f);
5513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
5523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5536ded16be15dd865a9b21ea304d5273c8be299c87Steve BlockDebug.ExecuteInDebugContext = function(f, without_debugger) {
5546ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
5556ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  return %ExecuteInDebugContext(f, !!without_debugger);
5566ded16be15dd865a9b21ea304d5273c8be299c87Steve Block};
5576ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
5583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.sourcePosition = function(f) {
5593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
5603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %FunctionGetScriptSourcePosition(f);
5613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
5623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.findFunctionSourceLocation = function(func, opt_line, opt_column) {
5653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var script = %FunctionGetScript(func);
5663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var script_offset = %FunctionGetScriptSourcePosition(func);
5673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return script.locationFromLine(opt_line, opt_column, script_offset);
5683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
5693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Returns the character position in a script based on a line number and an
5723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// optional position within that line.
5733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.findScriptSourcePosition = function(script, opt_line, opt_column) {
5743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var location = script.locationFromLine(opt_line, opt_column);
5753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return location ? location.position : null;
5763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
5773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.findBreakPoint = function(break_point_number, remove) {
5803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var break_point;
5813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < break_points.length; i++) {
5823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (break_points[i].number() == break_point_number) {
5833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      break_point = break_points[i];
5843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // Remove the break point from the list if requested.
5853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (remove) {
5863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        break_points.splice(i, 1);
5873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
5883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      break;
5893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
5903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
5913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (break_point) {
5923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return break_point;
5933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
5943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return this.findScriptBreakPoint(break_point_number, remove);
5953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
5963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
5973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
5988defd9ff6930b4e24729971a61cf7469daf119beSteve BlockDebug.findBreakPointActualLocations = function(break_point_number) {
5998defd9ff6930b4e24729971a61cf7469daf119beSteve Block  for (var i = 0; i < script_break_points.length; i++) {
6008defd9ff6930b4e24729971a61cf7469daf119beSteve Block    if (script_break_points[i].number() == break_point_number) {
6018defd9ff6930b4e24729971a61cf7469daf119beSteve Block      return script_break_points[i].actual_locations();
6028defd9ff6930b4e24729971a61cf7469daf119beSteve Block    }
6038defd9ff6930b4e24729971a61cf7469daf119beSteve Block  }
6048defd9ff6930b4e24729971a61cf7469daf119beSteve Block  for (var i = 0; i < break_points.length; i++) {
6058defd9ff6930b4e24729971a61cf7469daf119beSteve Block    if (break_points[i].number() == break_point_number) {
6068defd9ff6930b4e24729971a61cf7469daf119beSteve Block      return [break_points[i].actual_location];
6078defd9ff6930b4e24729971a61cf7469daf119beSteve Block    }
6088defd9ff6930b4e24729971a61cf7469daf119beSteve Block  }
6098defd9ff6930b4e24729971a61cf7469daf119beSteve Block  return [];
6108defd9ff6930b4e24729971a61cf7469daf119beSteve Block}
6113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
6123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.setBreakPoint = function(func, opt_line, opt_column, opt_condition) {
6133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_FUNCTION(func)) throw new Error('Parameters have wrong types.');
6143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Break points in API functions are not supported.
6153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (%FunctionIsAPIFunction(func)) {
6163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    throw new Error('Cannot set break point in native code.');
6173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
6183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Find source position relative to start of the function
6193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var break_position =
6203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      this.findFunctionSourceLocation(func, opt_line, opt_column).position;
6213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var source_position = break_position - this.sourcePosition(func);
6223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Find the script for the function.
6233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var script = %FunctionGetScript(func);
6243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Break in builtin JavaScript code is not supported.
6253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (script.type == Debug.ScriptType.Native) {
6263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    throw new Error('Cannot set break point in native code.');
6273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
6283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // If the script for the function has a name convert this to a script break
6293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // point.
6303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (script && script.id) {
6313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Adjust the source position to be script relative.
6323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    source_position += %FunctionGetScriptSourcePosition(func);
6333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Find line and column for the position in the script and set a script
6343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // break point from that.
6353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var location = script.locationFromPosition(source_position, false);
6363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return this.setScriptBreakPointById(script.id,
6373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                        location.line, location.column,
6383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                        opt_condition);
6393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
6403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Set a break point directly on the function.
641b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    var break_point = MakeBreakPoint(source_position);
6428defd9ff6930b4e24729971a61cf7469daf119beSteve Block    var actual_position =
6438defd9ff6930b4e24729971a61cf7469daf119beSteve Block        %SetFunctionBreakPoint(func, source_position, break_point);
6448defd9ff6930b4e24729971a61cf7469daf119beSteve Block    actual_position += this.sourcePosition(func);
6458defd9ff6930b4e24729971a61cf7469daf119beSteve Block    var actual_location = script.locationFromPosition(actual_position, true);
6468defd9ff6930b4e24729971a61cf7469daf119beSteve Block    break_point.actual_location = { line: actual_location.line,
6478defd9ff6930b4e24729971a61cf7469daf119beSteve Block                                    column: actual_location.column };
6483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    break_point.setCondition(opt_condition);
6493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return break_point.number();
6503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
6513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
6523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
6533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
654b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben MurdochDebug.setBreakPointByScriptIdAndPosition = function(script_id, position,
655b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                                                    condition, enabled)
656b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch{
657b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  break_point = MakeBreakPoint(position);
658b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  break_point.setCondition(condition);
659b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  if (!enabled)
660b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    break_point.disable();
661b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  var scripts = this.scripts();
662b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  for (var i = 0; i < scripts.length; i++) {
663b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    if (script_id == scripts[i].id) {
664b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      break_point.actual_position = %SetScriptBreakPoint(scripts[i], position,
665b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch                                                         break_point);
666b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch      break;
667b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch    }
668b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  }
669b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch  return break_point;
670b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch};
671b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
672b8e0da25ee8efac3bb05cd6b2730aafbd96119f4Ben Murdoch
6733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.enableBreakPoint = function(break_point_number) {
6743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var break_point = this.findBreakPoint(break_point_number, false);
675086aeeaae12517475c22695a200be45495516549Ben Murdoch  // Only enable if the breakpoint hasn't been deleted:
676086aeeaae12517475c22695a200be45495516549Ben Murdoch  if (break_point) {
677086aeeaae12517475c22695a200be45495516549Ben Murdoch    break_point.enable();
678086aeeaae12517475c22695a200be45495516549Ben Murdoch  }
6793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
6803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
6813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
6823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.disableBreakPoint = function(break_point_number) {
6833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var break_point = this.findBreakPoint(break_point_number, false);
684086aeeaae12517475c22695a200be45495516549Ben Murdoch  // Only enable if the breakpoint hasn't been deleted:
685086aeeaae12517475c22695a200be45495516549Ben Murdoch  if (break_point) {
686086aeeaae12517475c22695a200be45495516549Ben Murdoch    break_point.disable();
687086aeeaae12517475c22695a200be45495516549Ben Murdoch  }
6883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
6893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
6903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
6913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.changeBreakPointCondition = function(break_point_number, condition) {
6923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var break_point = this.findBreakPoint(break_point_number, false);
6933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  break_point.setCondition(condition);
6943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
6953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
6963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
6973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.changeBreakPointIgnoreCount = function(break_point_number, ignoreCount) {
6983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (ignoreCount < 0) {
6993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    throw new Error('Invalid argument');
7003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
7013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var break_point = this.findBreakPoint(break_point_number, false);
7023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  break_point.setIgnoreCount(ignoreCount);
7033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
7043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.clearBreakPoint = function(break_point_number) {
7073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var break_point = this.findBreakPoint(break_point_number, true);
7083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (break_point) {
7093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return %ClearBreakPoint(break_point);
7103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
7113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    break_point = this.findScriptBreakPoint(break_point_number, true);
7123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (!break_point) {
7133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      throw new Error('Invalid breakpoint');
7143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
7153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
7163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
7173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.clearAllBreakPoints = function() {
7203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < break_points.length; i++) {
7213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    break_point = break_points[i];
7223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    %ClearBreakPoint(break_point);
7233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
7243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  break_points = [];
7253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
7263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
728086aeeaae12517475c22695a200be45495516549Ben MurdochDebug.disableAllBreakPoints = function() {
729086aeeaae12517475c22695a200be45495516549Ben Murdoch  // Disable all user defined breakpoints:
730086aeeaae12517475c22695a200be45495516549Ben Murdoch  for (var i = 1; i < next_break_point_number; i++) {
731086aeeaae12517475c22695a200be45495516549Ben Murdoch    Debug.disableBreakPoint(i);
732086aeeaae12517475c22695a200be45495516549Ben Murdoch  }
733086aeeaae12517475c22695a200be45495516549Ben Murdoch  // Disable all exception breakpoints:
734086aeeaae12517475c22695a200be45495516549Ben Murdoch  %ChangeBreakOnException(Debug.ExceptionBreak.Caught, false);
735086aeeaae12517475c22695a200be45495516549Ben Murdoch  %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, false);
736086aeeaae12517475c22695a200be45495516549Ben Murdoch};
737086aeeaae12517475c22695a200be45495516549Ben Murdoch
738086aeeaae12517475c22695a200be45495516549Ben Murdoch
7393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.findScriptBreakPoint = function(break_point_number, remove) {
7403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var script_break_point;
7413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < script_break_points.length; i++) {
7423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (script_break_points[i].number() == break_point_number) {
7433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      script_break_point = script_break_points[i];
7443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // Remove the break point from the list if requested.
7453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (remove) {
7463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        script_break_point.clear();
7473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        script_break_points.splice(i,1);
7483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
7493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      break;
7503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
7513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
7523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return script_break_point;
7533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
7543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Sets a breakpoint in a script identified through id or name at the
7573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// specified source line and column within that line.
7583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.setScriptBreakPoint = function(type, script_id_or_name,
7593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                     opt_line, opt_column, opt_condition,
7603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                     opt_groupId) {
7613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Create script break point object.
7623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var script_break_point =
7633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      new ScriptBreakPoint(type, script_id_or_name, opt_line, opt_column,
7643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                           opt_groupId);
7653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Assign number to the new script break point and add it.
7673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  script_break_point.number_ = next_break_point_number++;
7683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  script_break_point.setCondition(opt_condition);
7693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  script_break_points.push(script_break_point);
7703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Run through all scripts to see if this script break point matches any
7723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // loaded scripts.
7733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var scripts = this.scripts();
7743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < scripts.length; i++) {
7753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (script_break_point.matchesScript(scripts[i])) {
7763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      script_break_point.set(scripts[i]);
7773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
7783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
7793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return script_break_point.number();
7813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
7823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.setScriptBreakPointById = function(script_id,
7853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                         opt_line, opt_column,
7863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                         opt_condition, opt_groupId) {
7873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId,
7883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                  script_id, opt_line, opt_column,
7893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                  opt_condition, opt_groupId);
7903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
7913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
7933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.setScriptBreakPointByName = function(script_name,
7943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                           opt_line, opt_column,
7953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                           opt_condition, opt_groupId) {
7963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptName,
7973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                  script_name, opt_line, opt_column,
7983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                  opt_condition, opt_groupId);
7993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
8003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.enableScriptBreakPoint = function(break_point_number) {
8033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var script_break_point = this.findScriptBreakPoint(break_point_number, false);
8043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  script_break_point.enable();
8053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
8063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.disableScriptBreakPoint = function(break_point_number) {
8093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var script_break_point = this.findScriptBreakPoint(break_point_number, false);
8103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  script_break_point.disable();
8113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
8123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.changeScriptBreakPointCondition = function(break_point_number, condition) {
8153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var script_break_point = this.findScriptBreakPoint(break_point_number, false);
8163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  script_break_point.setCondition(condition);
8173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
8183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.changeScriptBreakPointIgnoreCount = function(break_point_number, ignoreCount) {
8213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (ignoreCount < 0) {
8223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    throw new Error('Invalid argument');
8233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
8243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var script_break_point = this.findScriptBreakPoint(break_point_number, false);
8253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  script_break_point.setIgnoreCount(ignoreCount);
8263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
8273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.scriptBreakPoints = function() {
8303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return script_break_points;
8313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
8323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.clearStepping = function() {
8353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  %ClearStepping();
8363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
8373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.setBreakOnException = function() {
8390d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  return %ChangeBreakOnException(Debug.ExceptionBreak.Caught, true);
8403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
8413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.clearBreakOnException = function() {
8430d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  return %ChangeBreakOnException(Debug.ExceptionBreak.Caught, false);
8440d5e116f6aee03185f237311a943491bb079a768Kristian Monsen};
8450d5e116f6aee03185f237311a943491bb079a768Kristian Monsen
8460d5e116f6aee03185f237311a943491bb079a768Kristian MonsenDebug.isBreakOnException = function() {
8470d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  return !!%IsBreakOnException(Debug.ExceptionBreak.Caught);
8483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
8493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.setBreakOnUncaughtException = function() {
8513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, true);
8523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
8533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.clearBreakOnUncaughtException = function() {
8553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, false);
8563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
8573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8580d5e116f6aee03185f237311a943491bb079a768Kristian MonsenDebug.isBreakOnUncaughtException = function() {
8590d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  return !!%IsBreakOnException(Debug.ExceptionBreak.Uncaught);
8600d5e116f6aee03185f237311a943491bb079a768Kristian Monsen};
8610d5e116f6aee03185f237311a943491bb079a768Kristian Monsen
8623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.showBreakPoints = function(f, full) {
8633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
8643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var source = full ? this.scriptSource(f) : this.source(f);
8653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var offset = full ? this.sourcePosition(f) : 0;
8663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var locations = this.breakLocations(f);
8673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!locations) return source;
8683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  locations.sort(function(x, y) { return x - y; });
8693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var result = "";
8703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var prev_pos = 0;
8713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var pos;
8723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < locations.length; i++) {
8733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    pos = locations[i] - offset;
8743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    result += source.slice(prev_pos, pos);
8753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    result += "[B" + i + "]";
8763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    prev_pos = pos;
8773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
8783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  pos = source.length;
8793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  result += source.substring(prev_pos, pos);
8803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return result;
8813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
8823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Get all the scripts currently loaded. Locating all the scripts is based on
8853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// scanning the heap.
8863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebug.scripts = function() {
8873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Collect all scripts in the heap.
8883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %DebugGetLoadedScripts();
889bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch};
890bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
891bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
892bb769b257e753aafcbd96767abb2abc645eaa20cBen MurdochDebug.debuggerFlags = function() {
893bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  return debugger_flags;
894bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch};
895bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
896b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochDebug.MakeMirror = MakeMirror;
8973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
8983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction MakeExecutionState(break_id) {
8993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return new ExecutionState(break_id);
9003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
9013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction ExecutionState(break_id) {
9033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.break_id = break_id;
9043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.selected_frame = 0;
9053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
9063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExecutionState.prototype.prepareStep = function(opt_action, opt_count) {
9083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var action = Debug.StepAction.StepIn;
9093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_UNDEFINED(opt_action)) action = %ToNumber(opt_action);
9103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var count = opt_count ? %ToNumber(opt_count) : 1;
9113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %PrepareStep(this.break_id, action, count);
9133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
9143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
915b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochExecutionState.prototype.evaluateGlobal = function(source, disable_break,
916b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    opt_additional_context) {
917b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  return MakeMirror(%DebugEvaluateGlobal(this.break_id, source,
918b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                         Boolean(disable_break),
919b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                         opt_additional_context));
9203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExecutionState.prototype.frameCount = function() {
9233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %GetFrameCount(this.break_id);
9243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExecutionState.prototype.threadCount = function() {
9273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %GetThreadCount(this.break_id);
9283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExecutionState.prototype.frame = function(opt_index) {
9313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // If no index supplied return the selected frame.
9323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (opt_index == null) opt_index = this.selected_frame;
9336ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  if (opt_index < 0 || opt_index >= this.frameCount())
9346ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    throw new Error('Illegal frame index.');
9353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return new FrameMirror(this.break_id, opt_index);
9363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExecutionState.prototype.setSelectedFrame = function(index) {
9393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var i = %ToNumber(index);
9403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (i < 0 || i >= this.frameCount()) throw new Error('Illegal frame index.');
9413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.selected_frame = i;
9423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExecutionState.prototype.selectedFrame = function() {
9453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.selected_frame;
9463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExecutionState.prototype.debugCommandProcessor = function(opt_is_running) {
9493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return new DebugCommandProcessor(this, opt_is_running);
9503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction MakeBreakEvent(exec_state, break_points_hit) {
9543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return new BreakEvent(exec_state, break_points_hit);
9553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
9563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction BreakEvent(exec_state, break_points_hit) {
9593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.exec_state_ = exec_state;
9603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.break_points_hit_ = break_points_hit;
9613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
9623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakEvent.prototype.executionState = function() {
9653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_;
9663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakEvent.prototype.eventType = function() {
9703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return Debug.DebugEvent.Break;
9713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakEvent.prototype.func = function() {
9753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_.frame(0).func();
9763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakEvent.prototype.sourceLine = function() {
9803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_.frame(0).sourceLine();
9813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakEvent.prototype.sourceColumn = function() {
9853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_.frame(0).sourceColumn();
9863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakEvent.prototype.sourceLineText = function() {
9903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_.frame(0).sourceLineText();
9913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakEvent.prototype.breakPointsHit = function() {
9953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.break_points_hit_;
9963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
9973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
9993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuBreakEvent.prototype.toJSONProtocol = function() {
10003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var o = { seq: next_response_seq++,
10013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu            type: "event",
10023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu            event: "break",
10033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu            body: { invocationText: this.exec_state_.frame(0).invocationText(),
10043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                  }
10053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu          };
10063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Add script related information to the event if available.
10083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var script = this.func().script();
10093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (script) {
10103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.body.sourceLine = this.sourceLine(),
10113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.body.sourceColumn = this.sourceColumn(),
10123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.body.sourceLineText = this.sourceLineText(),
10133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.body.script = MakeScriptObject_(script, false);
10143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
10153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Add an Array of break points hit if any.
10173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.breakPointsHit()) {
10183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.body.breakpoints = [];
10193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    for (var i = 0; i < this.breakPointsHit().length; i++) {
10203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // Find the break point number. For break points originating from a
10213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // script break point supply the script break point number.
10223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      var breakpoint = this.breakPointsHit()[i];
10233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      var script_break_point = breakpoint.script_break_point();
10243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      var number;
10253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (script_break_point) {
10263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        number = script_break_point.number();
10273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else {
10283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        number = breakpoint.number();
10293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
10303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      o.body.breakpoints.push(number);
10313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
10323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
10333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return JSON.stringify(ObjectToProtocolObject_(o));
10343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
10353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction MakeExceptionEvent(exec_state, exception, uncaught) {
10383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return new ExceptionEvent(exec_state, exception, uncaught);
10393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
10403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction ExceptionEvent(exec_state, exception, uncaught) {
10433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.exec_state_ = exec_state;
10443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.exception_ = exception;
10453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.uncaught_ = uncaught;
10463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
10473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExceptionEvent.prototype.executionState = function() {
10503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_;
10513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
10523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExceptionEvent.prototype.eventType = function() {
10553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return Debug.DebugEvent.Exception;
10563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
10573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExceptionEvent.prototype.exception = function() {
10603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exception_;
10613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
10623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExceptionEvent.prototype.uncaught = function() {
10653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.uncaught_;
10663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
10673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExceptionEvent.prototype.func = function() {
10703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_.frame(0).func();
10713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
10723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExceptionEvent.prototype.sourceLine = function() {
10753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_.frame(0).sourceLine();
10763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
10773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExceptionEvent.prototype.sourceColumn = function() {
10803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_.frame(0).sourceColumn();
10813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
10823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExceptionEvent.prototype.sourceLineText = function() {
10853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_.frame(0).sourceLineText();
10863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
10873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuExceptionEvent.prototype.toJSONProtocol = function() {
10903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var o = new ProtocolMessage();
10913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  o.event = "exception";
10923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  o.body = { uncaught: this.uncaught_,
10933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu             exception: MakeMirror(this.exception_)
10943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu           };
10953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
10963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Exceptions might happen whithout any JavaScript frames.
10973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.exec_state_.frameCount() > 0) {
10983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.body.sourceLine = this.sourceLine();
10993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.body.sourceColumn = this.sourceColumn();
11003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.body.sourceLineText = this.sourceLineText();
11013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Add script information to the event if available.
11033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var script = this.func().script();
11043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (script) {
11053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      o.body.script = MakeScriptObject_(script, false);
11063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
11073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
11083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.body.sourceLine = -1;
11093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
11103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return o.toJSONProtocol();
11123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
11133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction MakeCompileEvent(exec_state, script, before) {
11163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return new CompileEvent(exec_state, script, before);
11173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
11183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction CompileEvent(exec_state, script, before) {
11213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.exec_state_ = exec_state;
11223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.script_ = MakeMirror(script);
11233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.before_ = before;
11243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
11253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuCompileEvent.prototype.executionState = function() {
11283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_;
11293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
11303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuCompileEvent.prototype.eventType = function() {
11333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.before_) {
11343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return Debug.DebugEvent.BeforeCompile;
11353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
11363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return Debug.DebugEvent.AfterCompile;
11373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
11383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
11393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuCompileEvent.prototype.script = function() {
11423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.script_;
11433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
11443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuCompileEvent.prototype.toJSONProtocol = function() {
11473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var o = new ProtocolMessage();
11483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  o.running = true;
11493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.before_) {
11503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.event = "beforeCompile";
11513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
11523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.event = "afterCompile";
11533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
11543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  o.body = {};
11553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  o.body.script = this.script_;
11563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return o.toJSONProtocol();
11583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
11593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction MakeNewFunctionEvent(func) {
11623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return new NewFunctionEvent(func);
11633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
11643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction NewFunctionEvent(func) {
11673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.func = func;
11683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
11693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuNewFunctionEvent.prototype.eventType = function() {
11723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return Debug.DebugEvent.NewFunction;
11733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
11743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuNewFunctionEvent.prototype.name = function() {
11773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.func.name;
11783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
11793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuNewFunctionEvent.prototype.setBreakPoint = function(p) {
11823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  Debug.setBreakPoint(this.func, p || 0);
11833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
11843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction MakeScriptCollectedEvent(exec_state, id) {
11873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return new ScriptCollectedEvent(exec_state, id);
11883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
11893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction ScriptCollectedEvent(exec_state, id) {
11923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.exec_state_ = exec_state;
11933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.id_ = id;
11943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
11953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
11973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptCollectedEvent.prototype.id = function() {
11983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.id_;
11993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
12003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptCollectedEvent.prototype.executionState = function() {
12033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.exec_state_;
12043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
12053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuScriptCollectedEvent.prototype.toJSONProtocol = function() {
12083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var o = new ProtocolMessage();
12093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  o.running = true;
12103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  o.event = "scriptCollected";
12113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  o.body = {};
12123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  o.body.script = { id: this.id() };
12133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return o.toJSONProtocol();
12143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
12153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction MakeScriptObject_(script, include_source) {
12183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var o = { id: script.id(),
12193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu            name: script.name(),
12203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu            lineOffset: script.lineOffset(),
12213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu            columnOffset: script.columnOffset(),
12223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu            lineCount: script.lineCount(),
12233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu          };
12243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_UNDEFINED(script.data())) {
12253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.data = script.data();
12263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
12273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (include_source) {
12283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    o.source = script.source();
12293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
12303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return o;
12313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
12323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction DebugCommandProcessor(exec_state, opt_is_running) {
12353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.exec_state_ = exec_state;
12363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.running_ = opt_is_running || false;
12373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
12383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.processDebugRequest = function (request) {
12413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.processDebugJSONRequest(request);
12423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
12433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction ProtocolMessage(request) {
12463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Update sequence number.
12473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.seq = next_response_seq++;
12483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (request) {
12503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // If message is based on a request this is a response. Fill the initial
12513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // response from the request.
12523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.type = 'response';
12533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.request_seq = request.seq;
12543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.command = request.command;
12553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
12563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // If message is not based on a request it is a dabugger generated event.
12573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.type = 'event';
12583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
12593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.success = true;
12603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Handler may set this field to control debugger state.
12613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.running = undefined;
12623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
12633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuProtocolMessage.prototype.setOption = function(name, value) {
12663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!this.options_) {
12673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.options_ = {};
12683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
12693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.options_[name] = value;
12703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
12713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuProtocolMessage.prototype.failed = function(message) {
12743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.success = false;
12753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  this.message = message;
12763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
12773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
12793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuProtocolMessage.prototype.toJSONProtocol = function() {
12803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Encode the protocol header.
12813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var json = {};
12823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  json.seq= this.seq;
12833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.request_seq) {
12843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    json.request_seq = this.request_seq;
12853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
12863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  json.type = this.type;
12873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.event) {
12883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    json.event = this.event;
12893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
12903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.command) {
12913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    json.command = this.command;
12923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
12933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.success) {
12943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    json.success = this.success;
12953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
12963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    json.success = false;
12973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
12983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.body) {
12993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Encode the body part.
13003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var bodyJson;
13013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var serializer = MakeMirrorSerializer(true, this.options_);
13023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (this.body instanceof Mirror) {
13033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      bodyJson = serializer.serializeValue(this.body);
13043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } else if (this.body instanceof Array) {
13053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      bodyJson = [];
13063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      for (var i = 0; i < this.body.length; i++) {
13073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        if (this.body[i] instanceof Mirror) {
13083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu          bodyJson.push(serializer.serializeValue(this.body[i]));
13093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        } else {
13103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu          bodyJson.push(ObjectToProtocolObject_(this.body[i], serializer));
13113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        }
13123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
13133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } else {
13143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      bodyJson = ObjectToProtocolObject_(this.body, serializer);
13153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
13163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    json.body = bodyJson;
13173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    json.refs = serializer.serializeReferencedObjects();
13183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
13193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.message) {
13203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    json.message = this.message;
13213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
13223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  json.running = this.running;
13233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return JSON.stringify(json);
13243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
13253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
13263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
13273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.createResponse = function(request) {
13283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return new ProtocolMessage(request);
13293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
13303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
13313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
13323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.processDebugJSONRequest = function(json_request) {
13333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var request;  // Current request.
13343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var response;  // Generated response.
13353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  try {
13363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    try {
13373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // Convert the JSON string to an object.
1338257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch      request = JSON.parse(json_request);
13393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
13403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // Create an initial response.
13413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response = this.createResponse(request);
13423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
13433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (!request.type) {
13443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        throw new Error('Type not specified');
13453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
13463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
13473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (request.type != 'request') {
13483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        throw new Error("Illegal type '" + request.type + "' in request");
13493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
13503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
13513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (!request.command) {
13523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        throw new Error('Command not specified');
13533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
13543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1355402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu      if (request.arguments) {
1356402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu        var args = request.arguments;
1357402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu        // TODO(yurys): remove request.arguments.compactFormat check once
1358402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu        // ChromeDevTools are switched to 'inlineRefs'
1359402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu        if (args.inlineRefs || args.compactFormat) {
1360402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu          response.setOption('inlineRefs', true);
1361402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu        }
1362402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu        if (!IS_UNDEFINED(args.maxStringLength)) {
1363402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu          response.setOption('maxStringLength', args.maxStringLength);
1364402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu        }
13653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
13663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
13673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (request.command == 'continue') {
13683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.continueRequest_(request, response);
13693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'break') {
13703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.breakRequest_(request, response);
13713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'setbreakpoint') {
13723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.setBreakPointRequest_(request, response);
13733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'changebreakpoint') {
13743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.changeBreakPointRequest_(request, response);
13753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'clearbreakpoint') {
13763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.clearBreakPointRequest_(request, response);
13773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'clearbreakpointgroup') {
13783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.clearBreakPointGroupRequest_(request, response);
1379086aeeaae12517475c22695a200be45495516549Ben Murdoch      } else if (request.command == 'disconnect') {
1380086aeeaae12517475c22695a200be45495516549Ben Murdoch        this.disconnectRequest_(request, response);
1381086aeeaae12517475c22695a200be45495516549Ben Murdoch      } else if (request.command == 'setexceptionbreak') {
1382086aeeaae12517475c22695a200be45495516549Ben Murdoch        this.setExceptionBreakRequest_(request, response);
138325f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      } else if (request.command == 'listbreakpoints') {
138425f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen        this.listBreakpointsRequest_(request, response);
13853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'backtrace') {
13863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.backtraceRequest_(request, response);
13873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'frame') {
13883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.frameRequest_(request, response);
13893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'scopes') {
13903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.scopesRequest_(request, response);
13913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'scope') {
13923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.scopeRequest_(request, response);
13933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'evaluate') {
13943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.evaluateRequest_(request, response);
1395e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      } else if (lol_is_enabled && request.command == 'getobj') {
1396e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch        this.getobjRequest_(request, response);
13973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'lookup') {
13983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.lookupRequest_(request, response);
13993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'references') {
14003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.referencesRequest_(request, response);
14013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'source') {
14023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.sourceRequest_(request, response);
14033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'scripts') {
14043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.scriptsRequest_(request, response);
14053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'threads') {
14063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.threadsRequest_(request, response);
14073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'suspend') {
14083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.suspendRequest_(request, response);
14093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'version') {
14103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.versionRequest_(request, response);
14113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (request.command == 'profile') {
1412bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch        this.profileRequest_(request, response);
14136ded16be15dd865a9b21ea304d5273c8be299c87Steve Block      } else if (request.command == 'changelive') {
1414bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch        this.changeLiveRequest_(request, response);
1415bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      } else if (request.command == 'flags') {
1416bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch        this.debuggerFlagsRequest_(request, response);
1417086aeeaae12517475c22695a200be45495516549Ben Murdoch      } else if (request.command == 'v8flags') {
1418086aeeaae12517475c22695a200be45495516549Ben Murdoch        this.v8FlagsRequest_(request, response);
1419086aeeaae12517475c22695a200be45495516549Ben Murdoch
1420086aeeaae12517475c22695a200be45495516549Ben Murdoch      // GC tools:
1421086aeeaae12517475c22695a200be45495516549Ben Murdoch      } else if (request.command == 'gc') {
1422086aeeaae12517475c22695a200be45495516549Ben Murdoch        this.gcRequest_(request, response);
1423086aeeaae12517475c22695a200be45495516549Ben Murdoch
1424e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      // LiveObjectList tools:
1425e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      } else if (lol_is_enabled && request.command == 'lol-capture') {
1426e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch        this.lolCaptureRequest_(request, response);
1427e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      } else if (lol_is_enabled && request.command == 'lol-delete') {
1428e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch        this.lolDeleteRequest_(request, response);
1429e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      } else if (lol_is_enabled && request.command == 'lol-diff') {
1430e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch        this.lolDiffRequest_(request, response);
1431e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      } else if (lol_is_enabled && request.command == 'lol-getid') {
1432e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch        this.lolGetIdRequest_(request, response);
1433e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      } else if (lol_is_enabled && request.command == 'lol-info') {
1434e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch        this.lolInfoRequest_(request, response);
1435e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      } else if (lol_is_enabled && request.command == 'lol-reset') {
1436e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch        this.lolResetRequest_(request, response);
1437e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      } else if (lol_is_enabled && request.command == 'lol-retainers') {
1438e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch        this.lolRetainersRequest_(request, response);
1439e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      } else if (lol_is_enabled && request.command == 'lol-path') {
1440e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch        this.lolPathRequest_(request, response);
1441e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      } else if (lol_is_enabled && request.command == 'lol-print') {
1442e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch        this.lolPrintRequest_(request, response);
1443e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch      } else if (lol_is_enabled && request.command == 'lol-stats') {
1444e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch        this.lolStatsRequest_(request, response);
1445e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
14463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else {
14473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        throw new Error('Unknown command "' + request.command + '" in request');
14483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
14493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } catch (e) {
14503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // If there is no response object created one (without command).
14513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (!response) {
14523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        response = this.createResponse();
14533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
14543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.success = false;
14553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.message = %ToString(e);
14563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
14573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
14583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Return the response as a JSON encoded string.
14593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    try {
14603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (!IS_UNDEFINED(response.running)) {
14613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        // Response controls running state.
14623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        this.running_ = response.running;
14633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
14646ded16be15dd865a9b21ea304d5273c8be299c87Steve Block      response.running = this.running_;
14653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return response.toJSONProtocol();
14663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } catch (e) {
14673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // Failed to generate response - return generic error.
14683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return '{"seq":' + response.seq + ',' +
14693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu              '"request_seq":' + request.seq + ',' +
14703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu              '"type":"response",' +
14713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu              '"success":false,' +
14723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu              '"message":"Internal error: ' + %ToString(e) + '"}';
14733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
14743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } catch (e) {
14753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Failed in one of the catch blocks above - most generic error.
14763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return '{"seq":0,"type":"response","success":false,"message":"Internal error"}';
14773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
14783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
14793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
14803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
14813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.continueRequest_ = function(request, response) {
14823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for arguments for continue.
14833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (request.arguments) {
14843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var count = 1;
14853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var action = Debug.StepAction.StepIn;
14863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
14873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Pull out arguments.
14883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var stepaction = request.arguments.stepaction;
14893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var stepcount = request.arguments.stepcount;
14903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
14913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Get the stepcount argument if any.
14923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (stepcount) {
14933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      count = %ToNumber(stepcount);
14943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (count < 0) {
14953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        throw new Error('Invalid stepcount argument "' + stepcount + '".');
14963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
14973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
14983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
14993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Get the stepaction argument.
15003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (stepaction) {
15013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (stepaction == 'in') {
15023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        action = Debug.StepAction.StepIn;
15033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (stepaction == 'min') {
15043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        action = Debug.StepAction.StepMin;
15053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (stepaction == 'next') {
15063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        action = Debug.StepAction.StepNext;
15073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (stepaction == 'out') {
15083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        action = Debug.StepAction.StepOut;
15093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else {
15103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        throw new Error('Invalid stepaction argument "' + stepaction + '".');
15113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
15123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
15133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
15143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Setup the VM for stepping.
15153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.exec_state_.prepareStep(action, count);
15163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
15173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
15183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // VM should be running after executing this request.
15193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.running = true;
15203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
15213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
15223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
15233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.breakRequest_ = function(request, response) {
15243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Ignore as break command does not do anything when broken.
15253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
15263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
15273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
15283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.setBreakPointRequest_ =
15293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    function(request, response) {
15303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for legal request.
15313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!request.arguments) {
15323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.failed('Missing arguments');
15333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
15343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
15353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
15363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Pull out arguments.
15373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var type = request.arguments.type;
15383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var target = request.arguments.target;
15393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var line = request.arguments.line;
15403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var column = request.arguments.column;
15413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var enabled = IS_UNDEFINED(request.arguments.enabled) ?
15423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      true : request.arguments.enabled;
15433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var condition = request.arguments.condition;
15443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var ignoreCount = request.arguments.ignoreCount;
15453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var groupId = request.arguments.groupId;
15463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
15473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for legal arguments.
15483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!type || IS_UNDEFINED(target)) {
15493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.failed('Missing argument "type" or "target"');
15503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
15513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
15523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (type != 'function' && type != 'handle' &&
15533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      type != 'script' && type != 'scriptId') {
15543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.failed('Illegal type "' + type + '"');
15553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
15563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
15573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
15583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Either function or script break point.
15593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var break_point_number;
15603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (type == 'function') {
15613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Handle function break point.
15623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (!IS_STRING(target)) {
15633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.failed('Argument "target" is not a string value');
15643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return;
15653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
15663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var f;
15673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    try {
15683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // Find the function through a global evaluate.
15693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      f = this.exec_state_.evaluateGlobal(target).value();
15703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } catch (e) {
15713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.failed('Error: "' + %ToString(e) +
15723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                      '" evaluating "' + target + '"');
15733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return;
15743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
15753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (!IS_FUNCTION(f)) {
15763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.failed('"' + target + '" does not evaluate to a function');
15773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return;
15783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
15793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
15803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Set function break point.
15813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    break_point_number = Debug.setBreakPoint(f, line, column, condition);
15823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else if (type == 'handle') {
15833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Find the object pointed by the specified handle.
15843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var handle = parseInt(target, 10);
15853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var mirror = LookupMirror(handle);
15863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (!mirror) {
15873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return response.failed('Object #' + handle + '# not found');
15883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
15893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (!mirror.isFunction()) {
15903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return response.failed('Object #' + handle + '# is not a function');
15913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
15923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
15933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Set function break point.
15943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    break_point_number = Debug.setBreakPoint(mirror.value(),
15953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                             line, column, condition);
15963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else if (type == 'script') {
15973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // set script break point.
15983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    break_point_number =
15993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        Debug.setScriptBreakPointByName(target, line, column, condition,
16003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                        groupId);
16013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {  // type == 'scriptId.
16023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    break_point_number =
16033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        Debug.setScriptBreakPointById(target, line, column, condition, groupId);
16043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
16053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Set additional break point properties.
16073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var break_point = Debug.findBreakPoint(break_point_number);
16083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (ignoreCount) {
16093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    Debug.changeBreakPointIgnoreCount(break_point_number, ignoreCount);
16103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
16113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!enabled) {
16123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    Debug.disableBreakPoint(break_point_number);
16133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
16143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Add the break point number to the response.
16163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = { type: type,
16173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                    breakpoint: break_point_number }
16183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Add break point information to the response.
16203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (break_point instanceof ScriptBreakPoint) {
16213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (break_point.type() == Debug.ScriptBreakPointType.ScriptId) {
16223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.body.type = 'scriptId';
16233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.body.script_id = break_point.script_id();
16243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } else {
16253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.body.type = 'scriptName';
16263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.body.script_name = break_point.script_name();
16273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
16283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.body.line = break_point.line();
16293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.body.column = break_point.column();
16308defd9ff6930b4e24729971a61cf7469daf119beSteve Block    response.body.actual_locations = break_point.actual_locations();
16313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
16323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.body.type = 'function';
16338defd9ff6930b4e24729971a61cf7469daf119beSteve Block    response.body.actual_locations = [break_point.actual_location];
16343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
16353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
16363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.changeBreakPointRequest_ = function(request, response) {
16393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for legal request.
16403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!request.arguments) {
16413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.failed('Missing arguments');
16423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
16433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
16443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Pull out arguments.
16463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var break_point = %ToNumber(request.arguments.breakpoint);
16473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var enabled = request.arguments.enabled;
16483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var condition = request.arguments.condition;
16493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var ignoreCount = request.arguments.ignoreCount;
16503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for legal arguments.
16523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!break_point) {
16533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.failed('Missing argument "breakpoint"');
16543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
16553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
16563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Change enabled state if supplied.
16583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_UNDEFINED(enabled)) {
16593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (enabled) {
16603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      Debug.enableBreakPoint(break_point);
16613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } else {
16623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      Debug.disableBreakPoint(break_point);
16633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
16643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
16653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Change condition if supplied
16673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_UNDEFINED(condition)) {
16683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    Debug.changeBreakPointCondition(break_point, condition);
16693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
16703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Change ignore count if supplied
16723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_UNDEFINED(ignoreCount)) {
16733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    Debug.changeBreakPointIgnoreCount(break_point, ignoreCount);
16743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
16753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
16763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.clearBreakPointGroupRequest_ = function(request, response) {
16793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for legal request.
16803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!request.arguments) {
16813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.failed('Missing arguments');
16823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
16833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
16843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Pull out arguments.
16863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var group_id = request.arguments.groupId;
16873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for legal arguments.
16893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!group_id) {
16903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.failed('Missing argument "groupId"');
16913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
16923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
16933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
16943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var cleared_break_points = [];
16953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var new_script_break_points = [];
16963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < script_break_points.length; i++) {
16973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var next_break_point = script_break_points[i];
16983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (next_break_point.groupId() == group_id) {
16993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      cleared_break_points.push(next_break_point.number());
17003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      next_break_point.clear();
17013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } else {
17023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      new_script_break_points.push(next_break_point);
17033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
17043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
17053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  script_break_points = new_script_break_points;
17063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
17073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Add the cleared break point numbers to the response.
17083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = { breakpoints: cleared_break_points };
17093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
17103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
17113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
17123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.clearBreakPointRequest_ = function(request, response) {
17133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for legal request.
17143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!request.arguments) {
17153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.failed('Missing arguments');
17163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
17173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
17183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
17193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Pull out arguments.
17203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var break_point = %ToNumber(request.arguments.breakpoint);
17213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
17223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for legal arguments.
17233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!break_point) {
17243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.failed('Missing argument "breakpoint"');
17253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
17263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
17273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
17283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Clear break point.
17293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  Debug.clearBreakPoint(break_point);
17303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
17313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Add the cleared break point number to the response.
17323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = { breakpoint: break_point }
17333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
17343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
1735bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
173625f6136652d8341ed047e7fc1a450af5bd218ea9Kristian MonsenDebugCommandProcessor.prototype.listBreakpointsRequest_ = function(request, response) {
173725f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  var array = [];
173825f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  for (var i = 0; i < script_break_points.length; i++) {
173925f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen    var break_point = script_break_points[i];
174025f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen
174125f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen    var description = {
174225f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      number: break_point.number(),
174325f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      line: break_point.line(),
174425f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      column: break_point.column(),
174525f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      groupId: break_point.groupId(),
174625f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      hit_count: break_point.hit_count(),
174725f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      active: break_point.active(),
174825f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      condition: break_point.condition(),
17498defd9ff6930b4e24729971a61cf7469daf119beSteve Block      ignoreCount: break_point.ignoreCount(),
17508defd9ff6930b4e24729971a61cf7469daf119beSteve Block      actual_locations: break_point.actual_locations()
175125f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen    }
1752bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
175325f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen    if (break_point.type() == Debug.ScriptBreakPointType.ScriptId) {
175425f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      description.type = 'scriptId';
175525f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      description.script_id = break_point.script_id();
175625f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen    } else {
175725f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      description.type = 'scriptName';
175825f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen      description.script_name = break_point.script_name();
175925f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen    }
176025f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen    array.push(description);
176125f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen  }
1762bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
1763086aeeaae12517475c22695a200be45495516549Ben Murdoch  response.body = {
1764086aeeaae12517475c22695a200be45495516549Ben Murdoch    breakpoints: array,
1765086aeeaae12517475c22695a200be45495516549Ben Murdoch    breakOnExceptions: Debug.isBreakOnException(),
1766086aeeaae12517475c22695a200be45495516549Ben Murdoch    breakOnUncaughtExceptions: Debug.isBreakOnUncaughtException()
1767086aeeaae12517475c22695a200be45495516549Ben Murdoch  }
1768086aeeaae12517475c22695a200be45495516549Ben Murdoch}
1769086aeeaae12517475c22695a200be45495516549Ben Murdoch
1770086aeeaae12517475c22695a200be45495516549Ben Murdoch
1771086aeeaae12517475c22695a200be45495516549Ben MurdochDebugCommandProcessor.prototype.disconnectRequest_ =
1772086aeeaae12517475c22695a200be45495516549Ben Murdoch    function(request, response) {
1773086aeeaae12517475c22695a200be45495516549Ben Murdoch  Debug.disableAllBreakPoints();
1774086aeeaae12517475c22695a200be45495516549Ben Murdoch  this.continueRequest_(request, response);
1775086aeeaae12517475c22695a200be45495516549Ben Murdoch}
1776086aeeaae12517475c22695a200be45495516549Ben Murdoch
1777086aeeaae12517475c22695a200be45495516549Ben Murdoch
1778086aeeaae12517475c22695a200be45495516549Ben MurdochDebugCommandProcessor.prototype.setExceptionBreakRequest_ =
1779086aeeaae12517475c22695a200be45495516549Ben Murdoch    function(request, response) {
1780086aeeaae12517475c22695a200be45495516549Ben Murdoch  // Check for legal request.
1781086aeeaae12517475c22695a200be45495516549Ben Murdoch  if (!request.arguments) {
1782086aeeaae12517475c22695a200be45495516549Ben Murdoch    response.failed('Missing arguments');
1783086aeeaae12517475c22695a200be45495516549Ben Murdoch    return;
1784086aeeaae12517475c22695a200be45495516549Ben Murdoch  }
1785086aeeaae12517475c22695a200be45495516549Ben Murdoch
1786086aeeaae12517475c22695a200be45495516549Ben Murdoch  // Pull out and check the 'type' argument:
1787086aeeaae12517475c22695a200be45495516549Ben Murdoch  var type = request.arguments.type;
1788086aeeaae12517475c22695a200be45495516549Ben Murdoch  if (!type) {
1789086aeeaae12517475c22695a200be45495516549Ben Murdoch    response.failed('Missing argument "type"');
1790086aeeaae12517475c22695a200be45495516549Ben Murdoch    return;
1791086aeeaae12517475c22695a200be45495516549Ben Murdoch  }
1792086aeeaae12517475c22695a200be45495516549Ben Murdoch
1793086aeeaae12517475c22695a200be45495516549Ben Murdoch  // Initialize the default value of enable:
1794086aeeaae12517475c22695a200be45495516549Ben Murdoch  var enabled;
1795086aeeaae12517475c22695a200be45495516549Ben Murdoch  if (type == 'all') {
1796086aeeaae12517475c22695a200be45495516549Ben Murdoch    enabled = !Debug.isBreakOnException();
1797086aeeaae12517475c22695a200be45495516549Ben Murdoch  } else if (type == 'uncaught') {
1798086aeeaae12517475c22695a200be45495516549Ben Murdoch    enabled = !Debug.isBreakOnUncaughtException();
1799086aeeaae12517475c22695a200be45495516549Ben Murdoch  }
1800086aeeaae12517475c22695a200be45495516549Ben Murdoch
1801086aeeaae12517475c22695a200be45495516549Ben Murdoch  // Pull out and check the 'enabled' argument if present:
1802086aeeaae12517475c22695a200be45495516549Ben Murdoch  if (!IS_UNDEFINED(request.arguments.enabled)) {
1803086aeeaae12517475c22695a200be45495516549Ben Murdoch    enabled = request.arguments.enabled;
1804086aeeaae12517475c22695a200be45495516549Ben Murdoch    if ((enabled != true) && (enabled != false)) {
1805086aeeaae12517475c22695a200be45495516549Ben Murdoch      response.failed('Illegal value for "enabled":"' + enabled + '"');
1806086aeeaae12517475c22695a200be45495516549Ben Murdoch    }
1807086aeeaae12517475c22695a200be45495516549Ben Murdoch  }
1808086aeeaae12517475c22695a200be45495516549Ben Murdoch
1809086aeeaae12517475c22695a200be45495516549Ben Murdoch  // Now set the exception break state:
1810086aeeaae12517475c22695a200be45495516549Ben Murdoch  if (type == 'all') {
1811086aeeaae12517475c22695a200be45495516549Ben Murdoch    %ChangeBreakOnException(Debug.ExceptionBreak.Caught, enabled);
1812086aeeaae12517475c22695a200be45495516549Ben Murdoch  } else if (type == 'uncaught') {
1813086aeeaae12517475c22695a200be45495516549Ben Murdoch    %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, enabled);
1814086aeeaae12517475c22695a200be45495516549Ben Murdoch  } else {
1815086aeeaae12517475c22695a200be45495516549Ben Murdoch    response.failed('Unknown "type":"' + type + '"');
1816086aeeaae12517475c22695a200be45495516549Ben Murdoch  }
1817086aeeaae12517475c22695a200be45495516549Ben Murdoch
1818086aeeaae12517475c22695a200be45495516549Ben Murdoch  // Add the cleared break point number to the response.
1819086aeeaae12517475c22695a200be45495516549Ben Murdoch  response.body = { 'type': type, 'enabled': enabled };
182025f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen}
182125f6136652d8341ed047e7fc1a450af5bd218ea9Kristian Monsen
18223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.backtraceRequest_ = function(request, response) {
18243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Get the number of frames.
18253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var total_frames = this.exec_state_.frameCount();
18263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Create simple response if there are no frames.
18283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (total_frames == 0) {
18293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.body = {
18303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      totalFrames: total_frames
18313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
18323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
18333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
18343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Default frame range to include in backtrace.
18363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var from_index = 0
18373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var to_index = kDefaultBacktraceLength;
18383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Get the range from the arguments.
18403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (request.arguments) {
18413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (request.arguments.fromFrame) {
18423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      from_index = request.arguments.fromFrame;
18433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
18443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (request.arguments.toFrame) {
18453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      to_index = request.arguments.toFrame;
18463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
18473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (request.arguments.bottom) {
18483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      var tmp_index = total_frames - from_index;
18493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      from_index = total_frames - to_index
18503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      to_index = tmp_index;
18513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
18523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (from_index < 0 || to_index < 0) {
18533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return response.failed('Invalid frame number');
18543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
18553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
18563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Adjust the index.
18583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  to_index = Math.min(total_frames, to_index);
18593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (to_index <= from_index) {
18613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var error = 'Invalid frame range';
18623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed(error);
18633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
18643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Create the response body.
18663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var frames = [];
18673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = from_index; i < to_index; i++) {
18683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    frames.push(this.exec_state_.frame(i));
18693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
18703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = {
18713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    fromFrame: from_index,
18723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    toFrame: to_index,
18733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    totalFrames: total_frames,
18743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    frames: frames
18753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
18763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
18773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.frameRequest_ = function(request, response) {
18803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // No frames no source.
18813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.exec_state_.frameCount() == 0) {
18823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('No frames');
18833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
18843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // With no arguments just keep the selected frame.
18863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (request.arguments) {
18873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var index = request.arguments.number;
18883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (index < 0 || this.exec_state_.frameCount() <= index) {
18893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return response.failed('Invalid frame number');
18903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
18913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    this.exec_state_.setSelectedFrame(request.arguments.number);
18933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
18943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = this.exec_state_.frame();
18953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
18963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
18983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.frameForScopeRequest_ = function(request) {
18993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Get the frame for which the scope or scopes are requested. With no frameNumber
19003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // argument use the currently selected frame.
19013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (request.arguments && !IS_UNDEFINED(request.arguments.frameNumber)) {
19023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    frame_index = request.arguments.frameNumber;
19033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (frame_index < 0 || this.exec_state_.frameCount() <= frame_index) {
19043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return response.failed('Invalid frame number');
19053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
19063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return this.exec_state_.frame(frame_index);
19073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
19083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return this.exec_state_.frame();
19093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
19103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
19113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.scopesRequest_ = function(request, response) {
19143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // No frames no scopes.
19153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.exec_state_.frameCount() == 0) {
19163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('No scopes');
19173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
19183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Get the frame for which the scopes are requested.
19203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var frame = this.frameForScopeRequest_(request);
19213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Fill all scopes for this frame.
19233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var total_scopes = frame.scopeCount();
19243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var scopes = [];
19253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < total_scopes; i++) {
19263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    scopes.push(frame.scope(i));
19273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
19283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = {
19293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    fromScope: 0,
19303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    toScope: total_scopes,
19313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    totalScopes: total_scopes,
19323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    scopes: scopes
19333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
19343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
19353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.scopeRequest_ = function(request, response) {
19383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // No frames no scopes.
19393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.exec_state_.frameCount() == 0) {
19403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('No scopes');
19413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
19423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Get the frame for which the scope is requested.
19443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var frame = this.frameForScopeRequest_(request);
19453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // With no scope argument just return top scope.
19473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var scope_index = 0;
19483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (request.arguments && !IS_UNDEFINED(request.arguments.number)) {
19493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    scope_index = %ToNumber(request.arguments.number);
19503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (scope_index < 0 || frame.scopeCount() <= scope_index) {
19513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return response.failed('Invalid scope number');
19523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
19533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
19543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = frame.scope(scope_index);
19563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
19573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.evaluateRequest_ = function(request, response) {
19603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!request.arguments) {
19613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Missing arguments');
19623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
19633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Pull out arguments.
19653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var expression = request.arguments.expression;
19663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var frame = request.arguments.frame;
19673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var global = request.arguments.global;
19683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var disable_break = request.arguments.disable_break;
1969b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  var additional_context = request.arguments.additional_context;
19703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // The expression argument could be an integer so we convert it to a
19723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // string.
19733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  try {
19743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    expression = String(expression);
19753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } catch(e) {
19763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Failed to convert expression argument to string');
19773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
19783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
19793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for legal arguments.
19803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_UNDEFINED(frame) && global) {
19813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Arguments "frame" and "global" are exclusive');
19823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
1983b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch
1984b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  var additional_context_object;
1985b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (additional_context) {
1986b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    additional_context_object = {};
1987b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    for (var i = 0; i < additional_context.length; i++) {
1988b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      var mapping = additional_context[i];
1989b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      if (!IS_STRING(mapping.name) || !IS_NUMBER(mapping.handle)) {
1990b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        return response.failed("Context element #" + i +
1991b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch            " must contain name:string and handle:number");
1992b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      }
1993b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      var context_value_mirror = LookupMirror(mapping.handle);
1994b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      if (!context_value_mirror) {
1995b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        return response.failed("Context object '" + mapping.name +
1996b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch            "' #" + mapping.handle + "# not found");
1997b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      }
1998b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      additional_context_object[mapping.name] = context_value_mirror.value();
1999b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    }
2000b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
20013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Global evaluate.
20033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (global) {
20043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Evaluate in the global context.
2005b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    response.body = this.exec_state_.evaluateGlobal(
2006b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        expression, Boolean(disable_break), additional_context_object);
20073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
20083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
20093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Default value for disable_break is true.
20113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (IS_UNDEFINED(disable_break)) {
20123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    disable_break = true;
20133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
20143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // No frames no evaluate in frame.
20163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.exec_state_.frameCount() == 0) {
20173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('No frames');
20183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
20193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check whether a frame was specified.
20213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_UNDEFINED(frame)) {
20223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var frame_number = %ToNumber(frame);
20233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (frame_number < 0 || frame_number >= this.exec_state_.frameCount()) {
20243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return response.failed('Invalid frame "' + frame + '"');
20253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
20263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Evaluate in the specified frame.
20273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.body = this.exec_state_.frame(frame_number).evaluate(
2028b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        expression, Boolean(disable_break), additional_context_object);
20293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
20303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
20313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Evaluate in the selected frame.
20323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.body = this.exec_state_.frame().evaluate(
2033b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch        expression, Boolean(disable_break), additional_context_object);
20343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return;
20353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
20363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
20373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
2039e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen MurdochDebugCommandProcessor.prototype.getobjRequest_ = function(request, response) {
2040e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  if (!request.arguments) {
2041e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    return response.failed('Missing arguments');
2042e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  }
2043e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2044e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  // Pull out arguments.
2045e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var obj_id = request.arguments.obj_id;
2046e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2047e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  // Check for legal arguments.
2048e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  if (IS_UNDEFINED(obj_id)) {
2049e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    return response.failed('Argument "obj_id" missing');
2050e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  }
2051e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2052e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  // Dump the object.
2053e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  response.body = MakeMirror(%GetLOLObj(obj_id));
2054e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch};
2055e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2056e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
20573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.lookupRequest_ = function(request, response) {
20583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!request.arguments) {
20593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Missing arguments');
20603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
20613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Pull out arguments.
20633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var handles = request.arguments.handles;
20643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for legal arguments.
20663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (IS_UNDEFINED(handles)) {
20673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Argument "handles" missing');
20683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
20693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Set 'includeSource' option for script lookup.
20713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!IS_UNDEFINED(request.arguments.includeSource)) {
20723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    includeSource = %ToBoolean(request.arguments.includeSource);
20733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    response.setOption('includeSource', includeSource);
20743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
20753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Lookup handles.
20773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var mirrors = {};
20783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < handles.length; i++) {
20793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var handle = handles[i];
20803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var mirror = LookupMirror(handle);
20813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (!mirror) {
20823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      return response.failed('Object #' + handle + '# not found');
20833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
20843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    mirrors[handle] = mirror;
20853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
20863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = mirrors;
20873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
20883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.referencesRequest_ =
20913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    function(request, response) {
20923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!request.arguments) {
20933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Missing arguments');
20943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
20953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
20963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Pull out arguments.
20973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var type = request.arguments.type;
20983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var handle = request.arguments.handle;
20993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
21003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Check for legal arguments.
21013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (IS_UNDEFINED(type)) {
21023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Argument "type" missing');
21033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
21043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (IS_UNDEFINED(handle)) {
21053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Argument "handle" missing');
21063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
21073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (type != 'referencedBy' && type != 'constructedBy') {
21083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Invalid type "' + type + '"');
21093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
21103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
21113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Lookup handle and return objects with references the object.
21123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var mirror = LookupMirror(handle);
21133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (mirror) {
21143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (type == 'referencedBy') {
21153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.body = mirror.referencedBy();
21163100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    } else {
21173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.body = mirror.constructedBy();
21183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
21193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
21203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Object #' + handle + '# not found');
21213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
21223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
21233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
21243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
21253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.sourceRequest_ = function(request, response) {
21263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // No frames no source.
21273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (this.exec_state_.frameCount() == 0) {
21283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('No source');
21293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
21303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
21313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var from_line;
21323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var to_line;
21333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var frame = this.exec_state_.frame();
21343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (request.arguments) {
21353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Pull out arguments.
21363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    from_line = request.arguments.fromLine;
21373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    to_line = request.arguments.toLine;
21383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
21393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (!IS_UNDEFINED(request.arguments.frame)) {
21403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      var frame_number = %ToNumber(request.arguments.frame);
21413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (frame_number < 0 || frame_number >= this.exec_state_.frameCount()) {
21423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        return response.failed('Invalid frame "' + frame + '"');
21433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
21443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      frame = this.exec_state_.frame(frame_number);
21453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
21463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
21473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
21483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Get the script selected.
21493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var script = frame.func().script();
21503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!script) {
21513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('No source');
21523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
21533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
21543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Get the source slice and fill it into the response.
21553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var slice = script.sourceSlice(from_line, to_line);
21563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!slice) {
21573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Invalid line interval');
21583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
21593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = {};
21603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body.source = slice.sourceText();
21613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body.fromLine = slice.from_line;
21623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body.toLine = slice.to_line;
21633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body.fromPosition = slice.from_position;
21643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body.toPosition = slice.to_position;
21653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body.totalLines = script.lineCount();
21663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
21673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
21683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
21693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.scriptsRequest_ = function(request, response) {
21703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var types = ScriptTypeFlag(Debug.ScriptType.Normal);
21713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var includeSource = false;
21723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var idsToInclude = null;
21733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (request.arguments) {
21743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Pull out arguments.
21753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (!IS_UNDEFINED(request.arguments.types)) {
21763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      types = %ToNumber(request.arguments.types);
21773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (isNaN(types) || types < 0) {
21783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        return response.failed('Invalid types "' + request.arguments.types + '"');
21793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
21803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
21816ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
21823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (!IS_UNDEFINED(request.arguments.includeSource)) {
21833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      includeSource = %ToBoolean(request.arguments.includeSource);
21843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.setOption('includeSource', includeSource);
21853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
21866ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
21873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (IS_ARRAY(request.arguments.ids)) {
21883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      idsToInclude = {};
21893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      var ids = request.arguments.ids;
21903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      for (var i = 0; i < ids.length; i++) {
21913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        idsToInclude[ids[i]] = true;
21923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
21933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
2194086aeeaae12517475c22695a200be45495516549Ben Murdoch
2195086aeeaae12517475c22695a200be45495516549Ben Murdoch    var filterStr = null;
2196086aeeaae12517475c22695a200be45495516549Ben Murdoch    var filterNum = null;
2197086aeeaae12517475c22695a200be45495516549Ben Murdoch    if (!IS_UNDEFINED(request.arguments.filter)) {
2198086aeeaae12517475c22695a200be45495516549Ben Murdoch      var num = %ToNumber(request.arguments.filter);
2199086aeeaae12517475c22695a200be45495516549Ben Murdoch      if (!isNaN(num)) {
2200086aeeaae12517475c22695a200be45495516549Ben Murdoch        filterNum = num;
2201086aeeaae12517475c22695a200be45495516549Ben Murdoch      }
2202086aeeaae12517475c22695a200be45495516549Ben Murdoch      filterStr = request.arguments.filter;
2203086aeeaae12517475c22695a200be45495516549Ben Murdoch    }
22043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
22053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Collect all scripts in the heap.
22073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var scripts = %DebugGetLoadedScripts();
22083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = [];
22103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < scripts.length; i++) {
22123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (idsToInclude && !idsToInclude[scripts[i].id]) {
22133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      continue;
22143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
2215086aeeaae12517475c22695a200be45495516549Ben Murdoch    if (filterStr || filterNum) {
2216086aeeaae12517475c22695a200be45495516549Ben Murdoch      var script = scripts[i];
2217086aeeaae12517475c22695a200be45495516549Ben Murdoch      var found = false;
2218086aeeaae12517475c22695a200be45495516549Ben Murdoch      if (filterNum && !found) {
2219086aeeaae12517475c22695a200be45495516549Ben Murdoch        if (script.id && script.id === filterNum) {
2220086aeeaae12517475c22695a200be45495516549Ben Murdoch          found = true;
2221086aeeaae12517475c22695a200be45495516549Ben Murdoch        }
2222086aeeaae12517475c22695a200be45495516549Ben Murdoch      }
2223086aeeaae12517475c22695a200be45495516549Ben Murdoch      if (filterStr && !found) {
2224086aeeaae12517475c22695a200be45495516549Ben Murdoch        if (script.name && script.name.indexOf(filterStr) >= 0) {
2225086aeeaae12517475c22695a200be45495516549Ben Murdoch          found = true;
2226086aeeaae12517475c22695a200be45495516549Ben Murdoch        }
2227086aeeaae12517475c22695a200be45495516549Ben Murdoch      }
2228086aeeaae12517475c22695a200be45495516549Ben Murdoch      if (!found) continue;
2229086aeeaae12517475c22695a200be45495516549Ben Murdoch    }
22303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (types & ScriptTypeFlag(scripts[i].type)) {
22313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      response.body.push(MakeMirror(scripts[i]));
22323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
22333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
22343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
22353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.threadsRequest_ = function(request, response) {
22383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Get the number of threads.
22393100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var total_threads = this.exec_state_.threadCount();
22403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Get information for all threads.
22423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var threads = [];
22433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < total_threads; i++) {
22443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var details = %GetThreadDetails(this.exec_state_.break_id, i);
22453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var thread_info = { current: details[0],
22463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                        id: details[1]
22473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                      }
22483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    threads.push(thread_info);
22493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
22503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Create the response body.
22523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = {
22533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    totalThreads: total_threads,
22543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    threads: threads
22553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
22563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
22573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.suspendRequest_ = function(request, response) {
22603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.running = false;
22613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
22623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.versionRequest_ = function(request, response) {
22653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = {
22663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    V8Version: %GetV8Version()
22673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
22683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
22693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.profileRequest_ = function(request, response) {
22723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (!request.arguments) {
22733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Missing arguments');
22743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
22753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var modules = parseInt(request.arguments.modules);
22763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (isNaN(modules)) {
22773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Modules is not an integer');
22783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
2279402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  var tag = parseInt(request.arguments.tag);
2280402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  if (isNaN(tag)) {
2281402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu    tag = 0;
2282402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu  }
22833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  if (request.arguments.command == 'resume') {
2284402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu    %ProfilerResume(modules, tag);
22853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else if (request.arguments.command == 'pause') {
2286402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu    %ProfilerPause(modules, tag);
22873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  } else {
22883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    return response.failed('Unknown command');
22893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
22903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  response.body = {};
22913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
22923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
22946ded16be15dd865a9b21ea304d5273c8be299c87Steve BlockDebugCommandProcessor.prototype.changeLiveRequest_ = function(request, response) {
22956ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  if (!Debug.LiveEdit) {
22966ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    return response.failed('LiveEdit feature is not supported');
22976ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  }
22986ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  if (!request.arguments) {
22996ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    return response.failed('Missing arguments');
23006ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  }
23016ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  var script_id = request.arguments.script_id;
23028defd9ff6930b4e24729971a61cf7469daf119beSteve Block  var preview_only = !!request.arguments.preview_only;
2303f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
23046ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  var scripts = %DebugGetLoadedScripts();
23056ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
23066ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  var the_script = null;
23076ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  for (var i = 0; i < scripts.length; i++) {
23086ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    if (scripts[i].id == script_id) {
23096ded16be15dd865a9b21ea304d5273c8be299c87Steve Block      the_script = scripts[i];
23106ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    }
23116ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  }
23126ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  if (!the_script) {
23136ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    response.failed('Script not found');
23146ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    return;
23156ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  }
23166ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
23176ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  var change_log = new Array();
2318bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
23196ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  if (!IS_STRING(request.arguments.new_source)) {
23206ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    throw "new_source argument expected";
23216ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  }
23226ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
23236ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  var new_source = request.arguments.new_source;
2324f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
23258defd9ff6930b4e24729971a61cf7469daf119beSteve Block  var result_description = Debug.LiveEdit.SetScriptSource(the_script,
23268defd9ff6930b4e24729971a61cf7469daf119beSteve Block      new_source, preview_only, change_log);
23278defd9ff6930b4e24729971a61cf7469daf119beSteve Block  response.body = {change_log: change_log, result: result_description};
2328f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch
2329bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  if (!preview_only && !this.running_ && result_description.stack_modified) {
2330bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch    response.body.stepin_recommended = true;
2331bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  }
23326ded16be15dd865a9b21ea304d5273c8be299c87Steve Block};
23336ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
23346ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
2335bb769b257e753aafcbd96767abb2abc645eaa20cBen MurdochDebugCommandProcessor.prototype.debuggerFlagsRequest_ = function(request,
2336bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch                                                                 response) {
2337bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  // Check for legal request.
2338bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  if (!request.arguments) {
2339bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch    response.failed('Missing arguments');
2340bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch    return;
2341bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  }
2342bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
2343bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  // Pull out arguments.
2344bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  var flags = request.arguments.flags;
2345bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
2346bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  response.body = { flags: [] };
2347bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  if (!IS_UNDEFINED(flags)) {
2348bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch    for (var i = 0; i < flags.length; i++) {
2349bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      var name = flags[i].name;
2350bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      var debugger_flag = debugger_flags[name];
2351bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      if (!debugger_flag) {
2352bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch        continue;
2353bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      }
2354bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      if ('value' in flags[i]) {
2355bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch        debugger_flag.setValue(flags[i].value);
2356bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      }
2357bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      response.body.flags.push({ name: name, value: debugger_flag.getValue() });
2358bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch    }
2359bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  } else {
2360bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch    for (var name in debugger_flags) {
2361bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      var value = debugger_flags[name].getValue();
2362bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch      response.body.flags.push({ name: name, value: value });
2363bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch    }
2364bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch  }
2365bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch}
2366bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
2367bb769b257e753aafcbd96767abb2abc645eaa20cBen Murdoch
2368086aeeaae12517475c22695a200be45495516549Ben MurdochDebugCommandProcessor.prototype.v8FlagsRequest_ = function(request, response) {
2369086aeeaae12517475c22695a200be45495516549Ben Murdoch  var flags = request.arguments.flags;
2370086aeeaae12517475c22695a200be45495516549Ben Murdoch  if (!flags) flags = '';
2371086aeeaae12517475c22695a200be45495516549Ben Murdoch  %SetFlags(flags);
2372086aeeaae12517475c22695a200be45495516549Ben Murdoch};
2373086aeeaae12517475c22695a200be45495516549Ben Murdoch
2374086aeeaae12517475c22695a200be45495516549Ben Murdoch
2375086aeeaae12517475c22695a200be45495516549Ben MurdochDebugCommandProcessor.prototype.gcRequest_ = function(request, response) {
2376086aeeaae12517475c22695a200be45495516549Ben Murdoch  var type = request.arguments.type;
2377086aeeaae12517475c22695a200be45495516549Ben Murdoch  if (!type) type = 'all';
2378086aeeaae12517475c22695a200be45495516549Ben Murdoch
2379086aeeaae12517475c22695a200be45495516549Ben Murdoch  var before = %GetHeapUsage();
2380086aeeaae12517475c22695a200be45495516549Ben Murdoch  %CollectGarbage(type);
2381086aeeaae12517475c22695a200be45495516549Ben Murdoch  var after = %GetHeapUsage();
2382086aeeaae12517475c22695a200be45495516549Ben Murdoch
2383086aeeaae12517475c22695a200be45495516549Ben Murdoch  response.body = { "before": before, "after": after };
2384086aeeaae12517475c22695a200be45495516549Ben Murdoch};
2385086aeeaae12517475c22695a200be45495516549Ben Murdoch
2386086aeeaae12517475c22695a200be45495516549Ben Murdoch
2387e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen MurdochDebugCommandProcessor.prototype.lolCaptureRequest_ =
2388e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    function(request, response) {
2389e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  response.body = %CaptureLOL();
2390e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch};
2391e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2392e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2393e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen MurdochDebugCommandProcessor.prototype.lolDeleteRequest_ =
2394e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    function(request, response) {
2395e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var id = request.arguments.id;
2396e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var result = %DeleteLOL(id);
2397e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  if (result) {
2398e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    response.body = { id: id };
2399e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  } else {
2400e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    response.failed('Failed to delete: live object list ' + id + ' not found.');
2401e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  }
2402e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch};
2403e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2404e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2405e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen MurdochDebugCommandProcessor.prototype.lolDiffRequest_ = function(request, response) {
2406e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var id1 = request.arguments.id1;
2407e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var id2 = request.arguments.id2;
2408e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var verbose = request.arguments.verbose;
2409e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var filter = request.arguments.filter;
2410e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  if (verbose === true) {
2411e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    var start = request.arguments.start;
2412e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    var count = request.arguments.count;
2413e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    response.body = %DumpLOL(id1, id2, start, count, filter);
2414e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  } else {
2415e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    response.body = %SummarizeLOL(id1, id2, filter);
2416e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  }
2417e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch};
2418e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2419e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2420e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen MurdochDebugCommandProcessor.prototype.lolGetIdRequest_ = function(request, response) {
2421e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var address = request.arguments.address;
2422e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  response.body = {};
2423e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  response.body.id = %GetLOLObjId(address);
2424e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch};
2425e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2426e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2427e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen MurdochDebugCommandProcessor.prototype.lolInfoRequest_ = function(request, response) {
2428e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var start = request.arguments.start;
2429e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var count = request.arguments.count;
2430e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  response.body = %InfoLOL(start, count);
2431e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch};
2432e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2433e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2434e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen MurdochDebugCommandProcessor.prototype.lolResetRequest_ = function(request, response) {
2435e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  %ResetLOL();
2436e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch};
2437e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2438e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2439e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen MurdochDebugCommandProcessor.prototype.lolRetainersRequest_ =
2440e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch    function(request, response) {
2441e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var id = request.arguments.id;
2442e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var verbose = request.arguments.verbose;
2443e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var start = request.arguments.start;
2444e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var count = request.arguments.count;
2445e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var filter = request.arguments.filter;
2446e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2447e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  response.body = %GetLOLObjRetainers(id, Mirror.prototype, verbose,
2448e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch                                      start, count, filter);
2449e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch};
2450e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2451e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2452e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen MurdochDebugCommandProcessor.prototype.lolPathRequest_ = function(request, response) {
2453e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var id1 = request.arguments.id1;
2454e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var id2 = request.arguments.id2;
2455e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  response.body = {};
2456e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  response.body.path = %GetLOLPath(id1, id2, Mirror.prototype);
2457e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch};
2458e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2459e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch
2460e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen MurdochDebugCommandProcessor.prototype.lolPrintRequest_ = function(request, response) {
2461e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  var id = request.arguments.id;
2462e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  response.body = {};
2463e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch  response.body.dump = %PrintLOLObj(id);
2464e0cee9b3ed82e2391fd85d118aeaa4ea361c687dBen Murdoch};
2465086aeeaae12517475c22695a200be45495516549Ben Murdoch
2466086aeeaae12517475c22695a200be45495516549Ben Murdoch
24673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// Check whether the previously processed command caused the VM to become
24683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu// running.
24693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.isRunning = function() {
24703100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return this.running_;
24713100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
24723100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
24733100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
24743100271588b61cbc1dc472a3f2f105d2eed8497fAndrei PopescuDebugCommandProcessor.prototype.systemBreak = function(cmd, args) {
24753100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return %SystemBreak();
24763100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
24773100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
24783100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
24793100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction NumberToHex8Str(n) {
24803100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var r = "";
24813100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < 8; ++i) {
24823100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    var c = hexCharArray[n & 0x0F];  // hexCharArray is defined in uri.js
24833100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    r = c + r;
24843100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    n = n >>> 4;
24853100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
24863100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return r;
24873100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu};
24883100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
24893100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
24903100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu/**
24913100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * Convert an Object to its debugger protocol representation. The representation
24923100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * may be serilized to a JSON object using JSON.stringify().
24933100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * This implementation simply runs through all string property names, converts
24943100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * each property value to a protocol value and adds the property to the result
24953100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * object. For type "object" the function will be called recursively. Note that
24963100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * circular structures will cause infinite recursion.
24973100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * @param {Object} object The object to format as protocol object.
24983100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * @param {MirrorSerializer} mirror_serializer The serializer to use if any
24993100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu *     mirror objects are encountered.
25003100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * @return {Object} Protocol object value.
25013100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu */
25023100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction ObjectToProtocolObject_(object, mirror_serializer) {
25033100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var content = {};
25043100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var key in object) {
25053100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    // Only consider string keys.
25063100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    if (typeof key == 'string') {
25073100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // Format the value based on its type.
25083100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      var property_value_json = ValueToProtocolValue_(object[key],
25093100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu                                                      mirror_serializer);
25103100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      // Add the property if relevant.
25113100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (!IS_UNDEFINED(property_value_json)) {
25123100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        content[key] = property_value_json;
25133100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
25143100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    }
25153100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
25166ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
25173100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return content;
25183100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
25193100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
25203100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
25213100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu/**
25223100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * Convert an array to its debugger protocol representation. It will convert
25233100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * each array element to a protocol value.
25243100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * @param {Array} array The array to format as protocol array.
25253100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * @param {MirrorSerializer} mirror_serializer The serializer to use if any
25263100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu *     mirror objects are encountered.
25273100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * @return {Array} Protocol array value.
25283100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu */
25293100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction ArrayToProtocolArray_(array, mirror_serializer) {
25303100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var json = [];
25313100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  for (var i = 0; i < array.length; i++) {
25323100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    json.push(ValueToProtocolValue_(array[i], mirror_serializer));
25333100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
25343100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return json;
25353100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
25363100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
25373100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
25383100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu/**
25396ded16be15dd865a9b21ea304d5273c8be299c87Steve Block * Convert a value to its debugger protocol representation.
25403100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * @param {*} value The value to format as protocol value.
25413100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * @param {MirrorSerializer} mirror_serializer The serializer to use if any
25423100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu *     mirror objects are encountered.
25433100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu * @return {*} Protocol value.
25443100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu */
25453100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescufunction ValueToProtocolValue_(value, mirror_serializer) {
25463100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  // Format the value based on its type.
25473100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  var json;
25483100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  switch (typeof value) {
25493100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    case 'object':
25503100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      if (value instanceof Mirror) {
25513100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        json = mirror_serializer.serializeValue(value);
25523100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else if (IS_ARRAY(value)){
25533100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        json = ArrayToProtocolArray_(value, mirror_serializer);
25543100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      } else {
25553100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu        json = ObjectToProtocolObject_(value, mirror_serializer);
25563100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      }
25573100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      break;
25583100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
25593100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    case 'boolean':
25603100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    case 'string':
25613100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    case 'number':
25623100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      json = value;
25633100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      break
25643100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu
25653100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu    default:
25663100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu      json = null;
25673100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  }
25683100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu  return json;
25693100271588b61cbc1dc472a3f2f105d2eed8497fAndrei Popescu}
2570