Changed a function in the internal debugger JavaScript to return the full source location instead of only the position.

Added an optional parameter to exclude/include the source line offset in source location.

Extended a message test to include a test with source offset.
Review URL: http://codereview.chromium.org/39342

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1461 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
sgjesse@chromium.org 2009-03-09 17:59:25 +00:00
parent 8eea2af615
commit 994ea00dfe
4 changed files with 69 additions and 40 deletions

View File

@ -437,10 +437,11 @@ Debug.sourcePosition = function(f) {
return %FunctionGetScriptSourcePosition(f);
};
Debug.findFunctionSourcePosition = function(func, opt_line, opt_column) {
Debug.findFunctionSourceLocation = function(func, opt_line, opt_column) {
var script = %FunctionGetScript(func);
var script_offset = %FunctionGetScriptSourcePosition(func);
return script.locationFromLine(opt_line, opt_column, script_offset).position;
return script.locationFromLine(opt_line, opt_column, script_offset);
}
@ -478,8 +479,10 @@ Debug.setBreakPoint = function(func, opt_line, opt_column, opt_condition) {
if (%FunctionIsAPIFunction(func)) {
throw new Error('Cannot set break point in native code.');
}
var source_position = this.findFunctionSourcePosition(func, opt_line, opt_column) -
this.sourcePosition(func);
// Find source position relative to start of the function
var break_position =
this.findFunctionSourceLocation(func, opt_line, opt_column).position;
var source_position = break_position - this.sourcePosition(func);
// Find the script for the function.
var script = %FunctionGetScript(func);
// Break in builtin JavaScript code is not supported.

View File

@ -181,7 +181,7 @@ function FormatMessage(message) {
function GetLineNumber(message) {
if (message.startPos == -1) return -1;
var location = message.script.locationFromPosition(message.startPos);
var location = message.script.locationFromPosition(message.startPos, true);
if (location == null) return -1;
return location.line + 1;
}
@ -190,7 +190,7 @@ function GetLineNumber(message) {
// Returns the source code line containing the given source
// position, or the empty string if the position is invalid.
function GetSourceLine(message) {
var location = message.script.locationFromPosition(message.startPos);
var location = message.script.locationFromPosition(message.startPos, true);
if (location == null) return "";
location.restrict();
return location.sourceText();
@ -230,10 +230,13 @@ function MakeError(type, args) {
/**
* Get information on a specific source position.
* @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.
*/
Script.prototype.locationFromPosition = function (position) {
Script.prototype.locationFromPosition = function (position,
include_resource_offset) {
var lineCount = this.lineCount();
var line = -1;
if (position <= this.line_ends[0]) {
@ -256,9 +259,11 @@ Script.prototype.locationFromPosition = function (position) {
var column = position - start;
// Adjust according to the offset within the resource.
line += this.line_offset;
if (line == this.line_offset) {
column += this.column_offset;
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);
@ -573,7 +578,7 @@ function UnsafeGetStackTraceLine(recv, fun, pos, isTopLevel) {
file = %FunctionGetScript(fun).data;
}
if (file) {
var location = %FunctionGetScript(fun).locationFromPosition(pos);
var location = %FunctionGetScript(fun).locationFromPosition(pos, true);
if (!isTopLevel) result += "(";
result += file;
if (location != null) {

View File

@ -5320,6 +5320,28 @@ TEST(CatchStackOverflow) {
}
static void CheckTryCatchSourceInfo(v8::Handle<v8::Script> script,
char* resource_name,
int line_offset) {
v8::HandleScope scope;
v8::TryCatch try_catch;
v8::Handle<v8::Value> result = script->Run();
CHECK(result.IsEmpty());
CHECK(try_catch.HasCaught());
v8::Handle<v8::Message> message = try_catch.Message();
CHECK(!message.IsEmpty());
CHECK_EQ(10 + line_offset, message->GetLineNumber());
CHECK_EQ(91, message->GetStartPosition());
CHECK_EQ(92, message->GetEndPosition());
CHECK_EQ(2, message->GetStartColumn());
CHECK_EQ(3, message->GetEndColumn());
v8::String::AsciiValue line(message->GetSourceLine());
CHECK_EQ(" throw 'nirk';", *line);
v8::String::AsciiValue name(message->GetScriptResourceName());
CHECK_EQ(resource_name, *name);
}
THREADED_TEST(TryCatchSourceInfo) {
v8::HandleScope scope;
LocalContext context;
@ -5337,23 +5359,22 @@ THREADED_TEST(TryCatchSourceInfo) {
"}\n"
"\n"
"Foo();\n");
v8::Handle<v8::Script> script =
v8::Script::Compile(source, v8::String::New("test.js"));
v8::TryCatch try_catch;
v8::Handle<v8::Value> result = script->Run();
CHECK(result.IsEmpty());
CHECK(try_catch.HasCaught());
v8::Handle<v8::Message> message = try_catch.Message();
CHECK(!message.IsEmpty());
CHECK_EQ(10, message->GetLineNumber());
CHECK_EQ(91, message->GetStartPosition());
CHECK_EQ(92, message->GetEndPosition());
CHECK_EQ(2, message->GetStartColumn());
CHECK_EQ(3, message->GetEndColumn());
v8::String::AsciiValue line(message->GetSourceLine());
CHECK_EQ(" throw 'nirk';", *line);
v8::String::AsciiValue name(message->GetScriptResourceName());
CHECK_EQ("test.js", *name);
char* resource_name;
v8::Handle<v8::Script> script;
resource_name = "test.js";
script = v8::Script::Compile(source, v8::String::New(resource_name));
CheckTryCatchSourceInfo(script, resource_name, 0);
resource_name = "test1.js";
v8::ScriptOrigin origin1(v8::String::New(resource_name));
script = v8::Script::Compile(source, &origin1);
CheckTryCatchSourceInfo(script, resource_name, 0);
resource_name = "test2.js";
v8::ScriptOrigin origin2(v8::String::New(resource_name), v8::Integer::New(7));
script = v8::Script::Compile(source, &origin2);
CheckTryCatchSourceInfo(script, resource_name, 7);
}

View File

@ -175,18 +175,18 @@ assertEquals(3, script.locationFromLine(1, 12, start_b).line - comment_lines);
assertEquals(0, script.locationFromLine(1, 12, start_b).column);
// Test the Debug.findSourcePosition which wraps SourceManager.
assertEquals(0 + start_a, Debug.findFunctionSourcePosition(a, 0, 0));
assertEquals(0 + start_b, Debug.findFunctionSourcePosition(b, 0, 0));
assertEquals(6 + start_b, Debug.findFunctionSourcePosition(b, 1, 0));
assertEquals(8 + start_b, Debug.findFunctionSourcePosition(b, 1, 2));
assertEquals(18 + start_b, Debug.findFunctionSourcePosition(b, 2, 0));
assertEquals(0 + start_c, Debug.findFunctionSourcePosition(c, 0, 0));
assertEquals(7 + start_c, Debug.findFunctionSourcePosition(c, 1, 0));
assertEquals(21 + start_c, Debug.findFunctionSourcePosition(c, 2, 0));
assertEquals(38 + start_c, Debug.findFunctionSourcePosition(c, 3, 0));
assertEquals(52 + start_c, Debug.findFunctionSourcePosition(c, 4, 0));
assertEquals(69 + start_c, Debug.findFunctionSourcePosition(c, 5, 0));
assertEquals(76 + start_c, Debug.findFunctionSourcePosition(c, 6, 0));
assertEquals(0 + start_a, Debug.findFunctionSourceLocation(a, 0, 0).position);
assertEquals(0 + start_b, Debug.findFunctionSourceLocation(b, 0, 0).position);
assertEquals(6 + start_b, Debug.findFunctionSourceLocation(b, 1, 0).position);
assertEquals(8 + start_b, Debug.findFunctionSourceLocation(b, 1, 2).position);
assertEquals(18 + start_b, Debug.findFunctionSourceLocation(b, 2, 0).position);
assertEquals(0 + start_c, Debug.findFunctionSourceLocation(c, 0, 0).position);
assertEquals(7 + start_c, Debug.findFunctionSourceLocation(c, 1, 0).position);
assertEquals(21 + start_c, Debug.findFunctionSourceLocation(c, 2, 0).position);
assertEquals(38 + start_c, Debug.findFunctionSourceLocation(c, 3, 0).position);
assertEquals(52 + start_c, Debug.findFunctionSourceLocation(c, 4, 0).position);
assertEquals(69 + start_c, Debug.findFunctionSourceLocation(c, 5, 0).position);
assertEquals(76 + start_c, Debug.findFunctionSourceLocation(c, 6, 0).position);
// Test source line and restriction. All the following tests start from line 1
// column 2 in function b, which is the call to c.