diff --git a/test/inspector/BUILD.gn b/test/inspector/BUILD.gn index 5298c09a89..9c9774719e 100644 --- a/test/inspector/BUILD.gn +++ b/test/inspector/BUILD.gn @@ -11,6 +11,8 @@ v8_executable("inspector-test") { "inspector-impl.cc", "inspector-impl.h", "inspector-test.cc", + "isolate-data.cc", + "isolate-data.h", "task-runner.cc", "task-runner.h", ] diff --git a/test/inspector/inspector.gyp b/test/inspector/inspector.gyp index a5bdf64e09..26f44d4812 100644 --- a/test/inspector/inspector.gyp +++ b/test/inspector/inspector.gyp @@ -23,6 +23,8 @@ 'inspector-impl.cc', 'inspector-impl.h', 'inspector-test.cc', + 'isolate-data.cc', + 'isolate-data.h', 'task-runner.cc', 'task-runner.h', ], diff --git a/test/inspector/isolate-data.cc b/test/inspector/isolate-data.cc new file mode 100644 index 0000000000..06ba710b3e --- /dev/null +++ b/test/inspector/isolate-data.cc @@ -0,0 +1,79 @@ +// Copyright 2017 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "test/inspector/isolate-data.h" + +#include "test/inspector/inspector-impl.h" +#include "test/inspector/task-runner.h" + +namespace { + +const int kIsolateDataIndex = 2; + +v8::internal::Vector ToVector(v8::Local str) { + v8::internal::Vector buffer = + v8::internal::Vector::New(str->Length()); + str->Write(buffer.start(), 0, str->Length()); + return buffer; +} + +} // namespace + +IsolateData::IsolateData(TaskRunner* task_runner, + IsolateData::SetupGlobalTasks setup_global_tasks, + v8::StartupData* startup_data) + : task_runner_(task_runner), + 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); +} + +IsolateData* IsolateData::FromContext(v8::Local context) { + return static_cast( + context->GetAlignedPointerFromEmbedderData(kIsolateDataIndex)); +} + +int IsolateData::CreateContextGroup() { + v8::Local global_template = + v8::ObjectTemplate::New(isolate_); + for (auto it = setup_global_tasks_.begin(); it != setup_global_tasks_.end(); + ++it) { + (*it)->Run(isolate_, global_template); + } + v8::Local context = + v8::Context::New(isolate_, nullptr, global_template); + context->SetAlignedPointerInEmbedderData(kIsolateDataIndex, this); + int context_group_id = ++last_context_group_id_; + contexts_[context_group_id].Reset(isolate_, context); + return context_group_id; +} + +v8::Local IsolateData::GetContext(int context_group_id) { + return contexts_[context_group_id].Get(isolate_); +} + +void IsolateData::RegisterModule(v8::Local context, + v8::internal::Vector name, + v8::ScriptCompiler::Source* source) { + v8::Local module; + if (!v8::ScriptCompiler::CompileModule(isolate(), source).ToLocal(&module)) + return; + if (!module->Instantiate(context, &IsolateData::ModuleResolveCallback)) + return; + v8::Local result; + if (!module->Evaluate(context).ToLocal(&result)) return; + modules_[name] = v8::Global(isolate_, module); +} + +v8::MaybeLocal IsolateData::ModuleResolveCallback( + v8::Local context, v8::Local specifier, + v8::Local referrer) { + std::string str = *v8::String::Utf8Value(specifier); + IsolateData* data = IsolateData::FromContext(context); + return data->modules_[ToVector(specifier)].Get(data->isolate_); +} diff --git a/test/inspector/isolate-data.h b/test/inspector/isolate-data.h new file mode 100644 index 0000000000..8a5555d051 --- /dev/null +++ b/test/inspector/isolate-data.h @@ -0,0 +1,62 @@ +// Copyright 2017 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_TEST_INSPECTOR_PROTOCOL_ISOLATE_DATA_H_ +#define V8_TEST_INSPECTOR_PROTOCOL_ISOLATE_DATA_H_ + +#include + +#include "include/v8-inspector.h" +#include "include/v8-platform.h" +#include "include/v8.h" +#include "src/vector.h" + +class TaskRunner; + +class IsolateData { + public: + class SetupGlobalTask { + public: + virtual ~SetupGlobalTask() = default; + virtual void Run(v8::Isolate* isolate, + v8::Local global) = 0; + }; + using SetupGlobalTasks = std::vector>; + + IsolateData(TaskRunner* task_runner, SetupGlobalTasks setup_global_tasks, + v8::StartupData* startup_data); + static IsolateData* FromContext(v8::Local context); + + v8::Isolate* isolate() const { return isolate_; } + TaskRunner* task_runner() const { return task_runner_; } + int CreateContextGroup(); + v8::Local GetContext(int context_group_id); + void RegisterModule(v8::Local context, + v8::internal::Vector name, + v8::ScriptCompiler::Source* source); + + private: + struct VectorCompare { + bool operator()(const v8::internal::Vector& lhs, + const v8::internal::Vector& rhs) const { + for (int i = 0; i < lhs.length() && i < rhs.length(); ++i) { + if (lhs[i] != rhs[i]) return lhs[i] < rhs[i]; + } + return false; + } + }; + static v8::MaybeLocal ModuleResolveCallback( + v8::Local context, v8::Local specifier, + v8::Local referrer); + + TaskRunner* task_runner_; + SetupGlobalTasks setup_global_tasks_; + v8::Isolate* isolate_; + int last_context_group_id_ = 0; + std::map> contexts_; + std::map, v8::Global, + VectorCompare> + modules_; +}; +#endif // V8_TEST_INSPECTOR_PROTOCOL_ISOLATE_DATA_H_ diff --git a/test/inspector/task-runner.cc b/test/inspector/task-runner.cc index 5f21b9cab2..b26680fdf6 100644 --- a/test/inspector/task-runner.cc +++ b/test/inspector/task-runner.cc @@ -12,8 +12,6 @@ namespace { -const int kIsolateDataIndex = 2; - void ReportUncaughtException(v8::Isolate* isolate, const v8::TryCatch& try_catch) { CHECK(try_catch.HasCaught()); @@ -31,64 +29,6 @@ v8::internal::Vector ToVector(v8::Local str) { } // namespace -IsolateData::IsolateData(TaskRunner* task_runner, - IsolateData::SetupGlobalTasks setup_global_tasks, - v8::StartupData* startup_data) - : task_runner_(task_runner), - 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); -} - -IsolateData* IsolateData::FromContext(v8::Local context) { - return static_cast( - context->GetAlignedPointerFromEmbedderData(kIsolateDataIndex)); -} - -int IsolateData::CreateContextGroup() { - v8::Local global_template = - v8::ObjectTemplate::New(isolate_); - for (auto it = setup_global_tasks_.begin(); it != setup_global_tasks_.end(); - ++it) { - (*it)->Run(isolate_, global_template); - } - v8::Local context = - v8::Context::New(isolate_, nullptr, global_template); - context->SetAlignedPointerInEmbedderData(kIsolateDataIndex, this); - int context_group_id = ++last_context_group_id_; - contexts_[context_group_id].Reset(isolate_, context); - return context_group_id; -} - -v8::Local IsolateData::GetContext(int context_group_id) { - return contexts_[context_group_id].Get(isolate_); -} - -void IsolateData::RegisterModule(v8::Local context, - v8::internal::Vector name, - v8::ScriptCompiler::Source* source) { - v8::Local module; - if (!v8::ScriptCompiler::CompileModule(isolate(), source).ToLocal(&module)) - return; - if (!module->Instantiate(context, &IsolateData::ModuleResolveCallback)) - return; - v8::Local result; - if (!module->Evaluate(context).ToLocal(&result)) return; - modules_[name] = v8::Global(isolate_, module); -} - -v8::MaybeLocal IsolateData::ModuleResolveCallback( - v8::Local context, v8::Local specifier, - v8::Local referrer) { - std::string str = *v8::String::Utf8Value(specifier); - IsolateData* data = IsolateData::FromContext(context); - return data->modules_[ToVector(specifier)].Get(data->isolate_); -} - TaskRunner::TaskRunner(IsolateData::SetupGlobalTasks setup_global_tasks, bool catch_exceptions, v8::base::Semaphore* ready_semaphore, diff --git a/test/inspector/task-runner.h b/test/inspector/task-runner.h index db94d8bd1a..2052c621d1 100644 --- a/test/inspector/task-runner.h +++ b/test/inspector/task-runner.h @@ -15,54 +15,7 @@ #include "src/base/platform/platform.h" #include "src/locked-queue-inl.h" #include "src/vector.h" - -class TaskRunner; - -class IsolateData { - public: - class SetupGlobalTask { - public: - virtual ~SetupGlobalTask() = default; - virtual void Run(v8::Isolate* isolate, - v8::Local global) = 0; - }; - using SetupGlobalTasks = std::vector>; - - IsolateData(TaskRunner* task_runner, SetupGlobalTasks setup_global_tasks, - v8::StartupData* startup_data); - static IsolateData* FromContext(v8::Local context); - - v8::Isolate* isolate() const { return isolate_; } - TaskRunner* task_runner() const { return task_runner_; } - int CreateContextGroup(); - v8::Local GetContext(int context_group_id); - void RegisterModule(v8::Local context, - v8::internal::Vector name, - v8::ScriptCompiler::Source* source); - - private: - struct VectorCompare { - bool operator()(const v8::internal::Vector& lhs, - const v8::internal::Vector& rhs) const { - for (int i = 0; i < lhs.length() && i < rhs.length(); ++i) { - if (lhs[i] != rhs[i]) return lhs[i] < rhs[i]; - } - return false; - } - }; - static v8::MaybeLocal ModuleResolveCallback( - v8::Local context, v8::Local specifier, - v8::Local referrer); - - TaskRunner* task_runner_; - SetupGlobalTasks setup_global_tasks_; - v8::Isolate* isolate_; - int last_context_group_id_ = 0; - std::map> contexts_; - std::map, v8::Global, - VectorCompare> - modules_; -}; +#include "test/inspector/isolate-data.h" class TaskRunner : public v8::base::Thread { public: