1// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Flags: --expose-debug-as debug
29// Get the Debug object exposed from the debug context global object.
30Debug = debug.Debug
31
32// Note: the following tests only checks the debugger handling of the
33// setexceptionbreak command.  It does not test whether the debugger
34// actually breaks on exceptions or not.  That functionality is tested
35// in test-debug.cc instead.
36
37// Simple function which stores the last debug event.
38listenerComplete = false;
39exception = false;
40
41var breakpoint = -1;
42var base_request = '"seq":0,"type":"request","command":"setexceptionbreak"'
43
44function safeEval(code) {
45  try {
46    return eval('(' + code + ')');
47  } catch (e) {
48    assertEquals(void 0, e);
49    return undefined;
50  }
51}
52
53function testArguments(dcp, arguments, success, type, enabled) {
54  var request = '{' + base_request + ',"arguments":' + arguments + '}'
55  var json_response = dcp.processDebugJSONRequest(request);
56  var response = safeEval(json_response);
57  if (success) {
58    assertTrue(response.success, json_response);
59    assertEquals(response.body.type, type);
60    assertEquals(response.body.enabled, enabled);
61  } else {
62    assertFalse(response.success, json_response);
63  }
64}
65
66function listener(event, exec_state, event_data, data) {
67  try {
68  if (event == Debug.DebugEvent.Break) {
69    // Get the debug command processor.
70    var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
71
72    // Test some illegal setexceptionbreak requests.
73    var request = '{' + base_request + '}'
74    var response = safeEval(dcp.processDebugJSONRequest(request));
75    assertFalse(response.success);
76
77    testArguments(dcp, '{}', false);
78    testArguments(dcp, '{"type":0}', false);
79
80    // Test some legal setexceptionbreak requests with default.
81    // Note: by default, break on exceptions should be disabled.  Hence,
82    // the first time, we send the command with no enabled arg, the debugger
83    // should toggle it on.  The second time, it should toggle it off.
84    testArguments(dcp, '{"type":"all"}', true, "all", true);
85    testArguments(dcp, '{"type":"all"}', true, "all", false);
86    testArguments(dcp, '{"type":"uncaught"}', true, "uncaught", true);
87    testArguments(dcp, '{"type":"uncaught"}', true, "uncaught", false);
88
89    // Test some legal setexceptionbreak requests with explicit enabled arg.
90    testArguments(dcp, '{"type":"all","enabled":true}', true, "all", true);
91    testArguments(dcp, '{"type":"all","enabled":false}', true, "all", false);
92
93    testArguments(dcp, '{"type":"uncaught","enabled":true}', true,
94                  "uncaught", true);
95    testArguments(dcp, '{"type":"uncaught","enabled":false}', true,
96                  "uncaught", false);
97
98    // Indicate that all was processed.
99    listenerComplete = true;
100
101  }
102  } catch (e) {
103    exception = e
104  };
105};
106
107// Add the debug event listener.
108Debug.setListener(listener);
109
110function g() {};
111
112
113// Set a break point and call to invoke the debug event listener.
114breakpoint = Debug.setBreakPoint(g, 0, 0);
115g();
116
117// Make sure that the debug event listener vas invoked.
118assertFalse(exception, "exception in listener")
119assertTrue(listenerComplete, "listener did not run to completion");
120