15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Copyright 2012 the V8 project authors. All rights reserved.
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Redistribution and use in source and binary forms, with or without
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// modification, are permitted provided that the following conditions are
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// met:
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//     * Redistributions of source code must retain the above copyright
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       notice, this list of conditions and the following disclaimer.
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//     * Redistributions in binary form must reproduce the above
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       copyright notice, this list of conditions and the following
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       disclaimer in the documentation and/or other materials provided
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       with the distribution.
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       contributors may be used to endorse or promote products derived
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//       from this software without specific prior written permission.
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)//
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Flags: --expose-debug-as debug
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Get the Debug object exposed from the debug context global object.
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)Debug = debug.Debug
31e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Simple debug event handler which just counts the number of break points hit.
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)var break_point_hit_count = 0;
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
3507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdochfunction listener(event, exec_state, event_data, data) {
3607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch  if (event == Debug.DebugEvent.Break) {
375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    break_point_hit_count++;
38c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)  }
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)};
405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
41// Add the debug event listener.
42Debug.setListener(listener);
43
44eval(
45  "var inner;\n" +
46  "function outer() {\n" +         // Non-trivial outer closure.
47  "  var x = 5;\n" +
48  "  function a() {\n" +
49  "    var foo = 0, y = 7;\n" +
50  "    function b() {\n" +
51  "      var bar = 0, baz = 0, z = 11;\n" +
52  "      function c() {\n" +
53  "        return x + y + z;\n" +  // Breakpoint line ( #8 )
54  "      }\n" +
55  "      inner = c;\n" +
56  "      return c();\n" +
57  "    }\n" +
58  "    return b();\n" +
59  "  }\n" +
60  "  return a();\n" +
61  "}"
62);
63
64var script = Debug.findScript(outer);
65
66// The debugger triggers compilation of inner closures.
67assertEquals(0, Debug.scriptBreakPoints().length);
68var sbp = Debug.setScriptBreakPointById(script.id, 8);
69assertEquals(1, Debug.scriptBreakPoints().length);
70
71// The compiled outer closure should behave correctly.
72assertEquals(23, outer());
73assertEquals(1, break_point_hit_count);
74
75// The compiled inner closure should behave correctly.
76assertEquals(23, inner());
77assertEquals(2, break_point_hit_count);
78
79// Remove script break point.
80assertEquals(1, Debug.scriptBreakPoints().length);
81Debug.clearBreakPoint(sbp);
82assertEquals(0, Debug.scriptBreakPoints().length);
83