ddf80dc51c54aeac8e68ec2b4621931d04a2de31
[platform/upstream/nodejs.git] / deps / v8 / test / mjsunit / debug-sourceinfo.js
1 // Copyright 2008 the V8 project authors. All rights reserved.\r
2 // Redistribution and use in source and binary forms, with or without\r
3 // modification, are permitted provided that the following conditions are\r
4 // met:\r
5 //\r
6 //     * Redistributions of source code must retain the above copyright\r
7 //       notice, this list of conditions and the following disclaimer.\r
8 //     * Redistributions in binary form must reproduce the above\r
9 //       copyright notice, this list of conditions and the following\r
10 //       disclaimer in the documentation and/or other materials provided\r
11 //       with the distribution.\r
12 //     * Neither the name of Google Inc. nor the names of its\r
13 //       contributors may be used to endorse or promote products derived\r
14 //       from this software without specific prior written permission.\r
15 //\r
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27 \r
28 // Flags: --expose-debug-as debug\r
29 // For this test to work this file MUST have CR LF line endings.\r
30 function a() { b(); };\r
31 function    b() {\r
32   c(true);\r
33 };\r
34   function c(x) {\r
35     if (x) {\r
36       return 1;\r
37     } else {\r
38       return 1;\r
39     }\r
40   };\r
41 function d(x) {\r
42   x = 1 ;\r
43   x = 2 ;\r
44   x = 3 ;\r
45   x = 4 ;\r
46   x = 5 ;\r
47   x = 6 ;\r
48   x = 7 ;\r
49   x = 8 ;\r
50   x = 9 ;\r
51   x = 10;\r
52   x = 11;\r
53   x = 12;\r
54   x = 13;\r
55   x = 14;\r
56   x = 15;\r
57 }\r
58 \r
59 // Get the Debug object exposed from the debug context global object.\r
60 Debug = debug.Debug\r
61 \r
62 // This is the number of comment lines above the first test function.\r
63 var comment_lines = 29;\r
64 \r
65 // This is the last position in the entire file (note: this equals\r
66 // file size of <debug-sourceinfo.js> - 1, since starting at 0).\r
67 var last_position = 14312;\r
68 // This is the last line of entire file (note: starting at 0).\r
69 var last_line = 351;\r
70 // This is the last column of last line (note: starting at 0 and +2, due\r
71 // to trailing <CR><LF>).\r
72 var last_column = 2;\r
73 \r
74 // This magic number is the length or the first line comment (actually number\r
75 // of characters before 'function a(...'.\r
76 var comment_line_length = 1726;\r
77 var start_a = 10 + comment_line_length;\r
78 var start_b = 37 + comment_line_length;\r
79 var start_c = 71 + comment_line_length;\r
80 var start_d = 163 + comment_line_length;\r
81 \r
82 // The position of the first line of d(), i.e. "x = 1 ;".\r
83 var start_code_d = start_d + 7;\r
84 // The line # of the first line of d() (note: starting at 0).\r
85 var start_line_d = 41;\r
86 var line_length_d = 11;\r
87 var num_lines_d = 15;\r
88 \r
89 assertEquals(start_a, Debug.sourcePosition(a));\r
90 assertEquals(start_b, Debug.sourcePosition(b));\r
91 assertEquals(start_c, Debug.sourcePosition(c));\r
92 assertEquals(start_d, Debug.sourcePosition(d));\r
93 \r
94 var script = Debug.findScript(a);\r
95 assertTrue(script.data === Debug.findScript(b).data);\r
96 assertTrue(script.data === Debug.findScript(c).data);\r
97 assertTrue(script.data === Debug.findScript(d).data);\r
98 assertTrue(script.source === Debug.findScript(b).source);\r
99 assertTrue(script.source === Debug.findScript(c).source);\r
100 assertTrue(script.source === Debug.findScript(d).source);\r
101 \r
102 // Test that when running through source positions the position, line and\r
103 // column progresses as expected.\r
104 var position;\r
105 var line;\r
106 var column;\r
107 for (var p = 0; p < 100; p++) {\r
108   var location = script.locationFromPosition(p);\r
109   if (p > 0) {\r
110     assertEquals(position + 1, location.position);\r
111     if (line == location.line) {\r
112       assertEquals(column + 1, location.column);\r
113     } else {\r
114       assertEquals(line + 1, location.line);\r
115       assertEquals(0, location.column);\r
116     }\r
117   } else {\r
118     assertEquals(0, location.position);\r
119     assertEquals(0, location.line);\r
120     assertEquals(0, location.column);\r
121   }\r
122 \r
123   // Remember the location.\r
124   position = location.position;\r
125   line = location.line;\r
126   column = location.column;\r
127 }\r
128 \r
129 // Every line of d() is the same length.  Verify we can loop through all\r
130 // positions and find the right line # for each.\r
131 var p = start_code_d;\r
132 for (line = 0; line < num_lines_d; line++) {\r
133   for (column = 0; column < line_length_d; column++) {\r
134     var location = script.locationFromPosition(p);\r
135     assertEquals(p, location.position);\r
136     assertEquals(start_line_d + line, location.line);\r
137     assertEquals(column, location.column);\r
138     p++;\r
139   }\r
140 }\r
141 \r
142 // Test first position.\r
143 assertEquals(0, script.locationFromPosition(0).position);\r
144 assertEquals(0, script.locationFromPosition(0).line);\r
145 assertEquals(0, script.locationFromPosition(0).column);\r
146 \r
147 // Test second position.\r
148 assertEquals(1, script.locationFromPosition(1).position);\r
149 assertEquals(0, script.locationFromPosition(1).line);\r
150 assertEquals(1, script.locationFromPosition(1).column);\r
151 \r
152 // Test first position in function a().\r
153 assertEquals(start_a, script.locationFromPosition(start_a).position);\r
154 assertEquals(0, script.locationFromPosition(start_a).line - comment_lines);\r
155 assertEquals(10, script.locationFromPosition(start_a).column);\r
156 \r
157 // Test first position in function b().\r
158 assertEquals(start_b, script.locationFromPosition(start_b).position);\r
159 assertEquals(1, script.locationFromPosition(start_b).line - comment_lines);\r
160 assertEquals(13, script.locationFromPosition(start_b).column);\r
161 \r
162 // Test first position in function c().\r
163 assertEquals(start_c, script.locationFromPosition(start_c).position);\r
164 assertEquals(4, script.locationFromPosition(start_c).line - comment_lines);\r
165 assertEquals(12, script.locationFromPosition(start_c).column);\r
166 \r
167 // Test first position in function d().\r
168 assertEquals(start_d, script.locationFromPosition(start_d).position);\r
169 assertEquals(11, script.locationFromPosition(start_d).line - comment_lines);\r
170 assertEquals(10, script.locationFromPosition(start_d).column);\r
171 \r
172 // Test first line.\r
173 assertEquals(0, script.locationFromLine().position);\r
174 assertEquals(0, script.locationFromLine().line);\r
175 assertEquals(0, script.locationFromLine().column);\r
176 assertEquals(0, script.locationFromLine(0).position);\r
177 assertEquals(0, script.locationFromLine(0).line);\r
178 assertEquals(0, script.locationFromLine(0).column);\r
179 \r
180 // Test first line column 1.\r
181 assertEquals(1, script.locationFromLine(0, 1).position);\r
182 assertEquals(0, script.locationFromLine(0, 1).line);\r
183 assertEquals(1, script.locationFromLine(0, 1).column);\r
184 \r
185 // Test first line offset 1.\r
186 assertEquals(1, script.locationFromLine(0, 0, 1).position);\r
187 assertEquals(0, script.locationFromLine(0, 0, 1).line);\r
188 assertEquals(1, script.locationFromLine(0, 0, 1).column);\r
189 \r
190 // Test offset function a().\r
191 assertEquals(start_a, script.locationFromLine(void 0, void 0, start_a).position);\r
192 assertEquals(0, script.locationFromLine(void 0, void 0, start_a).line - comment_lines);\r
193 assertEquals(10, script.locationFromLine(void 0, void 0, start_a).column);\r
194 assertEquals(start_a, script.locationFromLine(0, void 0, start_a).position);\r
195 assertEquals(0, script.locationFromLine(0, void 0, start_a).line - comment_lines);\r
196 assertEquals(10, script.locationFromLine(0, void 0, start_a).column);\r
197 assertEquals(start_a, script.locationFromLine(0, 0, start_a).position);\r
198 assertEquals(0, script.locationFromLine(0, 0, start_a).line - comment_lines);\r
199 assertEquals(10, script.locationFromLine(0, 0, start_a).column);\r
200 \r
201 // Test second line offset function a().\r
202 assertEquals(start_a + 14, script.locationFromLine(1, 0, start_a).position);\r
203 assertEquals(1, script.locationFromLine(1, 0, start_a).line - comment_lines);\r
204 assertEquals(0, script.locationFromLine(1, 0, start_a).column);\r
205 \r
206 // Test second line column 2 offset function a().\r
207 assertEquals(start_a + 14 + 2, script.locationFromLine(1, 2, start_a).position);\r
208 assertEquals(1, script.locationFromLine(1, 2, start_a).line - comment_lines);\r
209 assertEquals(2, script.locationFromLine(1, 2, start_a).column);\r
210 \r
211 // Test offset function b().\r
212 assertEquals(start_b, script.locationFromLine(0, 0, start_b).position);\r
213 assertEquals(1, script.locationFromLine(0, 0, start_b).line - comment_lines);\r
214 assertEquals(13, script.locationFromLine(0, 0, start_b).column);\r
215 \r
216 // Test second line offset function b().\r
217 assertEquals(start_b + 6, script.locationFromLine(1, 0, start_b).position);\r
218 assertEquals(2, script.locationFromLine(1, 0, start_b).line - comment_lines);\r
219 assertEquals(0, script.locationFromLine(1, 0, start_b).column);\r
220 \r
221 // Test second line column 11 offset function b().\r
222 assertEquals(start_b + 6 + 11, script.locationFromLine(1, 11, start_b).position);\r
223 assertEquals(2, script.locationFromLine(1, 11, start_b).line - comment_lines);\r
224 assertEquals(11, script.locationFromLine(1, 11, start_b).column);\r
225 \r
226 // Test second line column 12 offset function b. Second line in b is 11 long\r
227 // using column 12 wraps to next line.\r
228 assertEquals(start_b + 6 + 12, script.locationFromLine(1, 12, start_b).position);\r
229 assertEquals(3, script.locationFromLine(1, 12, start_b).line - comment_lines);\r
230 assertEquals(0, script.locationFromLine(1, 12, start_b).column);\r
231 \r
232 // Test the Debug.findSourcePosition which wraps SourceManager.\r
233 assertEquals(0 + start_a, Debug.findFunctionSourceLocation(a, 0, 0).position);\r
234 assertEquals(0 + start_b, Debug.findFunctionSourceLocation(b, 0, 0).position);\r
235 assertEquals(6 + start_b, Debug.findFunctionSourceLocation(b, 1, 0).position);\r
236 assertEquals(8 + start_b, Debug.findFunctionSourceLocation(b, 1, 2).position);\r
237 assertEquals(18 + start_b, Debug.findFunctionSourceLocation(b, 2, 0).position);\r
238 assertEquals(0 + start_c, Debug.findFunctionSourceLocation(c, 0, 0).position);\r
239 assertEquals(7 + start_c, Debug.findFunctionSourceLocation(c, 1, 0).position);\r
240 assertEquals(21 + start_c, Debug.findFunctionSourceLocation(c, 2, 0).position);\r
241 assertEquals(38 + start_c, Debug.findFunctionSourceLocation(c, 3, 0).position);\r
242 assertEquals(52 + start_c, Debug.findFunctionSourceLocation(c, 4, 0).position);\r
243 assertEquals(69 + start_c, Debug.findFunctionSourceLocation(c, 5, 0).position);\r
244 assertEquals(76 + start_c, Debug.findFunctionSourceLocation(c, 6, 0).position);\r
245 assertEquals(0 + start_d, Debug.findFunctionSourceLocation(d, 0, 0).position);\r
246 assertEquals(7 + start_d, Debug.findFunctionSourceLocation(d, 1, 0).position);\r
247 for (i = 1; i <= num_lines_d; i++) {\r
248   assertEquals(7 + (i * line_length_d) + start_d, Debug.findFunctionSourceLocation(d, (i + 1), 0).position);\r
249 }\r
250 assertEquals(175 + start_d, Debug.findFunctionSourceLocation(d, 17, 0).position);\r
251 \r
252 // Make sure invalid inputs work properly.\r
253 assertEquals(0, script.locationFromPosition(-1).line);\r
254 assertEquals(null, script.locationFromPosition(last_position + 1));\r
255 \r
256 // Test last position.\r
257 assertEquals(last_position, script.locationFromPosition(last_position).position);\r
258 assertEquals(last_line, script.locationFromPosition(last_position).line);\r
259 assertEquals(last_column, script.locationFromPosition(last_position).column);\r
260 \r
261 // Test source line and restriction. All the following tests start from line 1\r
262 // column 2 in function b, which is the call to c.\r
263 //   c(true);\r
264 //   ^\r
265 \r
266 var location;\r
267 \r
268 location = script.locationFromLine(1, 0, start_b);\r
269 assertEquals('  c(true);', location.sourceText());\r
270 \r
271 result = ['c', ' c', ' c(', '  c(', '  c(t']\r
272 for (var i = 1; i <= 5; i++) {\r
273   location = script.locationFromLine(1, 2, start_b);\r
274   location.restrict(i);\r
275   assertEquals(result[i - 1], location.sourceText());\r
276 }\r
277 \r
278 location = script.locationFromLine(1, 2, start_b);\r
279 location.restrict(1, 0);\r
280 assertEquals('c', location.sourceText());\r
281 \r
282 location = script.locationFromLine(1, 2, start_b);\r
283 location.restrict(2, 0);\r
284 assertEquals('c(', location.sourceText());\r
285 \r
286 location = script.locationFromLine(1, 2, start_b);\r
287 location.restrict(2, 1);\r
288 assertEquals(' c', location.sourceText());\r
289 \r
290 location = script.locationFromLine(1, 2, start_b);\r
291 location.restrict(2, 2);\r
292 assertEquals(' c', location.sourceText());\r
293 \r
294 location = script.locationFromLine(1, 2, start_b);\r
295 location.restrict(2, 3);\r
296 assertEquals(' c', location.sourceText());\r
297 \r
298 location = script.locationFromLine(1, 2, start_b);\r
299 location.restrict(3, 1);\r
300 assertEquals(' c(', location.sourceText());\r
301 \r
302 location = script.locationFromLine(1, 2, start_b);\r
303 location.restrict(5, 0);\r
304 assertEquals('c(tru', location.sourceText());\r
305 \r
306 location = script.locationFromLine(1, 2, start_b);\r
307 location.restrict(5, 2);\r
308 assertEquals('  c(t', location.sourceText());\r
309 \r
310 location = script.locationFromLine(1, 2, start_b);\r
311 location.restrict(5, 4);\r
312 assertEquals('  c(t', location.sourceText());\r
313 \r
314 // All the following tests start from line 1 column 10 in function b, which is\r
315 // the final character.\r
316 //   c(true);\r
317 //          ^\r
318 \r
319 location = script.locationFromLine(1, 10, start_b);\r
320 location.restrict(5, 0);\r
321 assertEquals('rue);', location.sourceText());\r
322 \r
323 location = script.locationFromLine(1, 10, start_b);\r
324 location.restrict(7, 0);\r
325 assertEquals('(true);', location.sourceText());\r
326 \r
327 // All the following tests start from line 1 column 0 in function b, which is\r
328 // the first character.\r
329 //   c(true);\r
330 //^\r
331 \r
332 location = script.locationFromLine(1, 0, start_b);\r
333 location.restrict(5, 0);\r
334 assertEquals('  c(t', location.sourceText());\r
335 \r
336 location = script.locationFromLine(1, 0, start_b);\r
337 location.restrict(5, 4);\r
338 assertEquals('  c(t', location.sourceText());\r
339 \r
340 location = script.locationFromLine(1, 0, start_b);\r
341 location.restrict(7, 0);\r
342 assertEquals('  c(tru', location.sourceText());\r
343 \r
344 location = script.locationFromLine(1, 0, start_b);\r
345 location.restrict(7, 6);\r
346 assertEquals('  c(tru', location.sourceText());\r
347 \r
348 // Test that script.sourceLine(line) works.\r
349 for (line = 0; line < num_lines_d; line++) {\r
350   var line_content_regexp = new RegExp("  x = " + (line + 1));\r
351   assertTrue(line_content_regexp.test(script.sourceLine(start_line_d + line)));\r
352 }\r