Commit a2304802 authored by dgozman's avatar dgozman Committed by Commit bot

[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}
parent b3acc272
......@@ -114,15 +114,13 @@ class ConnectTask : public TaskRunner::Task {
bool is_inspector_task() final { return true; }
void Run(v8::Isolate* isolate,
const v8::Global<v8::Context>& global_context) {
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> 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<v8::Context>& 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<v8::Context>& 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<v8::Context>& global_context) override {
private:
void Run() override {
v8_inspector::V8InspectorSession* session = nullptr;
{
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> 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<uint16_t> message_;
int context_group_id_;
};
......
......@@ -190,9 +190,8 @@ class UtilsExtension : public TaskRunner::SetupGlobalTask {
v8::internal::Vector<const char> chars;
v8::Isolate* isolate = args.GetIsolate();
if (ReadFile(isolate, args[0], &chars)) {
ExecuteStringTask task(chars);
v8::Global<v8::Context> 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<v8::Context>& 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<v8::Context> context = global_context.Get(isolate);
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = default_context();
v8::Context::Scope context_scope(context);
v8::Local<v8::Function> function = function_.Get(isolate);
v8::Local<v8::Function> function = function_.Get(isolate());
v8::MaybeLocal<v8::Value> result;
result = function->Call(context, context->Global(), 0, nullptr);
}
private:
v8::Global<v8::Function> function_;
};
......
......@@ -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<v8::Context>& 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<const char>& expression)
: AsyncTask(nullptr, nullptr), expression_utf8_(expression) {}
void ExecuteStringTask::AsyncRun(v8::Isolate* isolate,
const v8::Global<v8::Context>& context) {
v8::MicrotasksScope microtasks_scope(isolate,
void ExecuteStringTask::AsyncRun() {
v8::MicrotasksScope microtasks_scope(isolate(),
v8::MicrotasksScope::kRunMicrotasks);
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> local_context = context.Get(isolate);
v8::Context::Scope context_scope(local_context);
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = default_context();
v8::Context::Scope context_scope(context);
v8::Local<v8::String> name =
v8::String::NewFromTwoByte(isolate, name_.start(),
v8::String::NewFromTwoByte(isolate(), name_.start(),
v8::NewStringType::kNormal, name_.length())
.ToLocalChecked();
v8::Local<v8::Integer> line_offset = v8::Integer::New(isolate, line_offset_);
v8::Local<v8::Integer> line_offset =
v8::Integer::New(isolate(), line_offset_);
v8::Local<v8::Integer> 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<v8::Value>(),
/* resource_is_opaque */ v8::Local<v8::Boolean>(),
/* is_wasm */ v8::Local<v8::Boolean>(),
v8::Boolean::New(isolate, is_module_));
v8::Boolean::New(isolate(), is_module_));
v8::Local<v8::String> 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<v8::Script> script;
if (!v8::ScriptCompiler::Compile(local_context, &scriptSource)
.ToLocal(&script))
if (!v8::ScriptCompiler::Compile(context, &scriptSource).ToLocal(&script))
return;
v8::MaybeLocal<v8::Value> result;
result = script->Run(local_context);
result = script->Run(context);
} else {
v8::Local<v8::Module> 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<v8::Value> 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);
}
}
......@@ -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<v8::Context>& 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<v8::Context> 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<v8::Context>& context) override;
virtual void AsyncRun(v8::Isolate* isolate,
const v8::Global<v8::Context>& 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<const char>& expression);
bool is_inspector_task() override { return false; }
void AsyncRun(v8::Isolate* isolate,
const v8::Global<v8::Context>& context) override;
private:
void AsyncRun() override;
v8::internal::Vector<uint16_t> expression_;
v8::internal::Vector<const char> expression_utf8_;
v8::internal::Vector<uint16_t> name_;
......
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