Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2008 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 Debug = debug.Debug
     31 
     32 // Simple debug event handler which just counts the number of break points hit.
     33 var break_point_hit_count;
     34 
     35 function listener(event, exec_state, event_data, data) {
     36   if (event == Debug.DebugEvent.Break) {
     37     break_point_hit_count++;
     38   }
     39 };
     40 
     41 // Add the debug event listener.
     42 Debug.setListener(listener);
     43 
     44 // Test functions.
     45 count = 0;
     46 function f() {};
     47 function g() {h(count++)};
     48 function h(x) {var a=x; return a};
     49 
     50 
     51 // Conditional breakpoint which syntax error.
     52 break_point_hit_count = 0;
     53 bp = Debug.setBreakPoint(f, 0, 0, '{{{');
     54 f();
     55 assertEquals(0, break_point_hit_count);
     56 assertEquals(0, Debug.findBreakPoint(bp, false).hit_count());
     57 Debug.clearBreakPoint(bp);
     58 
     59 // Conditional breakpoint which evaluates to false.
     60 break_point_hit_count = 0;
     61 bp = Debug.setBreakPoint(f, 0, 0, 'false');
     62 f();
     63 assertEquals(0, break_point_hit_count);
     64 assertEquals(0, Debug.findBreakPoint(bp, false).hit_count());
     65 Debug.clearBreakPoint(bp);
     66 
     67 // Conditional breakpoint which evaluates to true.
     68 break_point_hit_count = 0;
     69 bp = Debug.setBreakPoint(f, 0, 0, 'true');
     70 f();
     71 assertEquals(1, break_point_hit_count);
     72 assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
     73 Debug.clearBreakPoint(bp);
     74 
     75 // Conditional breakpoint which different types of quotes.
     76 break_point_hit_count = 0;
     77 bp = Debug.setBreakPoint(f, 0, 0, '"a" == "a"');
     78 f();
     79 assertEquals(1, break_point_hit_count);
     80 assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
     81 Debug.clearBreakPoint(bp);
     82 break_point_hit_count = 0;
     83 bp = Debug.setBreakPoint(f, 0, 0, "'a' == 'a'");
     84 f();
     85 assertEquals(1, break_point_hit_count);
     86 assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
     87 Debug.clearBreakPoint(bp);
     88 
     89 // Changing condition.
     90 break_point_hit_count = 0;
     91 bp = Debug.setBreakPoint(f, 0, 0, '"ab".indexOf("b") > 0');
     92 f();
     93 assertEquals(1, break_point_hit_count);
     94 assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
     95 Debug.changeBreakPointCondition(bp, 'Math.sin(Math.PI/2) > 1');
     96 f();
     97 assertEquals(1, break_point_hit_count);
     98 assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
     99 Debug.changeBreakPointCondition(bp, '1==1');
    100 f();
    101 assertEquals(2, break_point_hit_count);
    102 assertEquals(2, Debug.findBreakPoint(bp, false).hit_count());
    103 Debug.clearBreakPoint(bp);
    104 
    105 // Conditional breakpoint which checks global variable.
    106 break_point_hit_count = 0;
    107 bp = Debug.setBreakPoint(f, 0, 0, 'x==1');
    108 f();
    109 assertEquals(0, break_point_hit_count);
    110 assertEquals(0, Debug.findBreakPoint(bp, false).hit_count());
    111 x=1;
    112 f();
    113 assertEquals(1, break_point_hit_count);
    114 assertEquals(1, Debug.findBreakPoint(bp, false).hit_count());
    115 Debug.clearBreakPoint(bp);
    116 
    117 // Conditional breakpoint which checks global variable.
    118 break_point_hit_count = 0;
    119 bp = Debug.setBreakPoint(g, 0, 0, 'count % 2 == 0');
    120 for (var i = 0; i < 10; i++) {
    121   g();
    122 }
    123 assertEquals(5, break_point_hit_count);
    124 assertEquals(5, Debug.findBreakPoint(bp, false).hit_count());
    125 Debug.clearBreakPoint(bp);
    126 
    127 // Conditional breakpoint which checks a parameter.
    128 break_point_hit_count = 0;
    129 bp = Debug.setBreakPoint(h, 0, 0, 'x % 2 == 0');
    130 for (var i = 0; i < 10; i++) {
    131   g();
    132 }
    133 assertEquals(5, break_point_hit_count);
    134 assertEquals(5, Debug.findBreakPoint(bp, false).hit_count());
    135 Debug.clearBreakPoint(bp);
    136 
    137 // Conditional breakpoint which checks a local variable.
    138 break_point_hit_count = 0;
    139 bp = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0');
    140 for (var i = 0; i < 10; i++) {
    141   g();
    142 }
    143 assertEquals(5, break_point_hit_count);
    144 assertEquals(5, Debug.findBreakPoint(bp, false).hit_count());
    145 Debug.clearBreakPoint(bp);
    146 
    147 // Multiple conditional breakpoint which the same condition.
    148 break_point_hit_count = 0;
    149 bp1 = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0');
    150 bp2 = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0');
    151 for (var i = 0; i < 10; i++) {
    152   g();
    153 }
    154 assertEquals(5, break_point_hit_count);
    155 assertEquals(5, Debug.findBreakPoint(bp1, false).hit_count());
    156 assertEquals(5, Debug.findBreakPoint(bp2, false).hit_count());
    157 Debug.clearBreakPoint(bp1);
    158 Debug.clearBreakPoint(bp2);
    159 
    160 // Multiple conditional breakpoint which different conditions.
    161 break_point_hit_count = 0;
    162 bp1 = Debug.setBreakPoint(h, 0, 22, 'a % 2 == 0');
    163 bp2 = Debug.setBreakPoint(h, 0, 22, '(a + 1) % 2 == 0');
    164 for (var i = 0; i < 10; i++) {
    165   g();
    166 }
    167 assertEquals(10, break_point_hit_count);
    168 assertEquals(5, Debug.findBreakPoint(bp1, false).hit_count());
    169 assertEquals(5, Debug.findBreakPoint(bp2, false).hit_count());
    170 Debug.clearBreakPoint(bp1);
    171 Debug.clearBreakPoint(bp2);
    172