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.
30
31Debug = debug.Debug
32
33var function_z_text =
34"  function Z() {\n"
35+ "    return 'Z';\n" // Breakpoint line ( #6 )
36+ "  }\n";
37
38eval(
39"function F25() {\n"
40+ "  return 25;\n" // Breakpoint line ( #1 )
41+ "}\n"
42+ "function F26 () {\n"
43+ "  var x = 20;\n"
44+ function_z_text // function takes exactly 3 lines
45//                 // Breakpoint line ( #6 )
46//
47+ "  var y = 6;\n"
48+ "  return x + y;\n"
49+ "}\n"
50+ "function Nested() {\n"
51+ "  var a = 30;\n"
52+ "  return function F27() {\n"
53+ "    var b = 3;\n" // Breakpoint line ( #14 )
54+ "    return a - b;\n"
55+ "  }\n"
56+ "}\n"
57);
58
59
60assertEquals(25, F25());
61assertEquals(26, F26());
62
63var script = Debug.findScript(F25);
64
65assertEquals(0, Debug.scriptBreakPoints().length);
66
67Debug.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId, script.id, 1, 1, "true || false || false");
68Debug.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId, script.id, 6, 1, "true || false || false");
69Debug.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId, script.id, 14, 1, "true || false || false");
70
71assertEquals(3, Debug.scriptBreakPoints().length);
72
73var new_source = script.source.replace(function_z_text, "");
74print("new source: " + new_source);
75
76var change_log = new Array();
77var result = Debug.LiveEdit.SetScriptSource(script, new_source, false, change_log);
78print("Result: " + JSON.stringify(result) + "\n");
79print("Change log: " + JSON.stringify(change_log) + "\n");
80
81var breaks = Debug.scriptBreakPoints();
82
83// One breakpoint gets duplicated in a old version of script.
84assertTrue(breaks.length > 3 + 1);
85
86var breakpoints_in_script = 0;
87var break_position_map = {};
88for (var i = 0; i < breaks.length; i++) {
89  if (breaks[i].script_id() == script.id) {
90    break_position_map[breaks[i].line()] = true;
91    breakpoints_in_script++;
92  }
93}
94
95assertEquals(3, breakpoints_in_script);
96
97// Check 2 breakpoints. The one in deleted function should have been moved somewhere.
98assertTrue(break_position_map[1]);
99assertTrue(break_position_map[11]);
100
101// Delete all breakpoints to make this test reentrant.
102var breaks = Debug.scriptBreakPoints();
103var breaks_ids = [];
104
105for (var i = 0; i < breaks.length; i++) {
106  breaks_ids.push(breaks[i].number());
107}
108
109for (var i = 0; i < breaks_ids.length; i++) {
110  Debug.clearBreakPoint(breaks_ids[i]);
111}
112
113assertEquals(0, Debug.scriptBreakPoints().length);
114