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 // For this test to work this file MUST have CR LF line endings. 30 function a() { b(); }; 31 function b() { 32 c(true); 33 }; 34 function c(x) { 35 if (x) { 36 return 1; 37 } else { 38 return 1; 39 } 40 }; 41 function d(x) { 42 x = 1 ; 43 x = 2 ; 44 x = 3 ; 45 x = 4 ; 46 x = 5 ; 47 x = 6 ; 48 x = 7 ; 49 x = 8 ; 50 x = 9 ; 51 x = 10; 52 x = 11; 53 x = 12; 54 x = 13; 55 x = 14; 56 x = 15; 57 } 58 59 // Get the Debug object exposed from the debug context global object. 60 Debug = debug.Debug 61 62 // This is the number of comment lines above the first test function. 63 var comment_lines = 29; 64 65 // This is the last position in the entire file (note: this equals 66 // file size of <debug-sourceinfo.js> - 1, since starting at 0). 67 var last_position = 14312; 68 // This is the last line of entire file (note: starting at 0). 69 var last_line = 351; 70 // This is the last column of last line (note: starting at 0 and +2, due 71 // to trailing <CR><LF>). 72 var last_column = 2; 73 74 // This magic number is the length or the first line comment (actually number 75 // of characters before 'function a(...'. 76 var comment_line_length = 1726; 77 var start_a = 10 + comment_line_length; 78 var start_b = 37 + comment_line_length; 79 var start_c = 71 + comment_line_length; 80 var start_d = 163 + comment_line_length; 81 82 // The position of the first line of d(), i.e. "x = 1 ;". 83 var start_code_d = start_d + 7; 84 // The line # of the first line of d() (note: starting at 0). 85 var start_line_d = 41; 86 var line_length_d = 11; 87 var num_lines_d = 15; 88 89 assertEquals(start_a, Debug.sourcePosition(a)); 90 assertEquals(start_b, Debug.sourcePosition(b)); 91 assertEquals(start_c, Debug.sourcePosition(c)); 92 assertEquals(start_d, Debug.sourcePosition(d)); 93 94 var script = Debug.findScript(a); 95 assertTrue(script.data === Debug.findScript(b).data); 96 assertTrue(script.data === Debug.findScript(c).data); 97 assertTrue(script.data === Debug.findScript(d).data); 98 assertTrue(script.source === Debug.findScript(b).source); 99 assertTrue(script.source === Debug.findScript(c).source); 100 assertTrue(script.source === Debug.findScript(d).source); 101 102 // Test that when running through source positions the position, line and 103 // column progresses as expected. 104 var position; 105 var line; 106 var column; 107 for (var p = 0; p < 100; p++) { 108 var location = script.locationFromPosition(p); 109 if (p > 0) { 110 assertEquals(position + 1, location.position); 111 if (line == location.line) { 112 assertEquals(column + 1, location.column); 113 } else { 114 assertEquals(line + 1, location.line); 115 assertEquals(0, location.column); 116 } 117 } else { 118 assertEquals(0, location.position); 119 assertEquals(0, location.line); 120 assertEquals(0, location.column); 121 } 122 123 // Remember the location. 124 position = location.position; 125 line = location.line; 126 column = location.column; 127 } 128 129 // Every line of d() is the same length. Verify we can loop through all 130 // positions and find the right line # for each. 131 var p = start_code_d; 132 for (line = 0; line < num_lines_d; line++) { 133 for (column = 0; column < line_length_d; column++) { 134 var location = script.locationFromPosition(p); 135 assertEquals(p, location.position); 136 assertEquals(start_line_d + line, location.line); 137 assertEquals(column, location.column); 138 p++; 139 } 140 } 141 142 // Test first position. 143 assertEquals(0, script.locationFromPosition(0).position); 144 assertEquals(0, script.locationFromPosition(0).line); 145 assertEquals(0, script.locationFromPosition(0).column); 146 147 // Test second position. 148 assertEquals(1, script.locationFromPosition(1).position); 149 assertEquals(0, script.locationFromPosition(1).line); 150 assertEquals(1, script.locationFromPosition(1).column); 151 152 // Test first position in function a(). 153 assertEquals(start_a, script.locationFromPosition(start_a).position); 154 assertEquals(0, script.locationFromPosition(start_a).line - comment_lines); 155 assertEquals(10, script.locationFromPosition(start_a).column); 156 157 // Test first position in function b(). 158 assertEquals(start_b, script.locationFromPosition(start_b).position); 159 assertEquals(1, script.locationFromPosition(start_b).line - comment_lines); 160 assertEquals(13, script.locationFromPosition(start_b).column); 161 162 // Test first position in function c(). 163 assertEquals(start_c, script.locationFromPosition(start_c).position); 164 assertEquals(4, script.locationFromPosition(start_c).line - comment_lines); 165 assertEquals(12, script.locationFromPosition(start_c).column); 166 167 // Test first position in function d(). 168 assertEquals(start_d, script.locationFromPosition(start_d).position); 169 assertEquals(11, script.locationFromPosition(start_d).line - comment_lines); 170 assertEquals(10, script.locationFromPosition(start_d).column); 171 172 // Test first line. 173 assertEquals(0, script.locationFromLine().position); 174 assertEquals(0, script.locationFromLine().line); 175 assertEquals(0, script.locationFromLine().column); 176 assertEquals(0, script.locationFromLine(0).position); 177 assertEquals(0, script.locationFromLine(0).line); 178 assertEquals(0, script.locationFromLine(0).column); 179 180 // Test first line column 1. 181 assertEquals(1, script.locationFromLine(0, 1).position); 182 assertEquals(0, script.locationFromLine(0, 1).line); 183 assertEquals(1, script.locationFromLine(0, 1).column); 184 185 // Test first line offset 1. 186 assertEquals(1, script.locationFromLine(0, 0, 1).position); 187 assertEquals(0, script.locationFromLine(0, 0, 1).line); 188 assertEquals(1, script.locationFromLine(0, 0, 1).column); 189 190 // Test offset function a(). 191 assertEquals(start_a, script.locationFromLine(void 0, void 0, start_a).position); 192 assertEquals(0, script.locationFromLine(void 0, void 0, start_a).line - comment_lines); 193 assertEquals(10, script.locationFromLine(void 0, void 0, start_a).column); 194 assertEquals(start_a, script.locationFromLine(0, void 0, start_a).position); 195 assertEquals(0, script.locationFromLine(0, void 0, start_a).line - comment_lines); 196 assertEquals(10, script.locationFromLine(0, void 0, start_a).column); 197 assertEquals(start_a, script.locationFromLine(0, 0, start_a).position); 198 assertEquals(0, script.locationFromLine(0, 0, start_a).line - comment_lines); 199 assertEquals(10, script.locationFromLine(0, 0, start_a).column); 200 201 // Test second line offset function a(). 202 assertEquals(start_a + 14, script.locationFromLine(1, 0, start_a).position); 203 assertEquals(1, script.locationFromLine(1, 0, start_a).line - comment_lines); 204 assertEquals(0, script.locationFromLine(1, 0, start_a).column); 205 206 // Test second line column 2 offset function a(). 207 assertEquals(start_a + 14 + 2, script.locationFromLine(1, 2, start_a).position); 208 assertEquals(1, script.locationFromLine(1, 2, start_a).line - comment_lines); 209 assertEquals(2, script.locationFromLine(1, 2, start_a).column); 210 211 // Test offset function b(). 212 assertEquals(start_b, script.locationFromLine(0, 0, start_b).position); 213 assertEquals(1, script.locationFromLine(0, 0, start_b).line - comment_lines); 214 assertEquals(13, script.locationFromLine(0, 0, start_b).column); 215 216 // Test second line offset function b(). 217 assertEquals(start_b + 6, script.locationFromLine(1, 0, start_b).position); 218 assertEquals(2, script.locationFromLine(1, 0, start_b).line - comment_lines); 219 assertEquals(0, script.locationFromLine(1, 0, start_b).column); 220 221 // Test second line column 11 offset function b(). 222 assertEquals(start_b + 6 + 11, script.locationFromLine(1, 11, start_b).position); 223 assertEquals(2, script.locationFromLine(1, 11, start_b).line - comment_lines); 224 assertEquals(11, script.locationFromLine(1, 11, start_b).column); 225 226 // Test second line column 12 offset function b. Second line in b is 11 long 227 // using column 12 wraps to next line. 228 assertEquals(start_b + 6 + 12, script.locationFromLine(1, 12, start_b).position); 229 assertEquals(3, script.locationFromLine(1, 12, start_b).line - comment_lines); 230 assertEquals(0, script.locationFromLine(1, 12, start_b).column); 231 232 // Test the Debug.findSourcePosition which wraps SourceManager. 233 assertEquals(0 + start_a, Debug.findFunctionSourceLocation(a, 0, 0).position); 234 assertEquals(0 + start_b, Debug.findFunctionSourceLocation(b, 0, 0).position); 235 assertEquals(6 + start_b, Debug.findFunctionSourceLocation(b, 1, 0).position); 236 assertEquals(8 + start_b, Debug.findFunctionSourceLocation(b, 1, 2).position); 237 assertEquals(18 + start_b, Debug.findFunctionSourceLocation(b, 2, 0).position); 238 assertEquals(0 + start_c, Debug.findFunctionSourceLocation(c, 0, 0).position); 239 assertEquals(7 + start_c, Debug.findFunctionSourceLocation(c, 1, 0).position); 240 assertEquals(21 + start_c, Debug.findFunctionSourceLocation(c, 2, 0).position); 241 assertEquals(38 + start_c, Debug.findFunctionSourceLocation(c, 3, 0).position); 242 assertEquals(52 + start_c, Debug.findFunctionSourceLocation(c, 4, 0).position); 243 assertEquals(69 + start_c, Debug.findFunctionSourceLocation(c, 5, 0).position); 244 assertEquals(76 + start_c, Debug.findFunctionSourceLocation(c, 6, 0).position); 245 assertEquals(0 + start_d, Debug.findFunctionSourceLocation(d, 0, 0).position); 246 assertEquals(7 + start_d, Debug.findFunctionSourceLocation(d, 1, 0).position); 247 for (i = 1; i <= num_lines_d; i++) { 248 assertEquals(7 + (i * line_length_d) + start_d, Debug.findFunctionSourceLocation(d, (i + 1), 0).position); 249 } 250 assertEquals(175 + 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 + 1)); 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 261 // Test source line and restriction. All the following tests start from line 1 262 // column 2 in function b, which is the call to c. 263 // c(true); 264 // ^ 265 266 var location; 267 268 location = script.locationFromLine(1, 0, start_b); 269 assertEquals(' c(true);', location.sourceText()); 270 271 result = ['c', ' c', ' c(', ' c(', ' c(t'] 272 for (var i = 1; i <= 5; i++) { 273 location = script.locationFromLine(1, 2, start_b); 274 location.restrict(i); 275 assertEquals(result[i - 1], location.sourceText()); 276 } 277 278 location = script.locationFromLine(1, 2, start_b); 279 location.restrict(1, 0); 280 assertEquals('c', location.sourceText()); 281 282 location = script.locationFromLine(1, 2, start_b); 283 location.restrict(2, 0); 284 assertEquals('c(', location.sourceText()); 285 286 location = script.locationFromLine(1, 2, start_b); 287 location.restrict(2, 1); 288 assertEquals(' c', location.sourceText()); 289 290 location = script.locationFromLine(1, 2, start_b); 291 location.restrict(2, 2); 292 assertEquals(' c', location.sourceText()); 293 294 location = script.locationFromLine(1, 2, start_b); 295 location.restrict(2, 3); 296 assertEquals(' c', location.sourceText()); 297 298 location = script.locationFromLine(1, 2, start_b); 299 location.restrict(3, 1); 300 assertEquals(' c(', location.sourceText()); 301 302 location = script.locationFromLine(1, 2, start_b); 303 location.restrict(5, 0); 304 assertEquals('c(tru', location.sourceText()); 305 306 location = script.locationFromLine(1, 2, start_b); 307 location.restrict(5, 2); 308 assertEquals(' c(t', location.sourceText()); 309 310 location = script.locationFromLine(1, 2, start_b); 311 location.restrict(5, 4); 312 assertEquals(' c(t', location.sourceText()); 313 314 // All the following tests start from line 1 column 10 in function b, which is 315 // the final character. 316 // c(true); 317 // ^ 318 319 location = script.locationFromLine(1, 10, start_b); 320 location.restrict(5, 0); 321 assertEquals('rue);', location.sourceText()); 322 323 location = script.locationFromLine(1, 10, start_b); 324 location.restrict(7, 0); 325 assertEquals('(true);', location.sourceText()); 326 327 // All the following tests start from line 1 column 0 in function b, which is 328 // the first character. 329 // c(true); 330 //^ 331 332 location = script.locationFromLine(1, 0, start_b); 333 location.restrict(5, 0); 334 assertEquals(' c(t', location.sourceText()); 335 336 location = script.locationFromLine(1, 0, start_b); 337 location.restrict(5, 4); 338 assertEquals(' c(t', location.sourceText()); 339 340 location = script.locationFromLine(1, 0, start_b); 341 location.restrict(7, 0); 342 assertEquals(' c(tru', location.sourceText()); 343 344 location = script.locationFromLine(1, 0, start_b); 345 location.restrict(7, 6); 346 assertEquals(' c(tru', location.sourceText()); 347 348 // Test that script.sourceLine(line) works. 349 for (line = 0; line < num_lines_d; line++) { 350 var line_content_regexp = new RegExp(" x = " + (line + 1)); 351 assertTrue(line_content_regexp.test(script.sourceLine(start_line_d + line))); 352 } 353