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 #include "test/inspector/task-runner.h" | 5 #include "test/inspector/task-runner.h" |
6 | 6 |
7 #include "test/inspector/inspector-impl.h" | 7 #include "test/inspector/inspector-impl.h" |
8 | 8 |
9 #if !defined(_WIN32) && !defined(_WIN64) | 9 #if !defined(_WIN32) && !defined(_WIN64) |
10 #include <unistd.h> // NOLINT | 10 #include <unistd.h> // NOLINT |
11 #endif // !defined(_WIN32) && !defined(_WIN64) | 11 #endif // !defined(_WIN32) && !defined(_WIN64) |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 void ReportUncaughtException(v8::Isolate* isolate, | 15 void ReportUncaughtException(v8::Isolate* isolate, |
16 const v8::TryCatch& try_catch) { | 16 const v8::TryCatch& try_catch) { |
17 CHECK(try_catch.HasCaught()); | 17 CHECK(try_catch.HasCaught()); |
18 v8::HandleScope handle_scope(isolate); | 18 v8::HandleScope handle_scope(isolate); |
19 std::string message = *v8::String::Utf8Value(try_catch.Message()->Get()); | 19 std::string message = *v8::String::Utf8Value(try_catch.Message()->Get()); |
20 fprintf(stderr, "Unhandle exception: %s\n", message.data()); | 20 int line = try_catch.Message() |
| 21 ->GetLineNumber(isolate->GetCurrentContext()) |
| 22 .FromJust(); |
| 23 std::string source_line = |
| 24 *v8::String::Utf8Value(try_catch.Message() |
| 25 ->GetSourceLine(isolate->GetCurrentContext()) |
| 26 .ToLocalChecked()); |
| 27 fprintf(stderr, "Unhandle exception: %s @%s[%d]\n", message.data(), |
| 28 source_line.data(), line); |
21 } | 29 } |
22 | 30 |
23 v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) { | 31 v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) { |
24 v8::internal::Vector<uint16_t> buffer = | 32 v8::internal::Vector<uint16_t> buffer = |
25 v8::internal::Vector<uint16_t>::New(str->Length()); | 33 v8::internal::Vector<uint16_t>::New(str->Length()); |
26 str->Write(buffer.start(), 0, str->Length()); | 34 str->Write(buffer.start(), 0, str->Length()); |
27 return buffer; | 35 return buffer; |
28 } | 36 } |
29 | 37 |
30 } // namespace | 38 } // namespace |
31 | 39 |
32 TaskRunner::TaskRunner(IsolateData::SetupGlobalTasks setup_global_tasks, | 40 TaskRunner::TaskRunner(IsolateData::SetupGlobalTasks setup_global_tasks, |
33 bool catch_exceptions, | 41 bool catch_exceptions, |
34 v8::base::Semaphore* ready_semaphore, | 42 v8::base::Semaphore* ready_semaphore, |
35 v8::StartupData* startup_data) | 43 v8::StartupData* startup_data, |
| 44 InspectorClientImpl::FrontendChannel* channel) |
36 : Thread(Options("Task Runner")), | 45 : Thread(Options("Task Runner")), |
37 setup_global_tasks_(std::move(setup_global_tasks)), | 46 setup_global_tasks_(std::move(setup_global_tasks)), |
38 startup_data_(startup_data), | 47 startup_data_(startup_data), |
| 48 channel_(channel), |
39 catch_exceptions_(catch_exceptions), | 49 catch_exceptions_(catch_exceptions), |
40 ready_semaphore_(ready_semaphore), | 50 ready_semaphore_(ready_semaphore), |
41 data_(nullptr), | 51 data_(nullptr), |
42 process_queue_semaphore_(0), | 52 process_queue_semaphore_(0), |
43 nested_loop_count_(0) { | 53 nested_loop_count_(0) { |
44 Start(); | 54 Start(); |
45 } | 55 } |
46 | 56 |
47 TaskRunner::~TaskRunner() { Join(); } | 57 TaskRunner::~TaskRunner() { Join(); } |
48 | 58 |
49 void TaskRunner::Run() { | 59 void TaskRunner::Run() { |
50 data_.reset( | 60 data_.reset(new IsolateData(this, std::move(setup_global_tasks_), |
51 new IsolateData(this, std::move(setup_global_tasks_), startup_data_)); | 61 startup_data_, channel_)); |
52 | |
53 v8::Isolate::Scope isolate_scope(isolate()); | |
54 v8::HandleScope handle_scope(isolate()); | |
55 default_context_group_id_ = data_->CreateContextGroup(); | |
56 | |
57 if (ready_semaphore_) ready_semaphore_->Signal(); | 62 if (ready_semaphore_) ready_semaphore_->Signal(); |
58 RunMessageLoop(false); | 63 RunMessageLoop(false); |
59 } | 64 } |
60 | 65 |
61 void TaskRunner::RunMessageLoop(bool only_protocol) { | 66 void TaskRunner::RunMessageLoop(bool only_protocol) { |
62 int loop_number = ++nested_loop_count_; | 67 int loop_number = ++nested_loop_count_; |
63 while (nested_loop_count_ == loop_number && !is_terminated_.Value()) { | 68 while (nested_loop_count_ == loop_number && !is_terminated_.Value()) { |
64 TaskRunner::Task* task = GetNext(only_protocol); | 69 TaskRunner::Task* task = GetNext(only_protocol); |
65 if (!task) return; | 70 if (!task) return; |
66 v8::Isolate::Scope isolate_scope(isolate()); | 71 v8::Isolate::Scope isolate_scope(isolate()); |
67 if (catch_exceptions_) { | 72 if (catch_exceptions_) { |
68 v8::TryCatch try_catch(isolate()); | 73 v8::TryCatch try_catch(isolate()); |
69 task->RunOnTaskRunner(this); | 74 task->RunOnIsolate(data_.get()); |
70 delete task; | 75 delete task; |
71 if (try_catch.HasCaught()) { | 76 if (try_catch.HasCaught()) { |
72 ReportUncaughtException(isolate(), try_catch); | 77 ReportUncaughtException(isolate(), try_catch); |
73 fflush(stdout); | 78 fflush(stdout); |
74 fflush(stderr); | 79 fflush(stderr); |
75 _exit(0); | 80 _exit(0); |
76 } | 81 } |
77 } else { | 82 } else { |
78 task->RunOnTaskRunner(this); | 83 task->RunOnIsolate(data_.get()); |
79 delete task; | 84 delete task; |
80 } | 85 } |
81 } | 86 } |
82 } | 87 } |
83 | 88 |
84 void TaskRunner::QuitMessageLoop() { | 89 void TaskRunner::QuitMessageLoop() { |
85 DCHECK(nested_loop_count_ > 0); | 90 DCHECK(nested_loop_count_ > 0); |
86 --nested_loop_count_; | 91 --nested_loop_count_; |
87 } | 92 } |
88 | 93 |
(...skipping 19 matching lines...) Expand all Loading... |
108 } else { | 113 } else { |
109 Task* task = nullptr; | 114 Task* task = nullptr; |
110 if (deffered_queue_.Dequeue(&task)) return task; | 115 if (deffered_queue_.Dequeue(&task)) return task; |
111 if (queue_.Dequeue(&task)) return task; | 116 if (queue_.Dequeue(&task)) return task; |
112 } | 117 } |
113 process_queue_semaphore_.Wait(); | 118 process_queue_semaphore_.Wait(); |
114 } | 119 } |
115 return nullptr; | 120 return nullptr; |
116 } | 121 } |
117 | 122 |
118 AsyncTask::AsyncTask(const char* task_name, | 123 AsyncTask::AsyncTask(IsolateData* data, const char* task_name) |
119 v8_inspector::V8Inspector* inspector) | 124 : instrumenting_(data && task_name) { |
120 : inspector_(task_name ? inspector : nullptr) { | 125 if (!instrumenting_) return; |
121 if (inspector_) { | 126 data->inspector()->inspector()->asyncTaskScheduled( |
122 inspector_->asyncTaskScheduled( | 127 v8_inspector::StringView(reinterpret_cast<const uint8_t*>(task_name), |
123 v8_inspector::StringView(reinterpret_cast<const uint8_t*>(task_name), | 128 strlen(task_name)), |
124 strlen(task_name)), | 129 this, false); |
125 this, false); | |
126 } | |
127 } | 130 } |
128 | 131 |
129 void AsyncTask::Run() { | 132 void AsyncTask::Run() { |
130 if (inspector_) inspector_->asyncTaskStarted(this); | 133 if (instrumenting_) data()->inspector()->inspector()->asyncTaskStarted(this); |
131 AsyncRun(); | 134 AsyncRun(); |
132 if (inspector_) inspector_->asyncTaskFinished(this); | 135 if (instrumenting_) data()->inspector()->inspector()->asyncTaskFinished(this); |
133 } | 136 } |
134 | 137 |
135 ExecuteStringTask::ExecuteStringTask( | 138 ExecuteStringTask::ExecuteStringTask( |
| 139 IsolateData* data, int context_group_id, const char* task_name, |
136 const v8::internal::Vector<uint16_t>& expression, | 140 const v8::internal::Vector<uint16_t>& expression, |
137 v8::Local<v8::String> name, v8::Local<v8::Integer> line_offset, | 141 v8::Local<v8::String> name, v8::Local<v8::Integer> line_offset, |
138 v8::Local<v8::Integer> column_offset, v8::Local<v8::Boolean> is_module, | 142 v8::Local<v8::Integer> column_offset, v8::Local<v8::Boolean> is_module) |
139 const char* task_name, v8_inspector::V8Inspector* inspector) | 143 : AsyncTask(data, task_name), |
140 : AsyncTask(task_name, inspector), | |
141 expression_(expression), | 144 expression_(expression), |
142 name_(ToVector(name)), | 145 name_(ToVector(name)), |
143 line_offset_(line_offset.As<v8::Int32>()->Value()), | 146 line_offset_(line_offset.As<v8::Int32>()->Value()), |
144 column_offset_(column_offset.As<v8::Int32>()->Value()), | 147 column_offset_(column_offset.As<v8::Int32>()->Value()), |
145 is_module_(is_module->Value()) {} | 148 is_module_(is_module->Value()), |
| 149 context_group_id_(context_group_id) {} |
146 | 150 |
147 ExecuteStringTask::ExecuteStringTask( | 151 ExecuteStringTask::ExecuteStringTask( |
148 const v8::internal::Vector<const char>& expression) | 152 const v8::internal::Vector<const char>& expression, int context_group_id) |
149 : AsyncTask(nullptr, nullptr), expression_utf8_(expression) {} | 153 : AsyncTask(nullptr, nullptr), |
| 154 expression_utf8_(expression), |
| 155 context_group_id_(context_group_id) {} |
150 | 156 |
151 void ExecuteStringTask::AsyncRun() { | 157 void ExecuteStringTask::AsyncRun() { |
152 v8::MicrotasksScope microtasks_scope(isolate(), | 158 v8::MicrotasksScope microtasks_scope(isolate(), |
153 v8::MicrotasksScope::kRunMicrotasks); | 159 v8::MicrotasksScope::kRunMicrotasks); |
154 v8::HandleScope handle_scope(isolate()); | 160 v8::HandleScope handle_scope(isolate()); |
155 v8::Local<v8::Context> context = default_context(); | 161 v8::Local<v8::Context> context = data()->GetContext(context_group_id_); |
156 v8::Context::Scope context_scope(context); | 162 v8::Context::Scope context_scope(context); |
157 | 163 |
158 v8::Local<v8::String> name = | 164 v8::Local<v8::String> name = |
159 v8::String::NewFromTwoByte(isolate(), name_.start(), | 165 v8::String::NewFromTwoByte(isolate(), name_.start(), |
160 v8::NewStringType::kNormal, name_.length()) | 166 v8::NewStringType::kNormal, name_.length()) |
161 .ToLocalChecked(); | 167 .ToLocalChecked(); |
162 v8::Local<v8::Integer> line_offset = | 168 v8::Local<v8::Integer> line_offset = |
163 v8::Integer::New(isolate(), line_offset_); | 169 v8::Integer::New(isolate(), line_offset_); |
164 v8::Local<v8::Integer> column_offset = | 170 v8::Local<v8::Integer> column_offset = |
165 v8::Integer::New(isolate(), column_offset_); | 171 v8::Integer::New(isolate(), column_offset_); |
(...skipping 20 matching lines...) Expand all Loading... |
186 } | 192 } |
187 | 193 |
188 v8::ScriptCompiler::Source scriptSource(source, origin); | 194 v8::ScriptCompiler::Source scriptSource(source, origin); |
189 if (!is_module_) { | 195 if (!is_module_) { |
190 v8::Local<v8::Script> script; | 196 v8::Local<v8::Script> script; |
191 if (!v8::ScriptCompiler::Compile(context, &scriptSource).ToLocal(&script)) | 197 if (!v8::ScriptCompiler::Compile(context, &scriptSource).ToLocal(&script)) |
192 return; | 198 return; |
193 v8::MaybeLocal<v8::Value> result; | 199 v8::MaybeLocal<v8::Value> result; |
194 result = script->Run(context); | 200 result = script->Run(context); |
195 } else { | 201 } else { |
196 IsolateData::FromContext(context)->RegisterModule(context, name_, | 202 data()->RegisterModule(context, name_, &scriptSource); |
197 &scriptSource); | |
198 } | 203 } |
199 } | 204 } |
OLD | NEW |