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. 30function a() { b(); }; 31function b() { 32 c(true); 33}; 34 function c(x) { 35 if (x) { 36 return 1; 37 } else { 38 return 1; 39 } 40 }; 41function 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. 60Debug = debug.Debug 61 62// This is the number of comment lines above the first test function. 63var 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). 67var last_position = 14312; 68// This is the last line of entire file (note: starting at 0). 69var last_line = 351; 70// This is the last column of last line (note: starting at 0 and +2, due 71// to trailing <CR><LF>). 72var last_column = 2; 73 74// This magic number is the length or the first line comment (actually number 75// of characters before 'function a(...'. 76var comment_line_length = 1726; 77var start_a = 10 + comment_line_length; 78var start_b = 37 + comment_line_length; 79var start_c = 71 + comment_line_length; 80var start_d = 163 + comment_line_length; 81 82// The position of the first line of d(), i.e. "x = 1 ;". 83var start_code_d = start_d + 7; 84// The line # of the first line of d() (note: starting at 0). 85var start_line_d = 41; 86var line_length_d = 11; 87var num_lines_d = 15; 88 89assertEquals(start_a, Debug.sourcePosition(a)); 90assertEquals(start_b, Debug.sourcePosition(b)); 91assertEquals(start_c, Debug.sourcePosition(c)); 92assertEquals(start_d, Debug.sourcePosition(d)); 93 94var script = Debug.findScript(a); 95assertTrue(script.data === Debug.findScript(b).data); 96assertTrue(script.data === Debug.findScript(c).data); 97assertTrue(script.data === Debug.findScript(d).data); 98assertTrue(script.source === Debug.findScript(b).source); 99assertTrue(script.source === Debug.findScript(c).source); 100assertTrue(script.source === Debug.findScript(d).source); 101 102// Test that when running through source positions the position, line and 103// column progresses as expected. 104var position; 105var line; 106var column; 107for (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. 131var p = start_code_d; 132for (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. 143assertEquals(0, script.locationFromPosition(0).position); 144assertEquals(0, script.locationFromPosition(0).line); 145assertEquals(0, script.locationFromPosition(0).column); 146 147// Test second position. 148assertEquals(1, script.locationFromPosition(1).position); 149assertEquals(0, script.locationFromPosition(1).line); 150assertEquals(1, script.locationFromPosition(1).column); 151 152// Test first position in function a(). 153assertEquals(start_a, script.locationFromPosition(start_a).position); 154assertEquals(0, script.locationFromPosition(start_a).line - comment_lines); 155assertEquals(10, script.locationFromPosition(start_a).column); 156 157// Test first position in function b(). 158assertEquals(start_b, script.locationFromPosition(start_b).position); 159assertEquals(1, script.locationFromPosition(start_b).line - comment_lines); 160assertEquals(13, script.locationFromPosition(start_b).column); 161 162// Test first position in function c(). 163assertEquals(start_c, script.locationFromPosition(start_c).position); 164assertEquals(4, script.locationFromPosition(start_c).line - comment_lines); 165assertEquals(12, script.locationFromPosition(start_c).column); 166 167// Test first position in function d(). 168assertEquals(start_d, script.locationFromPosition(start_d).position); 169assertEquals(11, script.locationFromPosition(start_d).line - comment_lines); 170assertEquals(10, script.locationFromPosition(start_d).column); 171 172// Test first line. 173assertEquals(0, script.locationFromLine().position); 174assertEquals(0, script.locationFromLine().line); 175assertEquals(0, script.locationFromLine().column); 176assertEquals(0, script.locationFromLine(0).position); 177assertEquals(0, script.locationFromLine(0).line); 178assertEquals(0, script.locationFromLine(0).column); 179 180// Test first line column 1. 181assertEquals(1, script.locationFromLine(0, 1).position); 182assertEquals(0, script.locationFromLine(0, 1).line); 183assertEquals(1, script.locationFromLine(0, 1).column); 184 185// Test first line offset 1. 186assertEquals(1, script.locationFromLine(0, 0, 1).position); 187assertEquals(0, script.locationFromLine(0, 0, 1).line); 188assertEquals(1, script.locationFromLine(0, 0, 1).column); 189 190// Test offset function a(). 191assertEquals(start_a, script.locationFromLine(void 0, void 0, start_a).position); 192assertEquals(0, script.locationFromLine(void 0, void 0, start_a).line - comment_lines); 193assertEquals(10, script.locationFromLine(void 0, void 0, start_a).column); 194assertEquals(start_a, script.locationFromLine(0, void 0, start_a).position); 195assertEquals(0, script.locationFromLine(0, void 0, start_a).line - comment_lines); 196assertEquals(10, script.locationFromLine(0, void 0, start_a).column); 197assertEquals(start_a, script.locationFromLine(0, 0, start_a).position); 198assertEquals(0, script.locationFromLine(0, 0, start_a).line - comment_lines); 199assertEquals(10, script.locationFromLine(0, 0, start_a).column); 200 201// Test second line offset function a(). 202assertEquals(start_a + 14, script.locationFromLine(1, 0, start_a).position); 203assertEquals(1, script.locationFromLine(1, 0, start_a).line - comment_lines); 204assertEquals(0, script.locationFromLine(1, 0, start_a).column); 205 206// Test second line column 2 offset function a(). 207assertEquals(start_a + 14 + 2, script.locationFromLine(1, 2, start_a).position); 208assertEquals(1, script.locationFromLine(1, 2, start_a).line - comment_lines); 209assertEquals(2, script.locationFromLine(1, 2, start_a).column); 210 211// Test offset function b(). 212assertEquals(start_b, script.locationFromLine(0, 0, start_b).position); 213assertEquals(1, script.locationFromLine(0, 0, start_b).line - comment_lines); 214assertEquals(13, script.locationFromLine(0, 0, start_b).column); 215 216// Test second line offset function b(). 217assertEquals(start_b + 6, script.locationFromLine(1, 0, start_b).position); 218assertEquals(2, script.locationFromLine(1, 0, start_b).line - comment_lines); 219assertEquals(0, script.locationFromLine(1, 0, start_b).column); 220 221// Test second line column 11 offset function b(). 222assertEquals(start_b + 6 + 11, script.locationFromLine(1, 11, start_b).position); 223assertEquals(2, script.locationFromLine(1, 11, start_b).line - comment_lines); 224assertEquals(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. 228assertEquals(start_b + 6 + 12, script.locationFromLine(1, 12, start_b).position); 229assertEquals(3, script.locationFromLine(1, 12, start_b).line - comment_lines); 230assertEquals(0, script.locationFromLine(1, 12, start_b).column); 231 232// Test the Debug.findSourcePosition which wraps SourceManager. 233assertEquals(0 + start_a, Debug.findFunctionSourceLocation(a, 0, 0).position); 234assertEquals(0 + start_b, Debug.findFunctionSourceLocation(b, 0, 0).position); 235assertEquals(6 + start_b, Debug.findFunctionSourceLocation(b, 1, 0).position); 236assertEquals(8 + start_b, Debug.findFunctionSourceLocation(b, 1, 2).position); 237assertEquals(18 + start_b, Debug.findFunctionSourceLocation(b, 2, 0).position); 238assertEquals(0 + start_c, Debug.findFunctionSourceLocation(c, 0, 0).position); 239assertEquals(7 + start_c, Debug.findFunctionSourceLocation(c, 1, 0).position); 240assertEquals(21 + start_c, Debug.findFunctionSourceLocation(c, 2, 0).position); 241assertEquals(38 + start_c, Debug.findFunctionSourceLocation(c, 3, 0).position); 242assertEquals(52 + start_c, Debug.findFunctionSourceLocation(c, 4, 0).position); 243assertEquals(69 + start_c, Debug.findFunctionSourceLocation(c, 5, 0).position); 244assertEquals(76 + start_c, Debug.findFunctionSourceLocation(c, 6, 0).position); 245assertEquals(0 + start_d, Debug.findFunctionSourceLocation(d, 0, 0).position); 246assertEquals(7 + start_d, Debug.findFunctionSourceLocation(d, 1, 0).position); 247for (i = 1; i <= num_lines_d; i++) { 248 assertEquals(7 + (i * line_length_d) + start_d, Debug.findFunctionSourceLocation(d, (i + 1), 0).position); 249} 250assertEquals(175 + start_d, Debug.findFunctionSourceLocation(d, 17, 0).position); 251 252// Make sure invalid inputs work properly. 253assertEquals(0, script.locationFromPosition(-1).line); 254assertEquals(null, script.locationFromPosition(last_position + 1)); 255 256// Test last position. 257assertEquals(last_position, script.locationFromPosition(last_position).position); 258assertEquals(last_line, script.locationFromPosition(last_position).line); 259assertEquals(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 266var location; 267 268location = script.locationFromLine(1, 0, start_b); 269assertEquals(' c(true);', location.sourceText()); 270 271result = ['c', ' c', ' c(', ' c(', ' c(t'] 272for (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 278location = script.locationFromLine(1, 2, start_b); 279location.restrict(1, 0); 280assertEquals('c', location.sourceText()); 281 282location = script.locationFromLine(1, 2, start_b); 283location.restrict(2, 0); 284assertEquals('c(', location.sourceText()); 285 286location = script.locationFromLine(1, 2, start_b); 287location.restrict(2, 1); 288assertEquals(' c', location.sourceText()); 289 290location = script.locationFromLine(1, 2, start_b); 291location.restrict(2, 2); 292assertEquals(' c', location.sourceText()); 293 294location = script.locationFromLine(1, 2, start_b); 295location.restrict(2, 3); 296assertEquals(' c', location.sourceText()); 297 298location = script.locationFromLine(1, 2, start_b); 299location.restrict(3, 1); 300assertEquals(' c(', location.sourceText()); 301 302location = script.locationFromLine(1, 2, start_b); 303location.restrict(5, 0); 304assertEquals('c(tru', location.sourceText()); 305 306location = script.locationFromLine(1, 2, start_b); 307location.restrict(5, 2); 308assertEquals(' c(t', location.sourceText()); 309 310location = script.locationFromLine(1, 2, start_b); 311location.restrict(5, 4); 312assertEquals(' 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 319location = script.locationFromLine(1, 10, start_b); 320location.restrict(5, 0); 321assertEquals('rue);', location.sourceText()); 322 323location = script.locationFromLine(1, 10, start_b); 324location.restrict(7, 0); 325assertEquals('(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 332location = script.locationFromLine(1, 0, start_b); 333location.restrict(5, 0); 334assertEquals(' c(t', location.sourceText()); 335 336location = script.locationFromLine(1, 0, start_b); 337location.restrict(5, 4); 338assertEquals(' c(t', location.sourceText()); 339 340location = script.locationFromLine(1, 0, start_b); 341location.restrict(7, 0); 342assertEquals(' c(tru', location.sourceText()); 343 344location = script.locationFromLine(1, 0, start_b); 345location.restrict(7, 6); 346assertEquals(' c(tru', location.sourceText()); 347 348// Test that script.sourceLine(line) works. 349for (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