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

Unified Diff: src/js/messages.js

Issue 2002993002: Refactor script position calculation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/isolate.cc ('k') | src/objects.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/js/messages.js
diff --git a/src/js/messages.js b/src/js/messages.js
index cf8345eb037cd6489aab5f36f6bad0ba3dc16099..0d1aa29a0e894c90e3f02cc0f1da25651e4cad46 100644
--- a/src/js/messages.js
+++ b/src/js/messages.js
@@ -214,7 +214,7 @@ function GetSourceLine(message) {
var start_position = %MessageGetStartPosition(message);
var location = script.locationFromPosition(start_position, true);
if (location == null) return "";
- return location.sourceText();
+ return location.sourceText;
}
@@ -225,68 +225,27 @@ function GetSourceLine(message) {
else the line number.
*/
function ScriptLineFromPosition(position) {
- var lower = 0;
- var upper = this.lineCount() - 1;
- var line_ends = this.line_ends;
-
- // We'll never find invalid positions so bail right away.
- if (position > line_ends[upper]) {
- return -1;
- }
-
- // This means we don't have to safe-guard indexing line_ends[i - 1].
- if (position <= line_ends[0]) {
- return 0;
- }
-
- // Binary search to find line # from position range.
- while (upper >= 1) {
- var i = (lower + upper) >> 1;
-
- if (position > line_ends[i]) {
- lower = i + 1;
- } else if (position <= line_ends[i - 1]) {
- upper = i - 1;
- } else {
- return i;
- }
- }
-
- return -1;
+ var info = %ScriptPositionInfo(this, position, false);
+ return (info == null) ? -1 : info.line;
}
/**
* Get information on a specific source position.
+ * Returns an object with the following following properties:
+ * script : script object for the source
+ * line : source line number
+ * column : source column within the line
+ * position : position within the source
+ * sourceText : a string containing the current line
* @param {number} position The source position
* @param {boolean} include_resource_offset Set to true to have the resource
* offset added to the location
- * @return {SourceLocation}
- * If line is negative or not in the source null is returned.
+ * @return If line is negative or not in the source null is returned.
*/
function ScriptLocationFromPosition(position,
include_resource_offset) {
- var line = this.lineFromPosition(position);
- if (line == -1) return null;
-
- // Determine start, end and column.
- var line_ends = this.line_ends;
- var start = line == 0 ? 0 : line_ends[line - 1] + 1;
- var end = line_ends[line];
- if (end > 0 && %_StringCharAt(this.source, end - 1) === '\r') {
- end--;
- }
- var column = position - start;
-
- // Adjust according to the offset within the resource.
- if (include_resource_offset) {
- line += this.line_offset;
- if (line == this.line_offset) {
- column += this.column_offset;
- }
- }
-
- return new SourceLocation(this, position, line, column, start, end);
+ return %ScriptPositionInfo(this, position, !!include_resource_offset);
}
@@ -302,8 +261,7 @@ function ScriptLocationFromPosition(position,
* @param {number} opt_offset_position The offset from the begining of the
* source from where the line and column calculation starts.
* Default value is 0
- * @return {SourceLocation}
- * If line is negative or not in the source null is returned.
+ * @return If line is negative or not in the source null is returned.
*/
function ScriptLocationFromLine(opt_line, opt_column, opt_offset_position) {
// Default is the first line in the script. Lines in the script is relative
@@ -333,7 +291,7 @@ function ScriptLocationFromLine(opt_line, opt_column, opt_offset_position) {
}
return this.locationFromPosition(
- this.line_ends[offset_line + line - 1] + 1 + column); // line > 0 here.
+ %ScriptLineStartPosition(this, offset_line + line) + column);
}
}
@@ -367,15 +325,14 @@ function ScriptSourceSlice(opt_from_line, opt_to_line) {
return null;
}
- var line_ends = this.line_ends;
- var from_position = from_line == 0 ? 0 : line_ends[from_line - 1] + 1;
- var to_position = to_line == 0 ? 0 : line_ends[to_line - 1] + 1;
+ var from_position = %ScriptLineStartPosition(this, from_line);
+ var to_position = %ScriptLineStartPosition(this, to_line);
// Return a source slice with line numbers re-adjusted to the resource.
return new SourceSlice(this,
from_line + this.line_offset,
to_line + this.line_offset,
- from_position, to_position);
+ from_position, to_position);
}
@@ -393,9 +350,8 @@ function ScriptSourceLine(opt_line) {
}
// Return the source line.
- var line_ends = this.line_ends;
- var start = line == 0 ? 0 : line_ends[line - 1] + 1;
- var end = line_ends[line];
+ var start = %ScriptLineStartPosition(this, line);
+ var end = %ScriptLineEndPosition(this, line);
return %_Call(StringSubstring, this.source, start, end);
}
@@ -407,17 +363,7 @@ function ScriptSourceLine(opt_line) {
*/
function ScriptLineCount() {
// Return number of source lines.
- return this.line_ends.length;
-}
-
-
-/**
- * Returns the position of the nth line end.
- * @return {number}
- * Zero-based position of the nth line end in the script.
- */
-function ScriptLineEnd(n) {
- return this.line_ends[n];
+ return %ScriptLineCount(this);
}
@@ -442,7 +388,6 @@ utils.SetUpLockedPrototype(Script, [
"name",
"source_url",
"source_mapping_url",
- "line_ends",
"line_offset",
"column_offset"
], [
@@ -453,59 +398,11 @@ utils.SetUpLockedPrototype(Script, [
"sourceLine", ScriptSourceLine,
"lineCount", ScriptLineCount,
"nameOrSourceURL", ScriptNameOrSourceURL,
- "lineEnd", ScriptLineEnd
]
);
/**
- * Class for source location. A source location is a position within some
- * source with the following properties:
- * script : script object for the source
- * line : source line number
- * column : source column within the line
- * position : position within the source
- * start : position of start of source context (inclusive)
- * end : position of end of source context (not inclusive)
- * Source text for the source context is the character interval
- * [start, end[. In most cases end will point to a newline character.
- * It might point just past the final position of the source if the last
- * source line does not end with a newline character.
- * @param {Script} script The Script object for which this is a location
- * @param {number} position Source position for the location
- * @param {number} line The line number for the location
- * @param {number} column The column within the line for the location
- * @param {number} start Source position for start of source context
- * @param {number} end Source position for end of source context
- * @constructor
- */
-function SourceLocation(script, position, line, column, start, end) {
- this.script = script;
- this.position = position;
- this.line = line;
- this.column = column;
- this.start = start;
- this.end = end;
-}
-
-
-/**
- * Get the source text for a SourceLocation
- * @return {String}
- * Source text for this location.
- */
-function SourceLocationSourceText() {
- return %_Call(StringSubstring, this.script.source, this.start, this.end);
-}
-
-
-utils.SetUpLockedPrototype(SourceLocation,
- ["script", "position", "line", "column", "start", "end"],
- ["sourceText", SourceLocationSourceText]
-);
-
-
-/**
* Class for a source slice. A source slice is a part of a script source with
* the following properties:
* script : script object for the source
« no previous file with comments | « src/isolate.cc ('k') | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698