Commit f516037a authored by yangguo@chromium.org's avatar yangguo@chromium.org

Enable stepping into callback passed to builtins (e.g. Array.forEach).

BUG=109564
TEST=

Review URL: https://chromiumcodereview.appspot.com/10078014

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@11399 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 913568ce
...@@ -1027,13 +1027,28 @@ function ArrayFilter(f, receiver) { ...@@ -1027,13 +1027,28 @@ function ArrayFilter(f, receiver) {
var result = new $Array(); var result = new $Array();
var accumulator = new InternalArray(); var accumulator = new InternalArray();
var accumulator_length = 0; var accumulator_length = 0;
for (var i = 0; i < length; i++) { if (%DebugCallbackSupportsStepping(f)) {
if (i in array) { for (var i = 0; i < length; i++) {
var element = array[i]; if (i in array) {
if (%_CallFunction(receiver, element, i, array, f)) { var element = array[i];
accumulator[accumulator_length++] = element; // Prepare break slots for debugger step in.
%DebugPrepareStepInIfStepping(f);
if (%_CallFunction(receiver, element, i, array, f)) {
accumulator[accumulator_length++] = element;
}
}
}
} else {
// This is a duplicate of the previous loop sans debug stepping.
for (var i = 0; i < length; i++) {
if (i in array) {
var element = array[i];
if (%_CallFunction(receiver, element, i, array, f)) {
accumulator[accumulator_length++] = element;
}
} }
} }
// End of duplicate.
} }
%MoveArrayContents(accumulator, result); %MoveArrayContents(accumulator, result);
return result; return result;
...@@ -1059,12 +1074,24 @@ function ArrayForEach(f, receiver) { ...@@ -1059,12 +1074,24 @@ function ArrayForEach(f, receiver) {
} else if (!IS_SPEC_OBJECT(receiver)) { } else if (!IS_SPEC_OBJECT(receiver)) {
receiver = ToObject(receiver); receiver = ToObject(receiver);
} }
if (%DebugCallbackSupportsStepping(f)) {
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
if (i in array) { if (i in array) {
var element = array[i]; var element = array[i];
%_CallFunction(receiver, element, i, array, f); // Prepare break slots for debugger step in.
%DebugPrepareStepInIfStepping(f);
%_CallFunction(receiver, element, i, array, f);
}
}
} else {
// This is a duplicate of the previous loop sans debug stepping.
for (var i = 0; i < length; i++) {
if (i in array) {
var element = array[i];
%_CallFunction(receiver, element, i, array, f);
}
} }
// End of duplicate.
} }
} }
...@@ -1091,11 +1118,24 @@ function ArraySome(f, receiver) { ...@@ -1091,11 +1118,24 @@ function ArraySome(f, receiver) {
receiver = ToObject(receiver); receiver = ToObject(receiver);
} }
for (var i = 0; i < length; i++) { if (%DebugCallbackSupportsStepping(f)) {
if (i in array) { for (var i = 0; i < length; i++) {
var element = array[i]; if (i in array) {
if (%_CallFunction(receiver, element, i, array, f)) return true; var element = array[i];
// Prepare break slots for debugger step in.
%DebugPrepareStepInIfStepping(f);
if (%_CallFunction(receiver, element, i, array, f)) return true;
}
}
} else {
// This is a duplicate of the previous loop sans debug stepping.
for (var i = 0; i < length; i++) {
if (i in array) {
var element = array[i];
if (%_CallFunction(receiver, element, i, array, f)) return true;
}
} }
// End of duplicate.
} }
return false; return false;
} }
...@@ -1121,11 +1161,24 @@ function ArrayEvery(f, receiver) { ...@@ -1121,11 +1161,24 @@ function ArrayEvery(f, receiver) {
receiver = ToObject(receiver); receiver = ToObject(receiver);
} }
for (var i = 0; i < length; i++) { if (%DebugCallbackSupportsStepping(f)) {
if (i in array) { for (var i = 0; i < length; i++) {
var element = array[i]; if (i in array) {
if (!%_CallFunction(receiver, element, i, array, f)) return false; var element = array[i];
// Prepare break slots for debugger step in.
%DebugPrepareStepInIfStepping(f);
if (!%_CallFunction(receiver, element, i, array, f)) return false;
}
}
} else {
// This is a duplicate of the previous loop sans debug stepping.
for (var i = 0; i < length; i++) {
if (i in array) {
var element = array[i];
if (!%_CallFunction(receiver, element, i, array, f)) return false;
}
} }
// End of duplicate.
} }
return true; return true;
} }
...@@ -1152,11 +1205,24 @@ function ArrayMap(f, receiver) { ...@@ -1152,11 +1205,24 @@ function ArrayMap(f, receiver) {
var result = new $Array(); var result = new $Array();
var accumulator = new InternalArray(length); var accumulator = new InternalArray(length);
for (var i = 0; i < length; i++) { if (%DebugCallbackSupportsStepping(f)) {
if (i in array) { for (var i = 0; i < length; i++) {
var element = array[i]; if (i in array) {
accumulator[i] = %_CallFunction(receiver, element, i, array, f); var element = array[i];
// Prepare break slots for debugger step in.
%DebugPrepareStepInIfStepping(f);
accumulator[i] = %_CallFunction(receiver, element, i, array, f);
}
} }
} else {
// This is a duplicate of the previous loop sans debug stepping.
for (var i = 0; i < length; i++) {
if (i in array) {
var element = array[i];
accumulator[i] = %_CallFunction(receiver, element, i, array, f);
}
}
// End of duplicate.
} }
%MoveArrayContents(accumulator, result); %MoveArrayContents(accumulator, result);
return result; return result;
...@@ -1311,11 +1377,27 @@ function ArrayReduce(callback, current) { ...@@ -1311,11 +1377,27 @@ function ArrayReduce(callback, current) {
} }
var receiver = %GetDefaultReceiver(callback); var receiver = %GetDefaultReceiver(callback);
for (; i < length; i++) {
if (i in array) { if (%DebugCallbackSupportsStepping(callback)) {
var element = array[i]; for (; i < length; i++) {
current = %_CallFunction(receiver, current, element, i, array, callback); if (i in array) {
var element = array[i];
// Prepare break slots for debugger step in.
%DebugPrepareStepInIfStepping(callback);
current =
%_CallFunction(receiver, current, element, i, array, callback);
}
}
} else {
// This is a duplicate of the previous loop sans debug stepping.
for (; i < length; i++) {
if (i in array) {
var element = array[i];
current =
%_CallFunction(receiver, current, element, i, array, callback);
}
} }
// End of duplicate.
} }
return current; return current;
} }
...@@ -1348,11 +1430,27 @@ function ArrayReduceRight(callback, current) { ...@@ -1348,11 +1430,27 @@ function ArrayReduceRight(callback, current) {
} }
var receiver = %GetDefaultReceiver(callback); var receiver = %GetDefaultReceiver(callback);
for (; i >= 0; i--) {
if (i in array) { if (%DebugCallbackSupportsStepping(callback)) {
var element = array[i]; for (; i >= 0; i--) {
current = %_CallFunction(receiver, current, element, i, array, callback); if (i in array) {
var element = array[i];
// Prepare break slots for debugger step in.
%DebugPrepareStepInIfStepping(callback);
current =
%_CallFunction(receiver, current, element, i, array, callback);
}
}
} else {
// This is a duplicate of the previous loop sans debug stepping.
for (; i >= 0; i--) {
if (i in array) {
var element = array[i];
current =
%_CallFunction(receiver, current, element, i, array, callback);
}
} }
// End of duplicate.
} }
return current; return current;
} }
......
// Copyright 2011 the V8 project authors. All rights reserved. // Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are // modification, are permitted provided that the following conditions are
// met: // met:
...@@ -245,6 +245,8 @@ class Debug { ...@@ -245,6 +245,8 @@ class Debug {
bool IsBreakOnException(ExceptionBreakType type); bool IsBreakOnException(ExceptionBreakType type);
void PrepareStep(StepAction step_action, int step_count); void PrepareStep(StepAction step_action, int step_count);
void ClearStepping(); void ClearStepping();
void ClearStepOut();
bool IsStepping() { return thread_local_.step_count_ > 0; }
bool StepNextContinue(BreakLocationIterator* break_location_iterator, bool StepNextContinue(BreakLocationIterator* break_location_iterator,
JavaScriptFrame* frame); JavaScriptFrame* frame);
static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared); static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared);
...@@ -464,7 +466,6 @@ class Debug { ...@@ -464,7 +466,6 @@ class Debug {
void ActivateStepIn(StackFrame* frame); void ActivateStepIn(StackFrame* frame);
void ClearStepIn(); void ClearStepIn();
void ActivateStepOut(StackFrame* frame); void ActivateStepOut(StackFrame* frame);
void ClearStepOut();
void ClearStepNext(); void ClearStepNext();
// Returns whether the compile succeeded. // Returns whether the compile succeeded.
void RemoveDebugInfo(Handle<DebugInfo> debug_info); void RemoveDebugInfo(Handle<DebugInfo> debug_info);
......
...@@ -4696,6 +4696,36 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_StoreArrayLiteralElement) { ...@@ -4696,6 +4696,36 @@ RUNTIME_FUNCTION(MaybeObject*, Runtime_StoreArrayLiteralElement) {
} }
// Check whether debugger and is about to step into the callback that is passed
// to a built-in function such as Array.forEach.
RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugCallbackSupportsStepping) {
if (!isolate->IsDebuggerActive()) return isolate->heap()->false_value();
CONVERT_ARG_CHECKED(Object, callback, 0);
// We do not step into the callback if it's a builtin or not even a function.
if (!callback->IsJSFunction() || JSFunction::cast(callback)->IsBuiltin()) {
return isolate->heap()->false_value();
}
return isolate->heap()->true_value();
}
// Set one shot breakpoints for the callback function that is passed to a
// built-in function such as Array.forEach to enable stepping into the callback.
RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrepareStepInIfStepping) {
Debug* debug = isolate->debug();
if (!debug->IsStepping()) return NULL;
CONVERT_ARG_CHECKED(Object, callback, 0);
HandleScope scope(isolate);
Handle<SharedFunctionInfo> shared_info(JSFunction::cast(callback)->shared());
// When leaving the callback, step out has been activated, but not performed
// if we do not leave the builtin. To be able to step into the callback
// again, we need to clear the step out at this point.
debug->ClearStepOut();
debug->FloodWithOneShot(shared_info);
return NULL;
}
// Set a local property, even if it is READ_ONLY. If the property does not // Set a local property, even if it is READ_ONLY. If the property does not
// exist, it will be added with attributes NONE. // exist, it will be added with attributes NONE.
RUNTIME_FUNCTION(MaybeObject*, Runtime_IgnoreAttributesAndSetProperty) { RUNTIME_FUNCTION(MaybeObject*, Runtime_IgnoreAttributesAndSetProperty) {
......
...@@ -97,6 +97,8 @@ namespace internal { ...@@ -97,6 +97,8 @@ namespace internal {
F(AllocateInNewSpace, 1, 1) \ F(AllocateInNewSpace, 1, 1) \
F(SetNativeFlag, 1, 1) \ F(SetNativeFlag, 1, 1) \
F(StoreArrayLiteralElement, 5, 1) \ F(StoreArrayLiteralElement, 5, 1) \
F(DebugCallbackSupportsStepping, 1, 1) \
F(DebugPrepareStepInIfStepping, 1, 1) \
\ \
/* Array join support */ \ /* Array join support */ \
F(PushIfAbsent, 2, 1) \ F(PushIfAbsent, 2, 1) \
......
...@@ -266,6 +266,10 @@ function StringReplace(search, replace) { ...@@ -266,6 +266,10 @@ function StringReplace(search, replace) {
// Compute the string to replace with. // Compute the string to replace with.
if (IS_SPEC_FUNCTION(replace)) { if (IS_SPEC_FUNCTION(replace)) {
var receiver = %GetDefaultReceiver(replace); var receiver = %GetDefaultReceiver(replace);
// Prepare break slots for debugger step in.
if (%DebugCallbackSupportsStepping(replace)) {
%DebugPrepareStepInIfStepping(replace);
}
builder.add(%_CallFunction(receiver, builder.add(%_CallFunction(receiver,
search, search,
start, start,
...@@ -434,24 +438,49 @@ function StringReplaceGlobalRegExpWithFunction(subject, regexp, replace) { ...@@ -434,24 +438,49 @@ function StringReplaceGlobalRegExpWithFunction(subject, regexp, replace) {
var match_start = 0; var match_start = 0;
var override = new InternalArray(null, 0, subject); var override = new InternalArray(null, 0, subject);
var receiver = %GetDefaultReceiver(replace); var receiver = %GetDefaultReceiver(replace);
while (i < len) { if (%DebugCallbackSupportsStepping(replace)) {
var elem = res[i]; while (i < len) {
if (%_IsSmi(elem)) { var elem = res[i];
if (elem > 0) { if (%_IsSmi(elem)) {
match_start = (elem >> 11) + (elem & 0x7ff); if (elem > 0) {
match_start = (elem >> 11) + (elem & 0x7ff);
} else {
match_start = res[++i] - elem;
}
} else { } else {
match_start = res[++i] - elem; override[0] = elem;
override[1] = match_start;
lastMatchInfoOverride = override;
%DebugPrepareStepInIfStepping(replace);
var func_result =
%_CallFunction(receiver, elem, match_start, subject, replace);
res[i] = TO_STRING_INLINE(func_result);
match_start += elem.length;
} }
} else { i++;
override[0] = elem;
override[1] = match_start;
lastMatchInfoOverride = override;
var func_result =
%_CallFunction(receiver, elem, match_start, subject, replace);
res[i] = TO_STRING_INLINE(func_result);
match_start += elem.length;
} }
i++; } else {
// This is a duplicate of the previous loop sans debug stepping.
while (i < len) {
var elem = res[i];
if (%_IsSmi(elem)) {
if (elem > 0) {
match_start = (elem >> 11) + (elem & 0x7ff);
} else {
match_start = res[++i] - elem;
}
} else {
override[0] = elem;
override[1] = match_start;
lastMatchInfoOverride = override;
var func_result =
%_CallFunction(receiver, elem, match_start, subject, replace);
res[i] = TO_STRING_INLINE(func_result);
match_start += elem.length;
}
i++;
}
// End of duplicate.
} }
} else { } else {
var receiver = %GetDefaultReceiver(replace); var receiver = %GetDefaultReceiver(replace);
...@@ -491,9 +520,12 @@ function StringReplaceNonGlobalRegExpWithFunction(subject, regexp, replace) { ...@@ -491,9 +520,12 @@ function StringReplaceNonGlobalRegExpWithFunction(subject, regexp, replace) {
if (m == 1) { if (m == 1) {
// No captures, only the match, which is always valid. // No captures, only the match, which is always valid.
var s = SubString(subject, index, endOfMatch); var s = SubString(subject, index, endOfMatch);
// Prepare break slots for debugger step in.
if (%DebugCallbackSupportsStepping(replace)) {
%DebugPrepareStepInIfStepping(replace);
}
// Don't call directly to avoid exposing the built-in global object. // Don't call directly to avoid exposing the built-in global object.
replacement = replacement = %_CallFunction(receiver, s, index, subject, replace);
%_CallFunction(receiver, s, index, subject, replace);
} else { } else {
var parameters = new InternalArray(m + 2); var parameters = new InternalArray(m + 2);
for (var j = 0; j < m; j++) { for (var j = 0; j < m; j++) {
......
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Flags: --expose-debug-as debug
// Test stepping into callbacks passed to builtin functions.
Debug = debug.Debug
var exception = false;
function array_listener(event, exec_state, event_data, data) {
try {
if (event == Debug.DebugEvent.Break) {
if (breaks == 0) {
exec_state.prepareStep(Debug.StepAction.StepIn, 2);
breaks = 1;
} else if (breaks <= 3) {
breaks++;
// Check whether we break at the expected line.
print(event_data.sourceLineText());
assertTrue(event_data.sourceLineText().indexOf("Expected to step") > 0);
exec_state.prepareStep(Debug.StepAction.StepIn, 3);
}
}
} catch (e) {
exception = true;
}
};
function cb_false(num) {
print("element " + num); // Expected to step to this point.
return false;
}
function cb_true(num) {
print("element " + num); // Expected to step to this point.
return true;
}
function cb_reduce(a, b) {
print("elements " + a + " and " + b); // Expected to step to this point.
return a + b;
}
var a = [1, 2, 3, 4];
Debug.setListener(array_listener);
var breaks = 0;
debugger;
a.forEach(cb_true);
assertFalse(exception);
assertEquals(4, breaks);
breaks = 0;
debugger;
a.some(cb_false);
assertFalse(exception);
assertEquals(4, breaks);
breaks = 0;
debugger;
a.every(cb_true);
assertEquals(4, breaks);
assertFalse(exception);
breaks = 0;
debugger;
a.map(cb_true);
assertFalse(exception);
assertEquals(4, breaks);
breaks = 0;
debugger;
a.filter(cb_true);
assertFalse(exception);
assertEquals(4, breaks);
breaks = 0;
debugger;
a.reduce(cb_reduce);
assertFalse(exception);
assertEquals(4, breaks);
breaks = 0;
debugger;
a.reduceRight(cb_reduce);
assertFalse(exception);
assertEquals(4, breaks);
Debug.setListener(null);
// Test two levels of builtin callbacks:
// Array.forEach calls a callback function, which by itself uses
// Array.forEach with another callback function.
function second_level_listener(event, exec_state, event_data, data) {
try {
if (event == Debug.DebugEvent.Break) {
if (breaks == 0) {
exec_state.prepareStep(Debug.StepAction.StepIn, 3);
breaks = 1;
} else if (breaks <= 16) {
breaks++;
// Check whether we break at the expected line.
assertTrue(event_data.sourceLineText().indexOf("Expected to step") > 0);
// Step two steps further every four breaks to skip the
// forEach call in the first level of recurision.
var step = (breaks % 4 == 1) ? 6 : 3;
exec_state.prepareStep(Debug.StepAction.StepIn, step);
}
}
} catch (e) {
exception = true;
}
};
function cb_foreach(num) {
a.forEach(cb_true);
print("back to the first level of recursion.");
}
Debug.setListener(second_level_listener);
breaks = 0;
debugger;
a.forEach(cb_foreach);
assertFalse(exception);
assertEquals(17, breaks);
Debug.setListener(null);
//Test replace callback in String.replace.
function replace_listener(event, exec_state, event_data, data) {
try {
if (event == Debug.DebugEvent.Break) {
if (breaks == 0) {
exec_state.prepareStep(Debug.StepAction.StepIn, 2);
breaks = 1;
} else {
// Check whether we break at the expected line.
assertTrue(event_data.sourceLineText().indexOf("Expected to step") > 0);
}
}
} catch (e) {
exception = true;
}
};
function cb_replace(match) {
print("matching string: " + match); // Expected to step to this point.
return "_";
}
var s = "abcdefgehijke";
Debug.setListener(replace_listener);
breaks = 0;
debugger;
assertEquals("ab_defgehijke", s.replace("c", cb_replace));
assertFalse(exception);
assertEquals(1, breaks);
breaks = 0;
debugger;
assertEquals("abcdefgehij_", s.replace(/..$/, cb_replace));
assertFalse(exception);
assertEquals(1, breaks);
breaks = 0;
debugger;
assertEquals("abcd_fg_hijk_", s.replace(/e/g, cb_replace));
assertFalse(exception);
assertEquals(1, breaks);
Debug.setListener(null);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment