Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | function a() { b(); }; |
| 30 | function b() { |
| 31 | c(true); |
| 32 | }; |
| 33 | function c(x) { |
| 34 | if (x) { |
| 35 | return 1; |
| 36 | } else { |
| 37 | return 1; |
| 38 | } |
| 39 | }; |
| 40 | function 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. |
| 59 | Debug = debug.Debug |
| 60 | |
| 61 | // This is the number of comment lines above the first test function. |
| 62 | var 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 Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 66 | var last_position = 8126; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 67 | // This is the last line of entire file (note: starting at 0). |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 68 | var last_line = 200; |
| 69 | // This is the last column of last line (note: starting at 0). |
| 70 | var last_column = 71; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 71 | |
| 72 | // This magic number is the length or the first line comment (actually number |
| 73 | // of characters before 'function a(...'. |
| 74 | var comment_line_length = 1633; |
| 75 | var start_a = 9 + comment_line_length; |
| 76 | var start_b = 35 + comment_line_length; |
| 77 | var start_c = 66 + comment_line_length; |
| 78 | var start_d = 151 + comment_line_length; |
| 79 | |
| 80 | // The position of the first line of d(), i.e. "x = 1 ;". |
| 81 | var start_code_d = start_d + 6; |
| 82 | // The line # of the first line of d() (note: starting at 0). |
| 83 | var start_line_d = 40; |
| 84 | var line_length_d = 10; |
| 85 | var num_lines_d = 15; |
| 86 | |
| 87 | assertEquals(start_a, Debug.sourcePosition(a)); |
| 88 | assertEquals(start_b, Debug.sourcePosition(b)); |
| 89 | assertEquals(start_c, Debug.sourcePosition(c)); |
| 90 | assertEquals(start_d, Debug.sourcePosition(d)); |
| 91 | |
| 92 | var script = Debug.findScript(a); |
| 93 | assertTrue(script.data === Debug.findScript(b).data); |
| 94 | assertTrue(script.data === Debug.findScript(c).data); |
| 95 | assertTrue(script.data === Debug.findScript(d).data); |
| 96 | assertTrue(script.source === Debug.findScript(b).source); |
| 97 | assertTrue(script.source === Debug.findScript(c).source); |
| 98 | assertTrue(script.source === Debug.findScript(d).source); |
| 99 | |
| 100 | // Test that when running through source positions the position, line and |
| 101 | // column progresses as expected. |
| 102 | var position; |
| 103 | var line; |
| 104 | var column; |
| 105 | for (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. |
| 129 | var p = start_code_d; |
| 130 | for (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. |
| 141 | assertEquals(0, script.locationFromPosition(0).position); |
| 142 | assertEquals(0, script.locationFromPosition(0).line); |
| 143 | assertEquals(0, script.locationFromPosition(0).column); |
| 144 | |
| 145 | // Test second position. |
| 146 | assertEquals(1, script.locationFromPosition(1).position); |
| 147 | assertEquals(0, script.locationFromPosition(1).line); |
| 148 | assertEquals(1, script.locationFromPosition(1).column); |
| 149 | |
| 150 | // Test first position in function a(). |
| 151 | assertEquals(start_a, script.locationFromPosition(start_a).position); |
| 152 | assertEquals(0, script.locationFromPosition(start_a).line - comment_lines); |
| 153 | assertEquals(10, script.locationFromPosition(start_a).column); |
| 154 | |
| 155 | // Test first position in function b(). |
| 156 | assertEquals(start_b, script.locationFromPosition(start_b).position); |
| 157 | assertEquals(1, script.locationFromPosition(start_b).line - comment_lines); |
| 158 | assertEquals(13, script.locationFromPosition(start_b).column); |
| 159 | |
| 160 | // Test first position in function c(). |
| 161 | assertEquals(start_c, script.locationFromPosition(start_c).position); |
| 162 | assertEquals(4, script.locationFromPosition(start_c).line - comment_lines); |
| 163 | assertEquals(12, script.locationFromPosition(start_c).column); |
| 164 | |
| 165 | // Test first position in function d(). |
| 166 | assertEquals(start_d, script.locationFromPosition(start_d).position); |
| 167 | assertEquals(11, script.locationFromPosition(start_d).line - comment_lines); |
| 168 | assertEquals(10, script.locationFromPosition(start_d).column); |
| 169 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 170 | // Test the Debug.findSourcePosition which wraps SourceManager. |
| 171 | assertEquals(0 + start_a, Debug.findFunctionSourceLocation(a, 0, 0).position); |
| 172 | assertEquals(0 + start_b, Debug.findFunctionSourceLocation(b, 0, 0).position); |
| 173 | assertEquals(5 + start_b, Debug.findFunctionSourceLocation(b, 1, 0).position); |
| 174 | assertEquals(7 + start_b, Debug.findFunctionSourceLocation(b, 1, 2).position); |
| 175 | assertEquals(16 + start_b, Debug.findFunctionSourceLocation(b, 2, 0).position); |
| 176 | assertEquals(0 + start_c, Debug.findFunctionSourceLocation(c, 0, 0).position); |
| 177 | assertEquals(6 + start_c, Debug.findFunctionSourceLocation(c, 1, 0).position); |
| 178 | assertEquals(19 + start_c, Debug.findFunctionSourceLocation(c, 2, 0).position); |
| 179 | assertEquals(35 + start_c, Debug.findFunctionSourceLocation(c, 3, 0).position); |
| 180 | assertEquals(48 + start_c, Debug.findFunctionSourceLocation(c, 4, 0).position); |
| 181 | assertEquals(64 + start_c, Debug.findFunctionSourceLocation(c, 5, 0).position); |
| 182 | assertEquals(70 + start_c, Debug.findFunctionSourceLocation(c, 6, 0).position); |
| 183 | assertEquals(0 + start_d, Debug.findFunctionSourceLocation(d, 0, 0).position); |
| 184 | assertEquals(6 + start_d, Debug.findFunctionSourceLocation(d, 1, 0).position); |
| 185 | for (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 | } |
| 189 | assertEquals(158 + start_d, Debug.findFunctionSourceLocation(d, 17, 0).position); |
| 190 | |
| 191 | // Make sure invalid inputs work properly. |
| 192 | assertEquals(0, script.locationFromPosition(-1).line); |
| 193 | assertEquals(null, script.locationFromPosition(last_position + 2)); |
| 194 | |
| 195 | // Test last position. |
| 196 | assertEquals(last_position, script.locationFromPosition(last_position).position); |
| 197 | assertEquals(last_line, script.locationFromPosition(last_position).line); |
| 198 | assertEquals(last_column, script.locationFromPosition(last_position).column); |
| 199 | assertEquals(last_line + 1, |
| 200 | script.locationFromPosition(last_position + 1).line); |
| 201 | assertEquals(0, script.locationFromPosition(last_position + 1).column); |