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

Side by Side Diff: test/inspector-protocol/task-runner.cc

Issue 2361623006: [inspector] added inspector test runner [part 1] (Closed)
Patch Set: use String16 in test-runner Created 4 years, 2 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "test/inspector-protocol/task-runner.h"
6
7 namespace {
8
9 const int kTaskRunnerIndex = 2;
10
11 void ReportUncaughtException(v8::Isolate* isolate,
12 const v8::TryCatch& try_catch) {
13 CHECK(try_catch.HasCaught());
14 v8::HandleScope handle_scope(isolate);
15 std::string message = *v8::String::Utf8Value(try_catch.Message()->Get());
16 fprintf(stderr, "Unhandle exception: %s\n", message.data());
17 }
18
19 v8_inspector::String16 ToString16(const v8_inspector::StringView& string) {
20 if (string.is8Bit())
21 return v8_inspector::String16(
22 reinterpret_cast<const char*>(string.characters8()), string.length());
23 return v8_inspector::String16(
24 reinterpret_cast<const uint16_t*>(string.characters16()),
25 string.length());
26 }
27
28 } // namespace
29
30 TaskRunner::TaskRunner(v8::ExtensionConfiguration* extensions,
31 v8::base::Semaphore* ready_semaphore)
32 : Thread(Options("Task Runner")),
33 extensions_(extensions),
34 ready_semaphore_(ready_semaphore),
35 isolate_(nullptr),
36 process_queue_semaphore_(0),
37 nested_loop_count_(0) {
38 Start();
39 }
40
41 TaskRunner::~TaskRunner() { Join(); }
42
43 void TaskRunner::InitializeContext() {
44 v8::Isolate::CreateParams params;
45 params.array_buffer_allocator =
46 v8::ArrayBuffer::Allocator::NewDefaultAllocator();
47 isolate_ = v8::Isolate::New(params);
48 isolate_->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped);
49 v8::Isolate::Scope isolate_scope(isolate_);
50 v8::HandleScope handle_scope(isolate_);
51
52 v8::Local<v8::ObjectTemplate> global_template =
53 v8::ObjectTemplate::New(isolate_);
54 v8::Local<v8::Context> context =
55 v8::Context::New(isolate_, extensions_, global_template);
56 context->SetAlignedPointerInEmbedderData(kTaskRunnerIndex, this);
57 context_.Reset(isolate_, context);
58
59 if (ready_semaphore_) ready_semaphore_->Signal();
60 }
61
62 void TaskRunner::Run() {
63 InitializeContext();
64 RunMessageLoop(false);
65 }
66
67 void TaskRunner::RunMessageLoop(bool only_protocol) {
68 int loop_number = ++nested_loop_count_;
69 while (nested_loop_count_ == loop_number) {
70 TaskRunner::Task* task = GetNext(only_protocol);
71 v8::Isolate::Scope isolate_scope(isolate_);
72 v8::TryCatch try_catch(isolate_);
73 task->Run(isolate_, context_);
74 delete task;
75 if (try_catch.HasCaught()) {
76 ReportUncaughtException(isolate_, try_catch);
77 fflush(stdout);
78 fflush(stderr);
79 _exit(0);
80 }
81 }
82 }
83
84 void TaskRunner::QuitMessageLoop() {
85 DCHECK(nested_loop_count_ > 0);
86 --nested_loop_count_;
87 }
88
89 void TaskRunner::Append(Task* task) {
90 queue_.Enqueue(task);
91 process_queue_semaphore_.Signal();
92 }
93
94 TaskRunner::Task* TaskRunner::GetNext(bool only_protocol) {
95 for (;;) {
96 if (only_protocol) {
97 Task* task = nullptr;
98 if (queue_.Dequeue(&task)) {
99 if (task->is_inspector_task()) return task;
100 deffered_queue_.Enqueue(task);
101 }
102 } else {
103 Task* task = nullptr;
104 if (deffered_queue_.Dequeue(&task)) return task;
105 if (queue_.Dequeue(&task)) return task;
106 }
107 process_queue_semaphore_.Wait();
108 }
109 UNREACHABLE();
110 return nullptr;
111 }
112
113 TaskRunner* TaskRunner::FromContext(v8::Local<v8::Context> context) {
114 return static_cast<TaskRunner*>(
115 context->GetAlignedPointerFromEmbedderData(kTaskRunnerIndex));
116 }
117
118 ExecuteStringTask::ExecuteStringTask(const v8_inspector::StringView& expression)
119 : expression_(ToString16(expression)) {}
120
121 void ExecuteStringTask::Run(v8::Isolate* isolate,
122 const v8::Global<v8::Context>& context) {
123 v8::MicrotasksScope microtasks_scope(isolate,
124 v8::MicrotasksScope::kRunMicrotasks);
125 v8::HandleScope handle_scope(isolate);
126 v8::Local<v8::Context> local_context = context.Get(isolate);
127 v8::Context::Scope context_scope(local_context);
128
129 v8::ScriptOrigin origin(v8::String::Empty(isolate));
130 v8::Local<v8::String> source =
131 v8::String::NewFromTwoByte(isolate, expression_.characters16(),
132 v8::NewStringType::kNormal,
133 expression_.length())
134 .ToLocalChecked();
135
136 v8::ScriptCompiler::Source scriptSource(source, origin);
137 v8::Local<v8::Script> script;
138 if (!v8::ScriptCompiler::Compile(local_context, &scriptSource)
139 .ToLocal(&script))
140 return;
141 v8::MaybeLocal<v8::Value> result;
142 result = script->Run(local_context);
143 }
144
145 // main to make compiler happy before other patch will be landed.
146 int main(int argc, char* argv[]) { return 0; }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698