1// Copyright 2012 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
33
34function TestCase(test_scenario, expected_output) {
35  // Global variable, accessed from eval'd script.
36  test_output = "";
37
38  var script_text_generator = (function() {
39    var variables = { a: 1, b: 1, c: 1, d: 1, e: 1, f: 1 };
40
41    return {
42      get: function() {
43        return "(function() {\n " +
44            "  function A() {\n " +
45            "    test_output += 'a' + " + variables.a + ";\n " +
46            "    test_output += '=';\n " +
47            "    debugger;\n " +
48            "    return 'Capybara';\n " +
49            "  }\n " +
50            "  function B(p1, p2) {\n " +
51            "    test_output += 'b' + " + variables.b + ";\n " +
52            "    return A();\n " +
53            "  }\n " +
54            "  function C() {\n " +
55            "    test_output += 'c' + " + variables.c + ";\n " +
56            "    // Function call with argument adaptor is intentional.\n " +
57            "    return B();\n " +
58            "  }\n " +
59            "  function D() {\n " +
60            "    test_output += 'd' + " + variables.d + ";\n " +
61            "    // Function call with argument adaptor is intentional.\n " +
62            "    return C(1, 2);\n " +
63            "  }\n " +
64            "  function E() {\n " +
65            "    test_output += 'e' + " + variables.e + ";\n " +
66            "    return D();\n " +
67            "  }\n " +
68            "  function F() {\n " +
69            "    test_output += 'f' + " + variables.f + ";\n " +
70            "    return E();\n " +
71            "  }\n " +
72            "  return F();\n " +
73            "})\n";
74      },
75      change: function(var_name) {
76        variables[var_name]++;
77      }
78    };
79  })();
80
81  var test_fun = eval(script_text_generator.get());
82
83  var script = Debug.findScript(test_fun);
84
85  var scenario_pos = 0;
86
87  function DebuggerStatementHandler() {
88    while (true) {
89      assertTrue(scenario_pos < test_scenario.length);
90      var change_var = test_scenario[scenario_pos++];
91      if (change_var == '=') {
92        // Continue.
93        return;
94      }
95      script_text_generator.change(change_var);
96      try {
97        Debug.LiveEdit.SetScriptSource(script, script_text_generator.get(),
98            false, []);
99      } catch (e) {
100        print("LiveEdit exception: " + e);
101        throw e;
102      }
103    }
104  }
105
106  var saved_exception = null;
107
108  function listener(event, exec_state, event_data, data) {
109    if (event == Debug.DebugEvent.Break) {
110      try {
111        DebuggerStatementHandler();
112      } catch (e) {
113        saved_exception = e;
114      }
115    } else {
116      print("Other: " + event);
117    }
118  }
119
120  Debug.setListener(listener);
121  assertEquals("Capybara", test_fun());
122  Debug.setListener(null);
123
124  if (saved_exception) {
125    print("Exception: " + saved_exception);
126    assertUnreachable();
127  }
128
129  print(test_output);
130
131  assertEquals(expected_output, test_output);
132}
133
134TestCase(['='], "f1e1d1c1b1a1=");
135
136TestCase(['c', '=', '='], "f1e1d1c1b1a1=c2b1a1=");
137
138TestCase(['b', 'c', 'd', 'e', '=', '='], "f1e1d1c1b1a1=e2d2c2b2a1=");
139
140TestCase(['b', 'c', '=', 'b', 'c', 'd', 'e', '=', '='], "f1e1d1c1b1a1=c2b2a1=e2d2c3b3a1=");
141
142TestCase(['e', 'f', '=', '='], "f1e1d1c1b1a1=f2e2d1c1b1a1=");
143