Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1269)

Unified Diff: test/inspector-protocol/task-runner.cc

Issue 2361623006: [inspector] added inspector test runner [part 1] (Closed)
Patch Set: addressed comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/inspector-protocol/task-runner.cc
diff --git a/test/inspector-protocol/task-runner.cc b/test/inspector-protocol/task-runner.cc
new file mode 100644
index 0000000000000000000000000000000000000000..39216e4b25de5e5a4c5182729bbc053d69f50ec5
--- /dev/null
+++ b/test/inspector-protocol/task-runner.cc
@@ -0,0 +1,153 @@
+// 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.
+
+#include "test/inspector-protocol/task-runner.h"
+
+namespace {
+
+const int kTaskRunnerIndex = 2;
+
+class TaskScope {
+ public:
+ TaskScope(v8::Isolate* isolate, const v8::Global<v8::Context>& context);
+ ~TaskScope();
+
+ v8::Local<v8::Context> context() { return context_; }
+
+ private:
+ v8::Isolate* isolate_;
+ v8::Isolate::Scope isolate_scope_;
+ v8::HandleScope handle_scope_;
+ v8::Local<v8::Context> context_;
+ v8::Context::Scope context_scope_;
+ v8::MicrotasksScope microtasks_scope_;
+ v8::TryCatch try_catch_;
+
+ DISALLOW_COPY_AND_ASSIGN(TaskScope);
+};
+
+TaskScope::TaskScope(v8::Isolate* isolate,
+ const v8::Global<v8::Context>& context)
+ : isolate_(isolate),
+ isolate_scope_(isolate),
+ handle_scope_(isolate),
+ context_(context.Get(isolate)),
+ context_scope_(context_),
+ microtasks_scope_(isolate_, v8::MicrotasksScope::kRunMicrotasks),
+ try_catch_(isolate_) {}
+
+TaskScope::~TaskScope() {
+ if (try_catch_.HasCaught()) {
+ std::string message = *v8::String::Utf8Value(try_catch_.Message()->Get());
+ fprintf(stderr, "Unhandle exception: %s\n", message.data());
+ fflush(stdout);
+ fflush(stderr);
+ _exit(0);
+ }
+}
+
+} // namespace
+
+TaskRunner::TaskRunner(v8::ExtensionConfiguration* extensions,
+ v8::base::Semaphore* ready)
+ : Thread(Options("Task Runner")),
+ extensions_(extensions),
+ ready_(ready),
+ isolate_(nullptr),
+ process_queue_semaphore_(0),
+ nested_loops_(0) {
+ Start();
+}
+
+TaskRunner::~TaskRunner() { Join(); }
+
+void TaskRunner::InitializeContext() {
+ v8::Isolate::CreateParams params;
+ params.array_buffer_allocator =
+ v8::ArrayBuffer::Allocator::NewDefaultAllocator();
+ isolate_ = v8::Isolate::New(params);
+ isolate_->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped);
+ v8::Isolate::Scope isolate_scope(isolate_);
+ v8::HandleScope handle_scope(isolate_);
+
+ v8::Local<v8::ObjectTemplate> global_template =
+ v8::ObjectTemplate::New(isolate_);
+ v8::Local<v8::Context> context =
+ v8::Context::New(isolate_, extensions_, global_template);
+ context->SetAlignedPointerInEmbedderData(kTaskRunnerIndex, this);
+ context_.Reset(isolate_, context);
+
+ if (ready_) ready_->Signal();
+}
+
+void TaskRunner::Run() {
+ InitializeContext();
+ RunMessageLoop(false);
+}
+
+void TaskRunner::RunMessageLoop(bool only_protocol) {
+ int loop_number = ++nested_loops_;
+ while (nested_loops_ == loop_number) {
+ TaskRunner::Task* task = GetNext(only_protocol);
+ TaskScope task_scope(isolate_, context_);
+ task->Run(task_scope.context());
+ delete task;
+ }
+}
+
+void TaskRunner::QuitMessageLoop() {
+ DCHECK(nested_loops_ > 0);
+ --nested_loops_;
+}
+
+void TaskRunner::Append(Task* task) {
+ queue_.Enqueue(task);
+ process_queue_semaphore_.Signal();
+}
+
+TaskRunner::Task* TaskRunner::GetNext(bool only_protocol) {
+ for (;;) {
+ if (only_protocol) {
+ for (;;) {
dgozman 2016/09/24 00:56:10 Let's remove this for()
kozy 2016/09/26 01:44:08 Done.
+ Task* task = nullptr;
+ if (!queue_.Dequeue(&task)) break;
+ if (task->is_protocol_task()) return task;
+ deffered_queue_.Enqueue(task);
+ }
+ } else {
+ Task* task = nullptr;
+ if (deffered_queue_.Dequeue(&task)) return task;
+ if (queue_.Dequeue(&task)) return task;
+ }
+ process_queue_semaphore_.Wait();
+ }
+}
+
+TaskRunner* TaskRunner::FromContext(v8::Local<v8::Context> context) {
+ return static_cast<TaskRunner*>(
+ context->GetAlignedPointerFromEmbedderData(kTaskRunnerIndex));
+}
+
+ExecuteStringTask::ExecuteStringTask(const std::string& expression)
+ : expression_(expression) {}
+
+void ExecuteStringTask::Run(v8::Local<v8::Context> context) {
+ v8::Isolate* isolate = context->GetIsolate();
+ v8::ScriptOrigin origin(v8::String::Empty(isolate));
+
+ v8::Local<v8::String> source =
dgozman 2016/09/24 00:56:09 HandleScope
kozy 2016/09/26 01:44:08 Done.
+ v8::String::NewFromUtf8(isolate, expression_.data(),
+ v8::NewStringType::kNormal)
+ .ToLocalChecked();
+
+ v8::ScriptCompiler::Source scriptSource(source, origin);
+ v8::Local<v8::Script> script;
+ if (!v8::ScriptCompiler::Compile(context, &scriptSource).ToLocal(&script))
dgozman 2016/09/24 00:56:09 ContextScope
kozy 2016/09/26 01:44:08 Done.
+ return;
+ v8::MaybeLocal<v8::Value> result;
+ result = script->Run(context);
+}
+
+// main to make compiler happy before other patch will be landed.
+int main(int argc, char* argv[]) { return 0; }

Powered by Google App Engine
This is Rietveld 408576698