blob: b79fb8e3eccd29b4bc17f0839b9e826c06c83d91 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
29function a() { b(); };
30function b() {
31 c(true);
32};
33 function c(x) {
34 if (x) {
35 return 1;
36 } else {
37 return 1;
38 }
39 };
40function 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.
59Debug = debug.Debug
60
61// This is the number of comment lines above the first test function.
62var 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).
Ben Murdoch61f157c2016-09-16 13:49:30 +010066var last_position = 8126;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067// This is the last line of entire file (note: starting at 0).
Ben Murdoch61f157c2016-09-16 13:49:30 +010068var last_line = 200;
69// This is the last column of last line (note: starting at 0).
70var last_column = 71;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000071
72// This magic number is the length or the first line comment (actually number
73// of characters before 'function a(...'.
74var comment_line_length = 1633;
75var start_a = 9 + comment_line_length;
76var start_b = 35 + comment_line_length;
77var start_c = 66 + comment_line_length;
78var start_d = 151 + comment_line_length;
79
80// The position of the first line of d(), i.e. "x = 1 ;".
81var start_code_d = start_d + 6;
82// The line # of the first line of d() (note: starting at 0).
83var start_line_d = 40;
84var line_length_d = 10;
85var num_lines_d = 15;
86
87assertEquals(start_a, Debug.sourcePosition(a));
88assertEquals(start_b, Debug.sourcePosition(b));
89assertEquals(start_c, Debug.sourcePosition(c));
90assertEquals(start_d, Debug.sourcePosition(d));
91
92var script = Debug.findScript(a);
93assertTrue(script.data === Debug.findScript(b).data);
94assertTrue(script.data === Debug.findScript(c).data);
95assertTrue(script.data === Debug.findScript(d).data);
96assertTrue(script.source === Debug.findScript(b).source);
97assertTrue(script.source === Debug.findScript(c).source);
98assertTrue(script.source === Debug.findScript(d).source);
99
100// Test that when running through source positions the position, line and
101// column progresses as expected.
102var position;
103var line;
104var column;
105for (var p = 0; p < 100; p++) {
106 var location = script.locationFromPosition(p);
107 if (p > 0) {
108 assertEquals(position + 1, location.position);
109 if (line == location.line) {
110 assertEquals(column + 1, location.column);
111 } else {
112 assertEquals(line + 1, location.line);
113 assertEquals(0, location.column);
114 }
115 } else {
116 assertEquals(0, location.position);
117 assertEquals(0, location.line);
118 assertEquals(0, location.column);
119 }
120
121 // Remember the location.
122 position = location.position;
123 line = location.line;
124 column = location.column;
125}
126
127// Every line of d() is the same length. Verify we can loop through all
128// positions and find the right line # for each.
129var p = start_code_d;
130for (line = 0; line < num_lines_d; line++) {
131 for (column = 0; column < line_length_d; column++) {
132 var location = script.locationFromPosition(p);
133 assertEquals(p, location.position);
134 assertEquals(start_line_d + line, location.line);
135 assertEquals(column, location.column);
136 p++;
137 }
138}
139
140// Test first position.
141assertEquals(0, script.locationFromPosition(0).position);
142assertEquals(0, script.locationFromPosition(0).line);
143assertEquals(0, script.locationFromPosition(0).column);
144
145// Test second position.
146assertEquals(1, script.locationFromPosition(1).position);
147assertEquals(0, script.locationFromPosition(1).line);
148assertEquals(1, script.locationFromPosition(1).column);
149
150// Test first position in function a().
151assertEquals(start_a, script.locationFromPosition(start_a).position);
152assertEquals(0, script.locationFromPosition(start_a).line - comment_lines);
153assertEquals(10, script.locationFromPosition(start_a).column);
154
155// Test first position in function b().
156assertEquals(start_b, script.locationFromPosition(start_b).position);
157assertEquals(1, script.locationFromPosition(start_b).line - comment_lines);
158assertEquals(13, script.locationFromPosition(start_b).column);
159
160// Test first position in function c().
161assertEquals(start_c, script.locationFromPosition(start_c).position);
162assertEquals(4, script.locationFromPosition(start_c).line - comment_lines);
163assertEquals(12, script.locationFromPosition(start_c).column);
164
165// Test first position in function d().
166assertEquals(start_d, script.locationFromPosition(start_d).position);
167assertEquals(11, script.locationFromPosition(start_d).line - comment_lines);
168assertEquals(10, script.locationFromPosition(start_d).column);
169
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170// Test the Debug.findSourcePosition which wraps SourceManager.
171assertEquals(0 + start_a, Debug.findFunctionSourceLocation(a, 0, 0).position);
172assertEquals(0 + start_b, Debug.findFunctionSourceLocation(b, 0, 0).position);
173assertEquals(5 + start_b, Debug.findFunctionSourceLocation(b, 1, 0).position);
174assertEquals(7 + start_b, Debug.findFunctionSourceLocation(b, 1, 2).position);
175assertEquals(16 + start_b, Debug.findFunctionSourceLocation(b, 2, 0).position);
176assertEquals(0 + start_c, Debug.findFunctionSourceLocation(c, 0, 0).position);
177assertEquals(6 + start_c, Debug.findFunctionSourceLocation(c, 1, 0).position);
178assertEquals(19 + start_c, Debug.findFunctionSourceLocation(c, 2, 0).position);
179assertEquals(35 + start_c, Debug.findFunctionSourceLocation(c, 3, 0).position);
180assertEquals(48 + start_c, Debug.findFunctionSourceLocation(c, 4, 0).position);
181assertEquals(64 + start_c, Debug.findFunctionSourceLocation(c, 5, 0).position);
182assertEquals(70 + start_c, Debug.findFunctionSourceLocation(c, 6, 0).position);
183assertEquals(0 + start_d, Debug.findFunctionSourceLocation(d, 0, 0).position);
184assertEquals(6 + start_d, Debug.findFunctionSourceLocation(d, 1, 0).position);
185for (i = 1; i <= num_lines_d; i++) {
186 assertEquals(6 + (i * line_length_d) + start_d,
187 Debug.findFunctionSourceLocation(d, (i + 1), 0).position);
188}
189assertEquals(158 + start_d, Debug.findFunctionSourceLocation(d, 17, 0).position);
190
191// Make sure invalid inputs work properly.
192assertEquals(0, script.locationFromPosition(-1).line);
193assertEquals(null, script.locationFromPosition(last_position + 2));
194
195// Test last position.
196assertEquals(last_position, script.locationFromPosition(last_position).position);
197assertEquals(last_line, script.locationFromPosition(last_position).line);
198assertEquals(last_column, script.locationFromPosition(last_position).column);
199assertEquals(last_line + 1,
200 script.locationFromPosition(last_position + 1).line);
201assertEquals(0, script.locationFromPosition(last_position + 1).column);