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 function a() { b(); };
     30 function    b() {
     31   c(true);
     32 };
     33   function c(x) {
     34     if (x) {
     35       return 1;
     36     } else {
     37       return 1;
     38     }
     39   };
     40 function d(x) {
     41   x = 1 ;
     42   x = 2 ;
     43   x = 3 ;
     44   x = 4 ;
     45   x = 5 ;
     46   x = 6 ;
     47   x = 7 ;
     48   x = 8 ;
     49   x = 9 ;
     50   x = 10;
     51   x = 11;
     52   x = 12;
     53   x = 13;
     54   x = 14;
     55   x = 15;
     56 }
     57 
     58 // Get the Debug object exposed from the debug context global object.
     59 Debug = debug.Debug
     60 
     61 // This is the number of comment lines above the first test function.
     62 var comment_lines = 28;
     63 
     64 // This is the last position in the entire file (note: this equals
     65 // file size of <debug-sourceinfo.js> - 1, since starting at 0).
     66 var last_position = 11519;
     67 // This is the last line of entire file (note: starting at 0).
     68 var last_line = 269;
     69 // This is the last column of last line (note: starting at 0 and +1, due
     70 // to trailing <LF>).
     71 var last_column = 1;
     72 
     73 // This magic number is the length or the first line comment (actually number
     74 // of characters before 'function a(...'.
     75 var comment_line_length = 1633;
     76 var start_a = 9 + comment_line_length;
     77 var start_b = 35 + comment_line_length;
     78 var start_c = 66 + comment_line_length;
     79 var start_d = 151 + comment_line_length;
     80 
     81 // The position of the first line of d(), i.e. "x = 1 ;".
     82 var start_code_d = start_d + 6;
     83 // The line # of the first line of d() (note: starting at 0).
     84 var start_line_d = 40;
     85 var line_length_d = 10;
     86 var num_lines_d = 15;
     87 
     88 assertEquals(start_a, Debug.sourcePosition(a));
     89 assertEquals(start_b, Debug.sourcePosition(b));
     90 assertEquals(start_c, Debug.sourcePosition(c));
     91 assertEquals(start_d, Debug.sourcePosition(d));
     92 
     93 var script = Debug.findScript(a);
     94 assertTrue(script.data === Debug.findScript(b).data);
     95 assertTrue(script.data === Debug.findScript(c).data);
     96 assertTrue(script.data === Debug.findScript(d).data);
     97 assertTrue(script.source === Debug.findScript(b).source);
     98 assertTrue(script.source === Debug.findScript(c).source);
     99 assertTrue(script.source === Debug.findScript(d).source);
    100 
    101 // Test that when running through source positions the position, line and
    102 // column progresses as expected.
    103 var position;
    104 var line;
    105 var column;
    106 for (var p = 0; p < 100; p++) {
    107   var location = script.locationFromPosition(p);
    108   if (p > 0) {
    109     assertEquals(position + 1, location.position);
    110     if (line == location.line) {
    111       assertEquals(column + 1, location.column);
    112     } else {
    113       assertEquals(line + 1, location.line);
    114       assertEquals(0, location.column);
    115     }
    116   } else {
    117     assertEquals(0, location.position);
    118     assertEquals(0, location.line);
    119     assertEquals(0, location.column);
    120   }
    121 
    122   // Remember the location.
    123   position = location.position;
    124   line = location.line;
    125   column = location.column;
    126 }
    127 
    128 // Every line of d() is the same length.  Verify we can loop through all
    129 // positions and find the right line # for each.
    130 var p = start_code_d;
    131 for (line = 0; line < num_lines_d; line++) {
    132   for (column = 0; column < line_length_d; column++) {
    133     var location = script.locationFromPosition(p);
    134     assertEquals(p, location.position);
    135     assertEquals(start_line_d + line, location.line);
    136     assertEquals(column, location.column);
    137     p++;
    138   }
    139 }
    140 
    141 // Test first position.
    142 assertEquals(0, script.locationFromPosition(0).position);
    143 assertEquals(0, script.locationFromPosition(0).line);
    144 assertEquals(0, script.locationFromPosition(0).column);
    145 
    146 // Test second position.
    147 assertEquals(1, script.locationFromPosition(1).position);
    148 assertEquals(0, script.locationFromPosition(1).line);
    149 assertEquals(1, script.locationFromPosition(1).column);
    150 
    151 // Test first position in function a().
    152 assertEquals(start_a, script.locationFromPosition(start_a).position);
    153 assertEquals(0, script.locationFromPosition(start_a).line - comment_lines);
    154 assertEquals(10, script.locationFromPosition(start_a).column);
    155 
    156 // Test first position in function b().
    157 assertEquals(start_b, script.locationFromPosition(start_b).position);
    158 assertEquals(1, script.locationFromPosition(start_b).line - comment_lines);
    159 assertEquals(13, script.locationFromPosition(start_b).column);
    160 
    161 // Test first position in function c().
    162 assertEquals(start_c, script.locationFromPosition(start_c).position);
    163 assertEquals(4, script.locationFromPosition(start_c).line - comment_lines);
    164 assertEquals(12, script.locationFromPosition(start_c).column);
    165 
    166 // Test first position in function d().
    167 assertEquals(start_d, script.locationFromPosition(start_d).position);
    168 assertEquals(11, script.locationFromPosition(start_d).line - comment_lines);
    169 assertEquals(10, script.locationFromPosition(start_d).column);
    170 
    171 // Test first line.
    172 assertEquals(0, script.locationFromLine().position);
    173 assertEquals(0, script.locationFromLine().line);
    174 assertEquals(0, script.locationFromLine().column);
    175 assertEquals(0, script.locationFromLine(0).position);
    176 assertEquals(0, script.locationFromLine(0).line);
    177 assertEquals(0, script.locationFromLine(0).column);
    178 
    179 // Test first line column 1.
    180 assertEquals(1, script.locationFromLine(0, 1).position);
    181 assertEquals(0, script.locationFromLine(0, 1).line);
    182 assertEquals(1, script.locationFromLine(0, 1).column);
    183 
    184 // Test first line offset 1.
    185 assertEquals(1, script.locationFromLine(0, 0, 1).position);
    186 assertEquals(0, script.locationFromLine(0, 0, 1).line);
    187 assertEquals(1, script.locationFromLine(0, 0, 1).column);
    188 
    189 // Test offset function a().
    190 assertEquals(start_a, script.locationFromLine(void 0, void 0, start_a).position);
    191 assertEquals(0, script.locationFromLine(void 0, void 0, start_a).line - comment_lines);
    192 assertEquals(10, script.locationFromLine(void 0, void 0, start_a).column);
    193 assertEquals(start_a, script.locationFromLine(0, void 0, start_a).position);
    194 assertEquals(0, script.locationFromLine(0, void 0, start_a).line - comment_lines);
    195 assertEquals(10, script.locationFromLine(0, void 0, start_a).column);
    196 assertEquals(start_a, script.locationFromLine(0, 0, start_a).position);
    197 assertEquals(0, script.locationFromLine(0, 0, start_a).line - comment_lines);
    198 assertEquals(10, script.locationFromLine(0, 0, start_a).column);
    199 
    200 // Test second line offset function a().
    201 assertEquals(start_a + 13, script.locationFromLine(1, 0, start_a).position);
    202 assertEquals(1, script.locationFromLine(1, 0, start_a).line - comment_lines);
    203 assertEquals(0, script.locationFromLine(1, 0, start_a).column);
    204 
    205 // Test second line column 2 offset function a().
    206 assertEquals(start_a + 13 + 1, script.locationFromLine(1, 1, start_a).position);
    207 assertEquals(1, script.locationFromLine(1, 2, start_a).line - comment_lines);
    208 assertEquals(2, script.locationFromLine(1, 2, start_a).column);
    209 
    210 // Test offset function b().
    211 assertEquals(start_b, script.locationFromLine(0, 0, start_b).position);
    212 assertEquals(1, script.locationFromLine(0, 0, start_b).line - comment_lines);
    213 assertEquals(13, script.locationFromLine(0, 0, start_b).column);
    214 
    215 // Test second line offset function b().
    216 assertEquals(start_b + 5, script.locationFromLine(1, 0, start_b).position);
    217 assertEquals(2, script.locationFromLine(1, 0, start_b).line - comment_lines);
    218 assertEquals(0, script.locationFromLine(1, 0, start_b).column);
    219 
    220 // Test second line column 10 offset function b().
    221 assertEquals(start_b + 5 + 10, script.locationFromLine(1, 10, start_b).position);
    222 assertEquals(2, script.locationFromLine(1, 10, start_b).line - comment_lines);
    223 assertEquals(10, script.locationFromLine(1, 10, start_b).column);
    224 
    225 // Test second line column 11 offset function b. Second line in b is 10 long
    226 // using column 11 wraps to next line.
    227 assertEquals(start_b + 5 + 11, script.locationFromLine(1, 11, start_b).position);
    228 assertEquals(3, script.locationFromLine(1, 11, start_b).line - comment_lines);
    229 assertEquals(0, script.locationFromLine(1, 11, start_b).column);
    230 
    231 // Test the Debug.findSourcePosition which wraps SourceManager.
    232 assertEquals(0 + start_a, Debug.findFunctionSourceLocation(a, 0, 0).position);
    233 assertEquals(0 + start_b, Debug.findFunctionSourceLocation(b, 0, 0).position);
    234 assertEquals(5 + start_b, Debug.findFunctionSourceLocation(b, 1, 0).position);
    235 assertEquals(7 + start_b, Debug.findFunctionSourceLocation(b, 1, 2).position);
    236 assertEquals(16 + start_b, Debug.findFunctionSourceLocation(b, 2, 0).position);
    237 assertEquals(0 + start_c, Debug.findFunctionSourceLocation(c, 0, 0).position);
    238 assertEquals(6 + start_c, Debug.findFunctionSourceLocation(c, 1, 0).position);
    239 assertEquals(19 + start_c, Debug.findFunctionSourceLocation(c, 2, 0).position);
    240 assertEquals(35 + start_c, Debug.findFunctionSourceLocation(c, 3, 0).position);
    241 assertEquals(48 + start_c, Debug.findFunctionSourceLocation(c, 4, 0).position);
    242 assertEquals(64 + start_c, Debug.findFunctionSourceLocation(c, 5, 0).position);
    243 assertEquals(70 + start_c, Debug.findFunctionSourceLocation(c, 6, 0).position);
    244 assertEquals(0 + start_d, Debug.findFunctionSourceLocation(d, 0, 0).position);
    245 assertEquals(6 + start_d, Debug.findFunctionSourceLocation(d, 1, 0).position);
    246 for (i = 1; i <= num_lines_d; i++) {
    247   assertEquals(6 + (i * line_length_d) + start_d,
    248                Debug.findFunctionSourceLocation(d, (i + 1), 0).position);
    249 }
    250 assertEquals(158 + start_d, Debug.findFunctionSourceLocation(d, 17, 0).position);
    251 
    252 // Make sure invalid inputs work properly.
    253 assertEquals(0, script.locationFromPosition(-1).line);
    254 assertEquals(null, script.locationFromPosition(last_position + 2));
    255 
    256 // Test last position.
    257 assertEquals(last_position, script.locationFromPosition(last_position).position);
    258 assertEquals(last_line, script.locationFromPosition(last_position).line);
    259 assertEquals(last_column, script.locationFromPosition(last_position).column);
    260 assertEquals(last_line + 1,
    261              script.locationFromPosition(last_position + 1).line);
    262 assertEquals(0, script.locationFromPosition(last_position + 1).column);
    263 
    264 // Test that script.sourceLine(line) works.
    265 var location;
    266 
    267 for (line = 0; line < num_lines_d; line++) {
    268   var line_content_regexp = new RegExp("  x = " + (line + 1));
    269   assertTrue(line_content_regexp.test(script.sourceLine(start_line_d + line)));
    270 }
    271