Commit 93c12e5d authored by dgozman's avatar dgozman Committed by Commit bot

[inspector] Cleanup inspector test

- reduce boilerplate;
- encapsulate V8Inspector inside IsolateData;
- remove extra interfaces and classes.

BUG=none

Review-Url: https://codereview.chromium.org/2901523003
Cr-Commit-Position: refs/heads/master@{#45470}
parent 66d242de
This diff is collapsed.
......@@ -4,6 +4,7 @@
#include "test/inspector/isolate-data.h"
#include "src/inspector/test-interface.h"
#include "test/inspector/task-runner.h"
namespace {
......@@ -38,45 +39,20 @@ void Print(v8::Isolate* isolate, const v8_inspector::StringView& string) {
fwrite(*utf8_string, sizeof(**utf8_string), utf8_string.length(), stdout);
}
class ChannelImpl final : public v8_inspector::V8Inspector::Channel {
public:
ChannelImpl(IsolateData::FrontendChannel* frontend_channel, int session_id)
: frontend_channel_(frontend_channel), session_id_(session_id) {}
virtual ~ChannelImpl() = default;
private:
void sendResponse(
int callId,
std::unique_ptr<v8_inspector::StringBuffer> message) override {
frontend_channel_->SendMessageToFrontend(session_id_, message->string());
}
void sendNotification(
std::unique_ptr<v8_inspector::StringBuffer> message) override {
frontend_channel_->SendMessageToFrontend(session_id_, message->string());
}
void flushProtocolNotifications() override {}
IsolateData::FrontendChannel* frontend_channel_;
int session_id_;
DISALLOW_COPY_AND_ASSIGN(ChannelImpl);
};
} // namespace
IsolateData::IsolateData(TaskRunner* task_runner,
IsolateData::SetupGlobalTasks setup_global_tasks,
v8::StartupData* startup_data,
FrontendChannel* channel)
v8::StartupData* startup_data, bool with_inspector)
: task_runner_(task_runner),
setup_global_tasks_(std::move(setup_global_tasks)),
frontend_channel_(channel) {
setup_global_tasks_(std::move(setup_global_tasks)) {
v8::Isolate::CreateParams params;
params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
params.snapshot_blob = startup_data;
isolate_ = v8::Isolate::New(params);
isolate_->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped);
if (frontend_channel_) {
if (with_inspector) {
isolate_->AddMessageListener(&IsolateData::MessageHandler);
inspector_ = v8_inspector::V8Inspector::create(isolate_, this);
}
......@@ -141,11 +117,10 @@ v8::MaybeLocal<v8::Module> IsolateData::ModuleResolveCallback(
}
int IsolateData::ConnectSession(int context_group_id,
const v8_inspector::StringView& state) {
const v8_inspector::StringView& state,
v8_inspector::V8Inspector::Channel* channel) {
int session_id = ++last_session_id_;
channels_[session_id].reset(new ChannelImpl(frontend_channel_, session_id));
sessions_[session_id] =
inspector_->connect(context_group_id, channels_[session_id].get(), state);
sessions_[session_id] = inspector_->connect(context_group_id, channel, state);
context_group_by_session_[sessions_[session_id].get()] = context_group_id;
return session_id;
}
......@@ -157,7 +132,6 @@ std::unique_ptr<v8_inspector::StringBuffer> IsolateData::DisconnectSession(
context_group_by_session_.erase(it->second.get());
std::unique_ptr<v8_inspector::StringBuffer> result = it->second->stateJSON();
sessions_.erase(it);
channels_.erase(session_id);
return result;
}
......@@ -193,6 +167,27 @@ void IsolateData::CancelPauseOnNextStatement(int context_group_id) {
}
}
void IsolateData::AsyncTaskScheduled(const v8_inspector::StringView& name,
void* task, bool recurring) {
inspector_->asyncTaskScheduled(name, task, recurring);
}
void IsolateData::AsyncTaskStarted(void* task) {
inspector_->asyncTaskStarted(task);
}
void IsolateData::AsyncTaskFinished(void* task) {
inspector_->asyncTaskFinished(task);
}
void IsolateData::SetMaxAsyncTaskStacksForTest(int limit) {
v8_inspector::SetMaxAsyncTaskStacksForTest(inspector_.get(), limit);
}
void IsolateData::DumpAsyncTaskStacksStateForTest() {
v8_inspector::DumpAsyncTaskStacksStateForTest(inspector_.get());
}
// static
void IsolateData::MessageHandler(v8::Local<v8::Message> message,
v8::Local<v8::Value> exception) {
......
......@@ -26,19 +26,11 @@ class IsolateData : public v8_inspector::V8InspectorClient {
};
using SetupGlobalTasks = std::vector<std::unique_ptr<SetupGlobalTask>>;
class FrontendChannel {
public:
virtual ~FrontendChannel() = default;
virtual void SendMessageToFrontend(
int session_id, const v8_inspector::StringView& message) = 0;
};
IsolateData(TaskRunner* task_runner, SetupGlobalTasks setup_global_tasks,
v8::StartupData* startup_data, FrontendChannel* channel);
v8::StartupData* startup_data, bool with_inspector);
static IsolateData* FromContext(v8::Local<v8::Context> context);
v8::Isolate* isolate() const { return isolate_; }
v8_inspector::V8Inspector* inspector() const { return inspector_.get(); }
TaskRunner* task_runner() const { return task_runner_; }
// Setting things up.
......@@ -51,7 +43,8 @@ class IsolateData : public v8_inspector::V8InspectorClient {
// Working with V8Inspector api.
int ConnectSession(int context_group_id,
const v8_inspector::StringView& state);
const v8_inspector::StringView& state,
v8_inspector::V8Inspector::Channel* channel);
std::unique_ptr<v8_inspector::StringBuffer> DisconnectSession(int session_id);
void SendMessage(int session_id, const v8_inspector::StringView& message);
void BreakProgram(int context_group_id,
......@@ -61,11 +54,17 @@ class IsolateData : public v8_inspector::V8InspectorClient {
const v8_inspector::StringView& reason,
const v8_inspector::StringView& details);
void CancelPauseOnNextStatement(int context_group_id);
void AsyncTaskScheduled(const v8_inspector::StringView& name, void* task,
bool recurring);
void AsyncTaskStarted(void* task);
void AsyncTaskFinished(void* task);
// Test utilities.
void SetCurrentTimeMS(double time);
void SetMemoryInfo(v8::Local<v8::Value> memory_info);
void SetLogConsoleApiMessageCalls(bool log);
void SetMaxAsyncTaskStacksForTest(int limit);
void DumpAsyncTaskStacksStateForTest();
void FireContextCreated(v8::Local<v8::Context> context, int context_group_id);
void FireContextDestroyed(v8::Local<v8::Context> context);
......@@ -114,9 +113,7 @@ class IsolateData : public v8_inspector::V8InspectorClient {
int last_session_id_ = 0;
std::map<int, std::unique_ptr<v8_inspector::V8InspectorSession>> sessions_;
std::map<v8_inspector::V8InspectorSession*, int> context_group_by_session_;
std::map<int, std::unique_ptr<v8_inspector::V8Inspector::Channel>> channels_;
v8::Global<v8::Value> memory_info_;
FrontendChannel* frontend_channel_;
bool current_time_set_ = false;
double current_time_ = 0.0;
bool log_console_api_message_calls_ = false;
......
......@@ -26,24 +26,16 @@ void ReportUncaughtException(v8::Isolate* isolate,
source_line.data(), line);
}
v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) {
v8::internal::Vector<uint16_t> buffer =
v8::internal::Vector<uint16_t>::New(str->Length());
str->Write(buffer.start(), 0, str->Length());
return buffer;
}
} // namespace
TaskRunner::TaskRunner(IsolateData::SetupGlobalTasks setup_global_tasks,
bool catch_exceptions,
v8::base::Semaphore* ready_semaphore,
v8::StartupData* startup_data,
IsolateData::FrontendChannel* channel)
v8::StartupData* startup_data, bool with_inspector)
: Thread(Options("Task Runner")),
setup_global_tasks_(std::move(setup_global_tasks)),
startup_data_(startup_data),
channel_(channel),
with_inspector_(with_inspector),
catch_exceptions_(catch_exceptions),
ready_semaphore_(ready_semaphore),
data_(nullptr),
......@@ -56,7 +48,7 @@ TaskRunner::~TaskRunner() { Join(); }
void TaskRunner::Run() {
data_.reset(new IsolateData(this, std::move(setup_global_tasks_),
startup_data_, channel_));
startup_data_, with_inspector_));
if (ready_semaphore_) ready_semaphore_->Signal();
RunMessageLoop(false);
}
......@@ -69,7 +61,7 @@ void TaskRunner::RunMessageLoop(bool only_protocol) {
v8::Isolate::Scope isolate_scope(isolate());
if (catch_exceptions_) {
v8::TryCatch try_catch(isolate());
task->RunOnIsolate(data_.get());
task->Run(data_.get());
delete task;
if (try_catch.HasCaught()) {
ReportUncaughtException(isolate(), try_catch);
......@@ -78,7 +70,7 @@ void TaskRunner::RunMessageLoop(bool only_protocol) {
_exit(0);
}
} else {
task->RunOnIsolate(data_.get());
task->Run(data_.get());
delete task;
}
}
......@@ -105,7 +97,7 @@ TaskRunner::Task* TaskRunner::GetNext(bool only_protocol) {
if (only_protocol) {
Task* task = nullptr;
if (queue_.Dequeue(&task)) {
if (task->is_inspector_task()) return task;
if (task->is_priority_task()) return task;
deffered_queue_.Enqueue(task);
}
} else {
......@@ -117,86 +109,3 @@ TaskRunner::Task* TaskRunner::GetNext(bool only_protocol) {
}
return nullptr;
}
AsyncTask::AsyncTask(IsolateData* data, const char* task_name)
: instrumenting_(data && task_name) {
if (!instrumenting_) return;
data->inspector()->asyncTaskScheduled(
v8_inspector::StringView(reinterpret_cast<const uint8_t*>(task_name),
strlen(task_name)),
this, false);
}
void AsyncTask::Run() {
if (instrumenting_) data()->inspector()->asyncTaskStarted(this);
AsyncRun();
if (instrumenting_) data()->inspector()->asyncTaskFinished(this);
}
ExecuteStringTask::ExecuteStringTask(
IsolateData* data, int context_group_id, const char* task_name,
const v8::internal::Vector<uint16_t>& expression,
v8::Local<v8::String> name, v8::Local<v8::Integer> line_offset,
v8::Local<v8::Integer> column_offset, v8::Local<v8::Boolean> is_module)
: AsyncTask(data, task_name),
expression_(expression),
name_(ToVector(name)),
line_offset_(line_offset.As<v8::Int32>()->Value()),
column_offset_(column_offset.As<v8::Int32>()->Value()),
is_module_(is_module->Value()),
context_group_id_(context_group_id) {}
ExecuteStringTask::ExecuteStringTask(
const v8::internal::Vector<const char>& expression, int context_group_id)
: AsyncTask(nullptr, nullptr),
expression_utf8_(expression),
context_group_id_(context_group_id) {}
void ExecuteStringTask::AsyncRun() {
v8::MicrotasksScope microtasks_scope(isolate(),
v8::MicrotasksScope::kRunMicrotasks);
v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = data()->GetContext(context_group_id_);
v8::Context::Scope context_scope(context);
v8::Local<v8::String> name =
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> column_offset =
v8::Integer::New(isolate(), column_offset_);
v8::ScriptOrigin origin(
name, line_offset, column_offset,
/* resource_is_shared_cross_origin */ v8::Local<v8::Boolean>(),
/* script_id */ v8::Local<v8::Integer>(),
/* 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::Local<v8::String> source;
if (expression_.length()) {
source = v8::String::NewFromTwoByte(isolate(), expression_.start(),
v8::NewStringType::kNormal,
expression_.length())
.ToLocalChecked();
} else {
source = v8::String::NewFromUtf8(isolate(), expression_utf8_.start(),
v8::NewStringType::kNormal,
expression_utf8_.length())
.ToLocalChecked();
}
v8::ScriptCompiler::Source scriptSource(source, origin);
if (!is_module_) {
v8::Local<v8::Script> script;
if (!v8::ScriptCompiler::Compile(context, &scriptSource).ToLocal(&script))
return;
v8::MaybeLocal<v8::Value> result;
result = script->Run(context);
} else {
data()->RegisterModule(context, name_, &scriptSource);
}
}
......@@ -22,26 +22,13 @@ class TaskRunner : public v8::base::Thread {
class Task {
public:
virtual ~Task() {}
virtual bool is_inspector_task() = 0;
void RunOnIsolate(IsolateData* data) {
data_ = data;
Run();
data_ = nullptr;
}
protected:
virtual void Run() = 0;
v8::Isolate* isolate() const { return data_->isolate(); }
IsolateData* data() const { return data_; }
private:
IsolateData* data_ = nullptr;
virtual bool is_priority_task() = 0;
virtual void Run(IsolateData* data) = 0;
};
TaskRunner(IsolateData::SetupGlobalTasks setup_global_tasks,
bool catch_exceptions, v8::base::Semaphore* ready_semaphore,
v8::StartupData* startup_data,
IsolateData::FrontendChannel* channel);
v8::StartupData* startup_data, bool with_inspector);
virtual ~TaskRunner();
IsolateData* data() const { return data_.get(); }
......@@ -63,7 +50,7 @@ class TaskRunner : public v8::base::Thread {
IsolateData::SetupGlobalTasks setup_global_tasks_;
v8::StartupData* startup_data_;
IsolateData::FrontendChannel* channel_;
bool with_inspector_;
bool catch_exceptions_;
v8::base::Semaphore* ready_semaphore_;
std::unique_ptr<IsolateData> data_;
......@@ -82,43 +69,4 @@ class TaskRunner : public v8::base::Thread {
DISALLOW_COPY_AND_ASSIGN(TaskRunner);
};
class AsyncTask : public TaskRunner::Task {
public:
AsyncTask(IsolateData* data, const char* task_name);
virtual ~AsyncTask() = default;
protected:
virtual void AsyncRun() = 0;
void Run() override;
bool instrumenting_;
};
class ExecuteStringTask : public AsyncTask {
public:
ExecuteStringTask(IsolateData* data, int context_group_id,
const char* task_name,
const v8::internal::Vector<uint16_t>& expression,
v8::Local<v8::String> name,
v8::Local<v8::Integer> line_offset,
v8::Local<v8::Integer> column_offset,
v8::Local<v8::Boolean> is_module);
ExecuteStringTask(const v8::internal::Vector<const char>& expression,
int context_group_id);
bool is_inspector_task() override { return false; }
private:
void AsyncRun() override;
v8::internal::Vector<uint16_t> expression_;
v8::internal::Vector<const char> expression_utf8_;
v8::internal::Vector<uint16_t> name_;
int32_t line_offset_ = 0;
int32_t column_offset_ = 0;
bool is_module_ = false;
int context_group_id_;
DISALLOW_COPY_AND_ASSIGN(ExecuteStringTask);
};
#endif // V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_
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