From a2304802d8d1349bc7ac280a84b2cffa68b883ea Mon Sep 17 00:00:00 2001 From: dgozman Date: Tue, 16 May 2017 13:38:33 -0700 Subject: [PATCH] [inspector] Pass TaskRunner instead of Context to all tasks This will make it easier to create more connections/context groups. BUG=none Review-Url: https://codereview.chromium.org/2886903003 Cr-Commit-Position: refs/heads/master@{#45352} --- test/inspector/inspector-impl.cc | 32 +++++++++------------- test/inspector/inspector-test.cc | 18 ++++++------- test/inspector/task-runner.cc | 46 +++++++++++++++----------------- test/inspector/task-runner.h | 30 ++++++++++++++------- 4 files changed, 63 insertions(+), 63 deletions(-) diff --git a/test/inspector/inspector-impl.cc b/test/inspector/inspector-impl.cc index 1090cb8784..408f475292 100644 --- a/test/inspector/inspector-impl.cc +++ b/test/inspector/inspector-impl.cc @@ -114,15 +114,13 @@ class ConnectTask : public TaskRunner::Task { bool is_inspector_task() final { return true; } - void Run(v8::Isolate* isolate, - const v8::Global& global_context) { - v8::HandleScope handle_scope(isolate); - v8::Local context = global_context.Get(isolate); - client_->connect(context); + private: + void Run() override { + v8::HandleScope handle_scope(isolate()); + client_->connect(default_context()); if (ready_semaphore_) ready_semaphore_->Signal(); } - private: InspectorClientImpl* client_; v8::base::Semaphore* ready_semaphore_; }; @@ -138,13 +136,12 @@ class DisconnectTask : public TaskRunner::Task { bool is_inspector_task() final { return true; } - void Run(v8::Isolate* isolate, - const v8::Global& global_context) { + private: + void Run() override { client_->disconnect(reset_inspector_); if (ready_semaphore_) ready_semaphore_->Signal(); } - private: InspectorClientImpl* client_; bool reset_inspector_; v8::base::Semaphore* ready_semaphore_; @@ -164,13 +161,12 @@ class CreateContextGroupTask : public TaskRunner::Task { bool is_inspector_task() final { return true; } - void Run(v8::Isolate* isolate, - const v8::Global& global_context) { + private: + void Run() override { *context_group_id_ = client_->createContextGroup(setup_global_tasks_); if (ready_semaphore_) ready_semaphore_->Signal(); } - private: InspectorClientImpl* client_; TaskRunner::SetupGlobalTasks setup_global_tasks_; v8::base::Semaphore* ready_semaphore_; @@ -360,16 +356,15 @@ class SendMessageToBackendTask : public TaskRunner::Task { bool is_inspector_task() final { return true; } - void Run(v8::Isolate* isolate, - const v8::Global& global_context) override { + private: + void Run() override { v8_inspector::V8InspectorSession* session = nullptr; { - v8::HandleScope handle_scope(isolate); - v8::Local context = global_context.Get(isolate); + v8::HandleScope handle_scope(isolate()); if (!context_group_id_) { - session = InspectorClientImpl::SessionFromContext(context); + session = InspectorClientImpl::SessionFromContext(default_context()); } else { - session = InspectorClientFromContext(context) + session = InspectorClientFromContext(default_context()) ->sessions_[context_group_id_] .get(); } @@ -379,7 +374,6 @@ class SendMessageToBackendTask : public TaskRunner::Task { session->dispatchProtocolMessage(message_view); } - private: v8::internal::Vector message_; int context_group_id_; }; diff --git a/test/inspector/inspector-test.cc b/test/inspector/inspector-test.cc index d4f4654016..a3f42ede87 100644 --- a/test/inspector/inspector-test.cc +++ b/test/inspector/inspector-test.cc @@ -190,9 +190,8 @@ class UtilsExtension : public TaskRunner::SetupGlobalTask { v8::internal::Vector chars; v8::Isolate* isolate = args.GetIsolate(); if (ReadFile(isolate, args[0], &chars)) { - ExecuteStringTask task(chars); - v8::Global context(isolate, isolate->GetCurrentContext()); - task.Run(isolate, context); + ExecuteStringTask(chars).RunOnTaskRunner( + TaskRunner::FromContext(isolate->GetCurrentContext())); } } @@ -302,20 +301,19 @@ class SetTimeoutTask : public AsyncTask { bool is_inspector_task() final { return false; } - void AsyncRun(v8::Isolate* isolate, - const v8::Global& global_context) override { - v8::MicrotasksScope microtasks_scope(isolate, + private: + void AsyncRun() override { + v8::MicrotasksScope microtasks_scope(isolate(), v8::MicrotasksScope::kRunMicrotasks); - v8::HandleScope handle_scope(isolate); - v8::Local context = global_context.Get(isolate); + v8::HandleScope handle_scope(isolate()); + v8::Local context = default_context(); v8::Context::Scope context_scope(context); - v8::Local function = function_.Get(isolate); + v8::Local function = function_.Get(isolate()); v8::MaybeLocal result; result = function->Call(context, context->Global(), 0, nullptr); } - private: v8::Global function_; }; diff --git a/test/inspector/task-runner.cc b/test/inspector/task-runner.cc index 792af217c0..a5aa2a1e2c 100644 --- a/test/inspector/task-runner.cc +++ b/test/inspector/task-runner.cc @@ -105,7 +105,7 @@ void TaskRunner::RunMessageLoop(bool only_protocol) { v8::Isolate::Scope isolate_scope(isolate_); if (catch_exceptions_) { v8::TryCatch try_catch(isolate_); - task->Run(isolate_, contexts_.begin()->second); + task->RunOnTaskRunner(this); delete task; if (try_catch.HasCaught()) { ReportUncaughtException(isolate_, try_catch); @@ -114,7 +114,7 @@ void TaskRunner::RunMessageLoop(bool only_protocol) { _exit(0); } } else { - task->Run(isolate_, contexts_.begin()->second); + task->RunOnTaskRunner(this); delete task; } } @@ -183,10 +183,9 @@ AsyncTask::AsyncTask(const char* task_name, } } -void AsyncTask::Run(v8::Isolate* isolate, - const v8::Global& context) { +void AsyncTask::Run() { if (inspector_) inspector_->asyncTaskStarted(this); - AsyncRun(isolate, context); + AsyncRun(); if (inspector_) inspector_->asyncTaskFinished(this); } @@ -206,21 +205,21 @@ ExecuteStringTask::ExecuteStringTask( const v8::internal::Vector& expression) : AsyncTask(nullptr, nullptr), expression_utf8_(expression) {} -void ExecuteStringTask::AsyncRun(v8::Isolate* isolate, - const v8::Global& context) { - v8::MicrotasksScope microtasks_scope(isolate, +void ExecuteStringTask::AsyncRun() { + v8::MicrotasksScope microtasks_scope(isolate(), v8::MicrotasksScope::kRunMicrotasks); - v8::HandleScope handle_scope(isolate); - v8::Local local_context = context.Get(isolate); - v8::Context::Scope context_scope(local_context); + v8::HandleScope handle_scope(isolate()); + v8::Local context = default_context(); + v8::Context::Scope context_scope(context); v8::Local name = - v8::String::NewFromTwoByte(isolate, name_.start(), + v8::String::NewFromTwoByte(isolate(), name_.start(), v8::NewStringType::kNormal, name_.length()) .ToLocalChecked(); - v8::Local line_offset = v8::Integer::New(isolate, line_offset_); + v8::Local line_offset = + v8::Integer::New(isolate(), line_offset_); v8::Local column_offset = - v8::Integer::New(isolate, column_offset_); + v8::Integer::New(isolate(), column_offset_); v8::ScriptOrigin origin( name, line_offset, column_offset, @@ -229,15 +228,15 @@ void ExecuteStringTask::AsyncRun(v8::Isolate* isolate, /* source_map_url */ v8::Local(), /* resource_is_opaque */ v8::Local(), /* is_wasm */ v8::Local(), - v8::Boolean::New(isolate, is_module_)); + v8::Boolean::New(isolate(), is_module_)); v8::Local source; if (expression_.length()) { - source = v8::String::NewFromTwoByte(isolate, expression_.start(), + source = v8::String::NewFromTwoByte(isolate(), expression_.start(), v8::NewStringType::kNormal, expression_.length()) .ToLocalChecked(); } else { - source = v8::String::NewFromUtf8(isolate, expression_utf8_.start(), + source = v8::String::NewFromUtf8(isolate(), expression_utf8_.start(), v8::NewStringType::kNormal, expression_utf8_.length()) .ToLocalChecked(); @@ -246,22 +245,21 @@ void ExecuteStringTask::AsyncRun(v8::Isolate* isolate, v8::ScriptCompiler::Source scriptSource(source, origin); if (!is_module_) { v8::Local script; - if (!v8::ScriptCompiler::Compile(local_context, &scriptSource) - .ToLocal(&script)) + if (!v8::ScriptCompiler::Compile(context, &scriptSource).ToLocal(&script)) return; v8::MaybeLocal result; - result = script->Run(local_context); + result = script->Run(context); } else { v8::Local module; - if (!v8::ScriptCompiler::CompileModule(isolate, &scriptSource) + if (!v8::ScriptCompiler::CompileModule(isolate(), &scriptSource) .ToLocal(&module)) { return; } - if (!module->Instantiate(local_context, &TaskRunner::ModuleResolveCallback)) + if (!module->Instantiate(context, &TaskRunner::ModuleResolveCallback)) return; v8::Local result; - if (!module->Evaluate(local_context).ToLocal(&result)) return; - TaskRunner* runner = TaskRunner::FromContext(local_context); + if (!module->Evaluate(context).ToLocal(&result)) return; + TaskRunner* runner = TaskRunner::FromContext(context); runner->RegisterModule(name_, module); } } diff --git a/test/inspector/task-runner.h b/test/inspector/task-runner.h index c4adf7e4f1..88320cd622 100644 --- a/test/inspector/task-runner.h +++ b/test/inspector/task-runner.h @@ -32,8 +32,21 @@ class TaskRunner : public v8::base::Thread { public: virtual ~Task() {} virtual bool is_inspector_task() = 0; - virtual void Run(v8::Isolate* isolate, - const v8::Global& context) = 0; + void RunOnTaskRunner(TaskRunner* task_runner) { + task_runner_ = task_runner; + Run(); + task_runner_ = nullptr; + } + + protected: + virtual void Run() = 0; + v8::Isolate* isolate() const { return task_runner_->isolate_; } + v8::Local default_context() const { + return task_runner_->contexts_.begin()->second.Get(isolate()); + } + + private: + TaskRunner* task_runner_ = nullptr; }; class SetupGlobalTask { @@ -110,12 +123,10 @@ class AsyncTask : public TaskRunner::Task { AsyncTask(const char* task_name, v8_inspector::V8Inspector* inspector); virtual ~AsyncTask() = default; - void Run(v8::Isolate* isolate, - const v8::Global& context) override; - virtual void AsyncRun(v8::Isolate* isolate, - const v8::Global& context) = 0; - protected: + virtual void AsyncRun() = 0; + void Run() override; + v8_inspector::V8Inspector* inspector_; }; @@ -131,10 +142,9 @@ class ExecuteStringTask : public AsyncTask { const v8::internal::Vector& expression); bool is_inspector_task() override { return false; } - void AsyncRun(v8::Isolate* isolate, - const v8::Global& context) override; - private: + void AsyncRun() override; + v8::internal::Vector expression_; v8::internal::Vector expression_utf8_; v8::internal::Vector name_;