2016-09-30 15:52:31 +00:00
|
|
|
// Copyright 2016 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_TASK_RUNNER_H_
|
|
|
|
#define V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_
|
|
|
|
|
2017-01-31 00:19:41 +00:00
|
|
|
#include <map>
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
#include "include/v8-inspector.h"
|
|
|
|
#include "include/v8-platform.h"
|
|
|
|
#include "include/v8.h"
|
2016-10-19 02:04:34 +00:00
|
|
|
#include "src/base/atomic-utils.h"
|
2016-09-30 15:52:31 +00:00
|
|
|
#include "src/base/macros.h"
|
|
|
|
#include "src/base/platform/platform.h"
|
|
|
|
#include "src/locked-queue-inl.h"
|
2016-10-07 21:16:41 +00:00
|
|
|
#include "src/vector.h"
|
2016-09-30 15:52:31 +00:00
|
|
|
|
2017-01-31 00:19:41 +00:00
|
|
|
struct VectorCompare {
|
|
|
|
bool operator()(const v8::internal::Vector<uint16_t>& lhs,
|
|
|
|
const v8::internal::Vector<uint16_t>& 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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
class TaskRunner : public v8::base::Thread {
|
|
|
|
public:
|
|
|
|
class Task {
|
|
|
|
public:
|
|
|
|
virtual ~Task() {}
|
|
|
|
virtual bool is_inspector_task() = 0;
|
|
|
|
virtual void Run(v8::Isolate* isolate,
|
|
|
|
const v8::Global<v8::Context>& context) = 0;
|
|
|
|
};
|
|
|
|
|
2016-10-05 15:07:57 +00:00
|
|
|
TaskRunner(v8::ExtensionConfiguration* extensions, bool catch_exceptions,
|
|
|
|
v8::base::Semaphore* ready_semaphore);
|
2016-09-30 15:52:31 +00:00
|
|
|
virtual ~TaskRunner();
|
|
|
|
|
|
|
|
// Thread implementation.
|
|
|
|
void Run() override;
|
|
|
|
|
|
|
|
// Should be called from the same thread and only from task.
|
|
|
|
void RunMessageLoop(bool only_protocol);
|
|
|
|
void QuitMessageLoop();
|
|
|
|
|
|
|
|
// TaskRunner takes ownership.
|
|
|
|
void Append(Task* task);
|
|
|
|
|
|
|
|
static TaskRunner* FromContext(v8::Local<v8::Context>);
|
|
|
|
|
2017-03-07 22:30:05 +00:00
|
|
|
v8::Local<v8::Context> NewContextGroup();
|
|
|
|
v8::Local<v8::Context> GetContext(int context_group_id);
|
|
|
|
static int GetContextGroupId(v8::Local<v8::Context> context);
|
|
|
|
|
2016-10-19 02:04:34 +00:00
|
|
|
void Terminate();
|
|
|
|
|
2017-01-31 00:19:41 +00:00
|
|
|
void RegisterModule(v8::internal::Vector<uint16_t> name,
|
|
|
|
v8::Local<v8::Module> module);
|
|
|
|
static v8::MaybeLocal<v8::Module> ModuleResolveCallback(
|
|
|
|
v8::Local<v8::Context> context, v8::Local<v8::String> specifier,
|
|
|
|
v8::Local<v8::Module> referrer);
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
private:
|
2017-03-07 22:30:05 +00:00
|
|
|
void InitializeIsolate();
|
2016-09-30 15:52:31 +00:00
|
|
|
Task* GetNext(bool only_protocol);
|
|
|
|
|
|
|
|
v8::ExtensionConfiguration* extensions_;
|
2016-10-05 15:07:57 +00:00
|
|
|
bool catch_exceptions_;
|
2016-09-30 15:52:31 +00:00
|
|
|
v8::base::Semaphore* ready_semaphore_;
|
|
|
|
|
|
|
|
v8::Isolate* isolate_;
|
2017-03-07 22:30:05 +00:00
|
|
|
intptr_t last_context_group_id_ = 0;
|
|
|
|
std::map<intptr_t, v8::Global<v8::Context>> contexts_;
|
2016-09-30 15:52:31 +00:00
|
|
|
|
|
|
|
// deferred_queue_ combined with queue_ (in this order) have all tasks in the
|
2016-10-19 02:04:34 +00:00
|
|
|
// correct order. Sometimes we skip non-protocol tasks by moving them from
|
|
|
|
// queue_ to deferred_queue_.
|
2016-09-30 15:52:31 +00:00
|
|
|
v8::internal::LockedQueue<Task*> queue_;
|
|
|
|
v8::internal::LockedQueue<Task*> deffered_queue_;
|
|
|
|
v8::base::Semaphore process_queue_semaphore_;
|
|
|
|
|
2017-01-31 00:19:41 +00:00
|
|
|
std::map<v8::internal::Vector<uint16_t>, v8::Global<v8::Module>,
|
|
|
|
VectorCompare>
|
|
|
|
modules_;
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
int nested_loop_count_;
|
|
|
|
|
2016-10-19 02:04:34 +00:00
|
|
|
v8::base::AtomicNumber<int> is_terminated_;
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(TaskRunner);
|
|
|
|
};
|
|
|
|
|
2016-12-13 19:40:14 +00:00
|
|
|
class AsyncTask : public TaskRunner::Task {
|
|
|
|
public:
|
|
|
|
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;
|
|
|
|
|
2017-02-03 07:09:11 +00:00
|
|
|
protected:
|
2016-12-13 19:40:14 +00:00
|
|
|
v8_inspector::V8Inspector* inspector_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ExecuteStringTask : public AsyncTask {
|
2016-09-30 15:52:31 +00:00
|
|
|
public:
|
2016-11-04 19:59:11 +00:00
|
|
|
ExecuteStringTask(const v8::internal::Vector<uint16_t>& expression,
|
|
|
|
v8::Local<v8::String> name,
|
|
|
|
v8::Local<v8::Integer> line_offset,
|
2017-01-31 00:19:41 +00:00
|
|
|
v8::Local<v8::Integer> column_offset,
|
|
|
|
v8::Local<v8::Boolean> is_module, const char* task_name,
|
2016-12-13 19:40:14 +00:00
|
|
|
v8_inspector::V8Inspector* inspector);
|
2016-10-07 21:16:41 +00:00
|
|
|
explicit ExecuteStringTask(
|
|
|
|
const v8::internal::Vector<const char>& expression);
|
2016-09-30 15:52:31 +00:00
|
|
|
bool is_inspector_task() override { return false; }
|
|
|
|
|
2016-12-13 19:40:14 +00:00
|
|
|
void AsyncRun(v8::Isolate* isolate,
|
|
|
|
const v8::Global<v8::Context>& context) override;
|
2016-09-30 15:52:31 +00:00
|
|
|
|
|
|
|
private:
|
2016-10-07 21:16:41 +00:00
|
|
|
v8::internal::Vector<uint16_t> expression_;
|
|
|
|
v8::internal::Vector<const char> expression_utf8_;
|
2016-11-04 19:59:11 +00:00
|
|
|
v8::internal::Vector<uint16_t> name_;
|
2017-01-31 00:19:41 +00:00
|
|
|
int32_t line_offset_ = 0;
|
|
|
|
int32_t column_offset_ = 0;
|
|
|
|
bool is_module_ = false;
|
2016-09-30 15:52:31 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ExecuteStringTask);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // V8_TEST_INSPECTOR_PROTOCOL_TASK_RUNNER_H_
|