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
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.
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.
28 // Flags: --expose-debug-as debug
29 function a() { b(); };
58 // Get the Debug object exposed from the debug context global object.
61 // This is the number of comment lines above the first test function.
62 var comment_lines = 28;
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 = 11337;
67 // This is the last line of entire file (note: starting at 0).
69 // This is the last column of last line (note: starting at 0 and +1, due
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;
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;
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));
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);
101 // Test that when running through source positions the position, line and
102 // column progresses as expected.
106 for (var p = 0; p < 100; p++) {
107 var location = script.locationFromPosition(p);
109 assertEquals(position + 1, location.position);
110 if (line == location.line) {
111 assertEquals(column + 1, location.column);
113 assertEquals(line + 1, location.line);
114 assertEquals(0, location.column);
117 assertEquals(0, location.position);
118 assertEquals(0, location.line);
119 assertEquals(0, location.column);
122 // Remember the location.
123 position = location.position;
124 line = location.line;
125 column = location.column;
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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, Debug.findFunctionSourceLocation(d, (i + 1), 0).position);
249 assertEquals(158 + start_d, Debug.findFunctionSourceLocation(d, 17, 0).position);
251 // Make sure invalid inputs work properly.
252 assertEquals(0, script.locationFromPosition(-1).line);
253 assertEquals(null, script.locationFromPosition(last_position + 1));
255 // Test last position.
256 assertEquals(last_position, script.locationFromPosition(last_position).position);
257 assertEquals(last_line, script.locationFromPosition(last_position).line);
258 assertEquals(last_column, script.locationFromPosition(last_position).column);
260 // Test that script.sourceLine(line) works.
263 for (line = 0; line < num_lines_d; line++) {
264 var line_content_regexp = new RegExp(" x = " + (line + 1));
265 assertTrue(line_content_regexp.test(script.sourceLine(start_line_d + line)));