1// Copyright 2013 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
30Debug = debug.Debug
31
32listenerComplete = false;
33exception = false;
34
35// var0:   init after break point, changed by debug eval.
36// const0: init before break point, changed by debug eval.
37// const1: init after break point, materialized but untouched by debug eval.
38// const2: init after break point, materialized and changed by debug eval.
39// const3: context allocated const, init before break point, changed by eval.
40function f() {
41  var var1 = 21;
42  const const3 = 3;
43
44  function g() {
45    const const0 = 0;
46    assertEquals(undefined, const1);
47    assertEquals(undefined, const2);
48    assertEquals(3, const3);
49    assertEquals(21, var1);
50
51    debugger;  // Break point.
52
53    assertEquals(30, var0);
54    // TODO(yangguo): debug evaluate should not be able to alter
55    //                stack-allocated const values
56    // assertEquals(0, const0);
57    assertEquals(undefined, const1);
58    assertEquals(undefined, const2);
59    var var0 = 20;
60    const const1 = 1;
61    const const2 = 2;
62    assertEquals(20, var0);
63    assertEquals(1, const1);
64    assertEquals(2, const2);
65  }
66
67  g();
68
69  assertEquals(31, var1);
70  assertEquals(3, const3);
71}
72
73
74function listener(event, exec_state, event_data, data) {
75  if (event != Debug.DebugEvent.Break) return;
76  try {
77    var frame = exec_state.frame(0);
78    var evaluate = function(something) {
79      return frame.evaluate(something).value()
80    }
81
82    var count = frame.localCount();
83    assertEquals(4, count);
84    var expectation = { "const0" : 0,
85                        "const1" : undefined,
86                        "const2" : undefined,
87                        "const3" : 3,
88                        "var0"   : undefined,
89                        "var1"   : 21 };
90    for (var i = 0; i < frame.localCount(); ++i) {
91      var name = frame.localName(i);
92      var value = frame.localValue(i).value();
93      assertEquals(expectation[name], value);
94    }
95
96    evaluate('const0 = 10');
97    evaluate('const2 = 12');
98    evaluate('const3 = 13');
99    evaluate('var0 = 30');
100    evaluate('var1 = 31');
101
102    // Indicate that all was processed.
103    listenerComplete = true;
104  } catch (e) {
105    exception = e;
106    print("Caught something. " + e + " " + e.stack);
107  };
108};
109
110// Run and compile before debugger is active.
111try { f(); } catch (e) { }
112
113Debug.setListener(listener);
114
115f();
116
117Debug.setListener(null);
118
119assertFalse(exception, "exception in listener")
120assertTrue(listenerComplete);
121
122