Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(377)

Side by Side Diff: src/messages.js

Issue 118371: Change locationFromPosition() and locationFromLine() to use a binary search t... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | test/mjsunit/debug-sourceinfo.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 223
224 function MakeEvalError(type, args) { 224 function MakeEvalError(type, args) {
225 return MakeGenericError($EvalError, type, args); 225 return MakeGenericError($EvalError, type, args);
226 } 226 }
227 227
228 228
229 function MakeError(type, args) { 229 function MakeError(type, args) {
230 return MakeGenericError($Error, type, args); 230 return MakeGenericError($Error, type, args);
231 } 231 }
232 232
233 /**
234 * Find a line number given a specific source position.
235 * @param {number} position The source position.
236 * @return {number} 0 if input too small, -1 if input too large,
237 else the line number.
238 */
239 Script.prototype.lineFromPosition = function(position) {
240 var lower = 0;
241 var upper = this.lineCount() - 1;
242
243 // We'll never find invalid positions so bail right away.
244 if (position > this.line_ends[upper]) {
245 return -1;
246 }
247
248 // This means we don't have to safe-guard indexing line_ends[i - 1].
249 if (position <= this.line_ends[0]) {
250 return 0;
251 }
252
253 // Binary search to find line # from position range.
254 while (upper >= 1) {
255 var i = (lower + upper) >> 1;
256
257 if (position > this.line_ends[i]) {
258 lower = i + 1;
259 } else if (position <= this.line_ends[i - 1]) {
260 upper = i - 1;
261 } else {
262 return i;
263 }
264 }
265 return -1;
266 }
233 267
234 /** 268 /**
235 * Get information on a specific source position. 269 * Get information on a specific source position.
236 * @param {number} position The source position 270 * @param {number} position The source position
237 * @param {boolean} include_resource_offset Set to true to have the resource 271 * @param {boolean} include_resource_offset Set to true to have the resource
238 * offset added to the location 272 * offset added to the location
239 * @return {SourceLocation} 273 * @return {SourceLocation}
240 * If line is negative or not in the source null is returned. 274 * If line is negative or not in the source null is returned.
241 */ 275 */
242 Script.prototype.locationFromPosition = function (position, 276 Script.prototype.locationFromPosition = function (position,
243 include_resource_offset) { 277 include_resource_offset) {
244 var lineCount = this.lineCount(); 278 var line = this.lineFromPosition(position);
245 var line = -1;
246 if (position <= this.line_ends[0]) {
247 line = 0;
248 } else {
249 for (var i = 1; i < lineCount; i++) {
250 if (this.line_ends[i - 1] < position && position <= this.line_ends[i]) {
251 line = i;
252 break;
253 }
254 }
255 }
256
257 if (line == -1) return null; 279 if (line == -1) return null;
258 280
259 // Determine start, end and column. 281 // Determine start, end and column.
260 var start = line == 0 ? 0 : this.line_ends[line - 1] + 1; 282 var start = line == 0 ? 0 : this.line_ends[line - 1] + 1;
261 var end = this.line_ends[line]; 283 var end = this.line_ends[line];
262 if (end > 0 && this.source.charAt(end - 1) == '\r') end--; 284 if (end > 0 && this.source.charAt(end - 1) == '\r') end--;
263 var column = position - start; 285 var column = position - start;
264 286
265 // Adjust according to the offset within the resource. 287 // Adjust according to the offset within the resource.
266 if (include_resource_offset) { 288 if (include_resource_offset) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 var column = opt_column || 0; 323 var column = opt_column || 0;
302 if (line == 0) { 324 if (line == 0) {
303 column -= this.column_offset 325 column -= this.column_offset
304 } 326 }
305 327
306 var offset_position = opt_offset_position || 0; 328 var offset_position = opt_offset_position || 0;
307 if (line < 0 || column < 0 || offset_position < 0) return null; 329 if (line < 0 || column < 0 || offset_position < 0) return null;
308 if (line == 0) { 330 if (line == 0) {
309 return this.locationFromPosition(offset_position + column, false); 331 return this.locationFromPosition(offset_position + column, false);
310 } else { 332 } else {
311 // Find the line where the offset position is located 333 // Find the line where the offset position is located.
312 var lineCount = this.lineCount(); 334 var offset_line = this.lineFromPosition(offset_position);
313 var offset_line; 335
314 for (var i = 0; i < lineCount; i++) { 336 if (offset_line == -1 || offset_line + line >= this.lineCount()) {
315 if (offset_position <= this.line_ends[i]) { 337 return null;
316 offset_line = i;
317 break;
318 }
319 } 338 }
320 if (offset_line + line >= lineCount) return null; 339
321 return this.locationFromPosition(this.line_ends[offset_line + line - 1] + 1 + column); // line > 0 here. 340 return this.locationFromPosition(this.line_ends[offset_line + line - 1] + 1 + column); // line > 0 here.
322 } 341 }
323 } 342 }
324 343
325 344
326 /** 345 /**
327 * Get a slice of source code from the script. The boundaries for the slice is 346 * Get a slice of source code from the script. The boundaries for the slice is
328 * specified in lines. 347 * specified in lines.
329 * @param {number} opt_from_line The first line (zero bound) in the slice. 348 * @param {number} opt_from_line The first line (zero bound) in the slice.
330 * Default is 0 349 * Default is 0
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 return this.name + ": " + FormatMessage({ type: type, args: this.arguments } ); 700 return this.name + ": " + FormatMessage({ type: type, args: this.arguments } );
682 } 701 }
683 var message = this.message; 702 var message = this.message;
684 return this.name + (message ? (": " + message) : ""); 703 return this.name + (message ? (": " + message) : "");
685 }, DONT_ENUM); 704 }, DONT_ENUM);
686 705
687 706
688 // Boilerplate for exceptions for stack overflows. Used from 707 // Boilerplate for exceptions for stack overflows. Used from
689 // Top::StackOverflow(). 708 // Top::StackOverflow().
690 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []); 709 const kStackOverflowBoilerplate = MakeRangeError('stack_overflow', []);
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/debug-sourceinfo.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698