| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_ | 5 #ifndef V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_ |
| 6 #define V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_ | 6 #define V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "include/v8-inspector.h" | 10 #include "include/v8-inspector.h" |
| 11 #include "include/v8-platform.h" | 11 #include "include/v8-platform.h" |
| 12 #include "include/v8.h" | 12 #include "include/v8.h" |
| 13 #include "src/base/atomic-utils.h" | 13 #include "src/base/atomic-utils.h" |
| 14 #include "src/base/macros.h" | 14 #include "src/base/macros.h" |
| 15 #include "src/base/platform/platform.h" | 15 #include "src/base/platform/platform.h" |
| 16 #include "src/locked-queue-inl.h" | 16 #include "src/locked-queue-inl.h" |
| 17 #include "src/vector.h" | 17 #include "src/vector.h" |
| 18 | 18 |
| 19 struct VectorCompare { | 19 class TaskRunner; |
| 20 bool operator()(const v8::internal::Vector<uint16_t>& lhs, | 20 |
| 21 const v8::internal::Vector<uint16_t>& rhs) const { | 21 class IsolateData { |
| 22 for (int i = 0; i < lhs.length() && i < rhs.length(); ++i) { | 22 public: |
| 23 if (lhs[i] != rhs[i]) return lhs[i] < rhs[i]; | 23 class SetupGlobalTask { |
| 24 public: |
| 25 virtual ~SetupGlobalTask() = default; |
| 26 virtual void Run(v8::Isolate* isolate, |
| 27 v8::Local<v8::ObjectTemplate> global) = 0; |
| 28 }; |
| 29 using SetupGlobalTasks = std::vector<std::unique_ptr<SetupGlobalTask>>; |
| 30 |
| 31 IsolateData(TaskRunner* task_runner, SetupGlobalTasks setup_global_tasks, |
| 32 v8::StartupData* startup_data); |
| 33 static IsolateData* FromContext(v8::Local<v8::Context> context); |
| 34 |
| 35 v8::Isolate* isolate() const { return isolate_; } |
| 36 TaskRunner* task_runner() const { return task_runner_; } |
| 37 int CreateContextGroup(); |
| 38 v8::Local<v8::Context> GetContext(int context_group_id); |
| 39 void RegisterModule(v8::Local<v8::Context> context, |
| 40 v8::internal::Vector<uint16_t> name, |
| 41 v8::ScriptCompiler::Source* source); |
| 42 |
| 43 private: |
| 44 struct VectorCompare { |
| 45 bool operator()(const v8::internal::Vector<uint16_t>& lhs, |
| 46 const v8::internal::Vector<uint16_t>& rhs) const { |
| 47 for (int i = 0; i < lhs.length() && i < rhs.length(); ++i) { |
| 48 if (lhs[i] != rhs[i]) return lhs[i] < rhs[i]; |
| 49 } |
| 50 return false; |
| 24 } | 51 } |
| 25 return false; | 52 }; |
| 26 } | 53 static v8::MaybeLocal<v8::Module> ModuleResolveCallback( |
| 54 v8::Local<v8::Context> context, v8::Local<v8::String> specifier, |
| 55 v8::Local<v8::Module> referrer); |
| 56 |
| 57 TaskRunner* task_runner_; |
| 58 SetupGlobalTasks setup_global_tasks_; |
| 59 v8::Isolate* isolate_; |
| 60 int last_context_group_id_ = 0; |
| 61 std::map<int, v8::Global<v8::Context>> contexts_; |
| 62 std::map<v8::internal::Vector<uint16_t>, v8::Global<v8::Module>, |
| 63 VectorCompare> |
| 64 modules_; |
| 27 }; | 65 }; |
| 28 | 66 |
| 29 class TaskRunner : public v8::base::Thread { | 67 class TaskRunner : public v8::base::Thread { |
| 30 public: | 68 public: |
| 31 class Task { | 69 class Task { |
| 32 public: | 70 public: |
| 33 virtual ~Task() {} | 71 virtual ~Task() {} |
| 34 virtual bool is_inspector_task() = 0; | 72 virtual bool is_inspector_task() = 0; |
| 35 void RunOnTaskRunner(TaskRunner* task_runner) { | 73 void RunOnTaskRunner(TaskRunner* task_runner) { |
| 36 task_runner_ = task_runner; | 74 task_runner_ = task_runner; |
| 37 Run(); | 75 Run(); |
| 38 task_runner_ = nullptr; | 76 task_runner_ = nullptr; |
| 39 } | 77 } |
| 40 | 78 |
| 41 protected: | 79 protected: |
| 42 virtual void Run() = 0; | 80 virtual void Run() = 0; |
| 43 v8::Isolate* isolate() const { return task_runner_->isolate_; } | 81 v8::Isolate* isolate() const { return task_runner_->data_->isolate(); } |
| 44 v8::Local<v8::Context> default_context() const { | 82 v8::Local<v8::Context> default_context() const { |
| 45 return task_runner_->contexts_.begin()->second.Get(isolate()); | 83 return task_runner_->data_->GetContext( |
| 84 task_runner_->default_context_group_id_); |
| 46 } | 85 } |
| 47 | 86 |
| 48 private: | 87 private: |
| 49 TaskRunner* task_runner_ = nullptr; | 88 TaskRunner* task_runner_ = nullptr; |
| 50 }; | 89 }; |
| 51 | 90 |
| 52 class SetupGlobalTask { | 91 TaskRunner(IsolateData::SetupGlobalTasks setup_global_tasks, |
| 53 public: | 92 bool catch_exceptions, v8::base::Semaphore* ready_semaphore, |
| 54 virtual ~SetupGlobalTask() = default; | |
| 55 virtual void Run(v8::Isolate* isolate, | |
| 56 v8::Local<v8::ObjectTemplate> global) = 0; | |
| 57 }; | |
| 58 using SetupGlobalTasks = std::vector<std::unique_ptr<SetupGlobalTask>>; | |
| 59 | |
| 60 TaskRunner(SetupGlobalTasks setup_global_tasks, bool catch_exceptions, | |
| 61 v8::base::Semaphore* ready_semaphore, | |
| 62 v8::StartupData* startup_data); | 93 v8::StartupData* startup_data); |
| 63 virtual ~TaskRunner(); | 94 virtual ~TaskRunner(); |
| 95 IsolateData* data() const { return data_.get(); } |
| 96 int default_context_group_id() const { return default_context_group_id_; } |
| 64 | 97 |
| 65 // Thread implementation. | 98 // Thread implementation. |
| 66 void Run() override; | 99 void Run() override; |
| 67 | 100 |
| 68 // Should be called from the same thread and only from task. | 101 // Should be called from the same thread and only from task. |
| 69 void RunMessageLoop(bool only_protocol); | 102 void RunMessageLoop(bool only_protocol); |
| 70 void QuitMessageLoop(); | 103 void QuitMessageLoop(); |
| 71 | 104 |
| 72 // TaskRunner takes ownership. | 105 // TaskRunner takes ownership. |
| 73 void Append(Task* task); | 106 void Append(Task* task); |
| 74 | 107 |
| 75 static TaskRunner* FromContext(v8::Local<v8::Context>); | |
| 76 | |
| 77 v8::Local<v8::Context> NewContextGroup( | |
| 78 const SetupGlobalTasks& setup_global_tasks); | |
| 79 v8::Local<v8::Context> GetContext(int context_group_id); | |
| 80 static int GetContextGroupId(v8::Local<v8::Context> context); | |
| 81 | |
| 82 void Terminate(); | 108 void Terminate(); |
| 83 | 109 |
| 84 void RegisterModule(v8::internal::Vector<uint16_t> name, | 110 private: |
| 85 v8::Local<v8::Module> module); | 111 Task* GetNext(bool only_protocol); |
| 86 static v8::MaybeLocal<v8::Module> ModuleResolveCallback( | 112 v8::Isolate* isolate() const { return data_->isolate(); } |
| 87 v8::Local<v8::Context> context, v8::Local<v8::String> specifier, | |
| 88 v8::Local<v8::Module> referrer); | |
| 89 | 113 |
| 90 private: | 114 IsolateData::SetupGlobalTasks setup_global_tasks_; |
| 91 void InitializeIsolate(); | |
| 92 Task* GetNext(bool only_protocol); | |
| 93 | |
| 94 SetupGlobalTasks setup_global_tasks_; | |
| 95 v8::StartupData* startup_data_; | 115 v8::StartupData* startup_data_; |
| 96 bool catch_exceptions_; | 116 bool catch_exceptions_; |
| 97 v8::base::Semaphore* ready_semaphore_; | 117 v8::base::Semaphore* ready_semaphore_; |
| 98 | 118 std::unique_ptr<IsolateData> data_; |
| 99 v8::Isolate* isolate_; | 119 int default_context_group_id_; |
| 100 intptr_t last_context_group_id_ = 0; | |
| 101 std::map<intptr_t, v8::Global<v8::Context>> contexts_; | |
| 102 | 120 |
| 103 // deferred_queue_ combined with queue_ (in this order) have all tasks in the | 121 // deferred_queue_ combined with queue_ (in this order) have all tasks in the |
| 104 // correct order. Sometimes we skip non-protocol tasks by moving them from | 122 // correct order. Sometimes we skip non-protocol tasks by moving them from |
| 105 // queue_ to deferred_queue_. | 123 // queue_ to deferred_queue_. |
| 106 v8::internal::LockedQueue<Task*> queue_; | 124 v8::internal::LockedQueue<Task*> queue_; |
| 107 v8::internal::LockedQueue<Task*> deffered_queue_; | 125 v8::internal::LockedQueue<Task*> deffered_queue_; |
| 108 v8::base::Semaphore process_queue_semaphore_; | 126 v8::base::Semaphore process_queue_semaphore_; |
| 109 | 127 |
| 110 std::map<v8::internal::Vector<uint16_t>, v8::Global<v8::Module>, | |
| 111 VectorCompare> | |
| 112 modules_; | |
| 113 | |
| 114 int nested_loop_count_; | 128 int nested_loop_count_; |
| 115 | 129 |
| 116 v8::base::AtomicNumber<int> is_terminated_; | 130 v8::base::AtomicNumber<int> is_terminated_; |
| 117 | 131 |
| 118 DISALLOW_COPY_AND_ASSIGN(TaskRunner); | 132 DISALLOW_COPY_AND_ASSIGN(TaskRunner); |
| 119 }; | 133 }; |
| 120 | 134 |
| 121 class AsyncTask : public TaskRunner::Task { | 135 class AsyncTask : public TaskRunner::Task { |
| 122 public: | 136 public: |
| 123 AsyncTask(const char* task_name, v8_inspector::V8Inspector* inspector); | 137 AsyncTask(const char* task_name, v8_inspector::V8Inspector* inspector); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 149 v8::internal::Vector<const char> expression_utf8_; | 163 v8::internal::Vector<const char> expression_utf8_; |
| 150 v8::internal::Vector<uint16_t> name_; | 164 v8::internal::Vector<uint16_t> name_; |
| 151 int32_t line_offset_ = 0; | 165 int32_t line_offset_ = 0; |
| 152 int32_t column_offset_ = 0; | 166 int32_t column_offset_ = 0; |
| 153 bool is_module_ = false; | 167 bool is_module_ = false; |
| 154 | 168 |
| 155 DISALLOW_COPY_AND_ASSIGN(ExecuteStringTask); | 169 DISALLOW_COPY_AND_ASSIGN(ExecuteStringTask); |
| 156 }; | 170 }; |
| 157 | 171 |
| 158 #endif // V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_ | 172 #endif // V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_ |
| OLD | NEW |