Exposed method for finding global context by traversing context chain to API.

Review URL: http://codereview.chromium.org/112035

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2005 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
yurys@chromium.org 2009-05-20 07:28:42 +00:00
parent b8c09e9590
commit 897ecefe1e
2 changed files with 117 additions and 14 deletions

View File

@ -2384,7 +2384,9 @@ v8::Handle<v8::String> MessageImpl::GetJSON() const {
v8::Handle<v8::Context> MessageImpl::GetEventContext() const {
return v8::Utils::ToLocal(Debug::debugger_entry()->GetContext());
Handle<Context> context = Debug::debugger_entry()->GetContext();
Handle<Context> global_context(context->global_context());
return v8::Utils::ToLocal(global_context);
}

View File

@ -4172,11 +4172,8 @@ TEST(DebuggerUnload) {
}
// Debugger message handler which counts the number of times it is called.
static int message_handler_hit_count = 0;
static void MessageHandlerHitCount(const v8::Debug::Message& message) {
message_handler_hit_count++;
// Sends continue command to the debugger.
static void SendContinueCommand() {
const int kBufferSize = 1000;
uint16_t buffer[kBufferSize];
const char* command_continue =
@ -4188,6 +4185,15 @@ static void MessageHandlerHitCount(const v8::Debug::Message& message) {
}
// Debugger message handler which counts the number of times it is called.
static int message_handler_hit_count = 0;
static void MessageHandlerHitCount(const v8::Debug::Message& message) {
message_handler_hit_count++;
SendContinueCommand();
}
// Test clearing the debug message handler.
TEST(DebuggerClearMessageHandler) {
v8::HandleScope scope;
@ -4624,16 +4630,9 @@ static void ContextCheckMessageHandler(const v8::Debug::Message& message) {
expected_context_data));
message_handler_hit_count++;
const int kBufferSize = 1000;
uint16_t buffer[kBufferSize];
const char* command_continue =
"{\"seq\":0,"
"\"type\":\"request\","
"\"command\":\"continue\"}";
// Send a continue command for break events.
if (message.GetEvent() == v8::Break) {
v8::Debug::SendCommand(buffer, AsciiToUtf16(command_continue, buffer));
SendContinueCommand();
}
}
@ -4694,6 +4693,108 @@ TEST(ContextData) {
}
// Common part of EvalContextData and NestedBreakEventContextData tests.
static void ExecuteScriptForContextCheck() {
// Create a context.
v8::Persistent<v8::Context> context_1;
v8::Handle<v8::ObjectTemplate> global_template =
v8::Handle<v8::ObjectTemplate>();
v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>();
context_1 = v8::Context::New(NULL, global_template, global_object);
// Default data value is undefined.
CHECK(context_1->GetData()->IsUndefined());
// Set and check a data value.
v8::Handle<v8::Value> data_1 = v8::Number::New(1);
context_1->SetData(data_1);
CHECK(context_1->GetData()->StrictEquals(data_1));
// Simple test function with eval that causes a break.
const char* source = "function f() { eval('debugger;'); }";
// Enter and run function in the context.
{
v8::Context::Scope context_scope(context_1);
expected_context = context_1;
expected_context_data = data_1;
v8::Local<v8::Function> f = CompileFunction(source, "f");
f->Call(context_1->Global(), 0, NULL);
}
}
// Test which creates a context and sets embedder data on it. Checks that this
// data is set correctly and that when the debug message handler is called for
// break event in an eval statement the expected context is the one returned by
// Message.GetEventContext.
TEST(EvalContextData) {
v8::HandleScope scope;
v8::Debug::SetMessageHandler2(ContextCheckMessageHandler);
ExecuteScriptForContextCheck();
// One time compile event and one time break event.
CHECK_GT(message_handler_hit_count, 2);
}
static bool sent_eval = false;
static int break_count = 0;
// Check that the expected context is the one generating the debug event
// including the case of nested break event.
static void DebugEvalContextCheckMessageHandler(
const v8::Debug::Message& message) {
CHECK(message.GetEventContext() == expected_context);
CHECK(message.GetEventContext()->GetData()->StrictEquals(
expected_context_data));
message_handler_hit_count++;
if (message.IsEvent() && message.GetEvent() == v8::Break) {
break_count++;
if (!sent_eval) {
sent_eval = true;
const int kBufferSize = 1000;
uint16_t buffer[kBufferSize];
const char* eval_command =
"{\"seq\":0,"
"\"type\":\"request\","
"\"command\":\"evaluate\","
"arguments:{\"expression\":\"debugger;\","
"\"global\":true,\"disable_break\":false}}";
// Send evaluate command.
v8::Debug::SendCommand(buffer, AsciiToUtf16(eval_command, buffer));
return;
} else {
// It's a break event caused by the evaluation request above.
SendContinueCommand();
}
} else if (message.IsResponse()) {
// Response to the evaluation request. We're still on the breakpoint so
// send continue.
SendContinueCommand();
}
}
// Tests that context returned for break event is correct when the event occurs
// in 'evaluate' debugger request.
TEST(NestedBreakEventContextData) {
v8::HandleScope scope;
v8::Debug::SetMessageHandler2(DebugEvalContextCheckMessageHandler);
ExecuteScriptForContextCheck();
// One time compile event and two times break event.
CHECK_GT(message_handler_hit_count, 3);
// One break from the source and another from the evaluate request.
CHECK_EQ(break_count, 2);
}
// Debug event listener which counts the script collected events.
int script_collected_count = 0;
static void DebugEventScriptCollectedEvent(v8::DebugEvent event,