| 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 const int kTaskRunnerIndex = 2; | 15 const int kTaskRunnerIndex = 2; |
| 16 | 16 |
| 17 void ReportUncaughtException(v8::Isolate* isolate, | 17 void ReportUncaughtException(v8::Isolate* isolate, |
| 18 const v8::TryCatch& try_catch) { | 18 const v8::TryCatch& try_catch) { |
| 19 CHECK(try_catch.HasCaught()); | 19 CHECK(try_catch.HasCaught()); |
| 20 v8::HandleScope handle_scope(isolate); | 20 v8::HandleScope handle_scope(isolate); |
| 21 std::string message = *v8::String::Utf8Value(try_catch.Message()->Get()); | 21 std::string message = *v8::String::Utf8Value(try_catch.Message()->Get()); |
| 22 fprintf(stderr, "Unhandle exception: %s\n", message.data()); | 22 fprintf(stderr, "Unhandle exception: %s\n", message.data()); |
| 23 } | 23 } |
| 24 | 24 |
| 25 v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) { |
| 26 v8::internal::Vector<uint16_t> buffer = |
| 27 v8::internal::Vector<uint16_t>::New(str->Length()); |
| 28 str->Write(buffer.start(), 0, str->Length()); |
| 29 return buffer; |
| 30 } |
| 31 |
| 25 } // namespace | 32 } // namespace |
| 26 | 33 |
| 27 TaskRunner::TaskRunner(v8::ExtensionConfiguration* extensions, | 34 TaskRunner::TaskRunner(v8::ExtensionConfiguration* extensions, |
| 28 bool catch_exceptions, | 35 bool catch_exceptions, |
| 29 v8::base::Semaphore* ready_semaphore) | 36 v8::base::Semaphore* ready_semaphore) |
| 30 : Thread(Options("Task Runner")), | 37 : Thread(Options("Task Runner")), |
| 31 extensions_(extensions), | 38 extensions_(extensions), |
| 32 catch_exceptions_(catch_exceptions), | 39 catch_exceptions_(catch_exceptions), |
| 33 ready_semaphore_(ready_semaphore), | 40 ready_semaphore_(ready_semaphore), |
| 34 isolate_(nullptr), | 41 isolate_(nullptr), |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 void TaskRunner::Append(Task* task) { | 101 void TaskRunner::Append(Task* task) { |
| 95 queue_.Enqueue(task); | 102 queue_.Enqueue(task); |
| 96 process_queue_semaphore_.Signal(); | 103 process_queue_semaphore_.Signal(); |
| 97 } | 104 } |
| 98 | 105 |
| 99 void TaskRunner::Terminate() { | 106 void TaskRunner::Terminate() { |
| 100 is_terminated_.Increment(1); | 107 is_terminated_.Increment(1); |
| 101 process_queue_semaphore_.Signal(); | 108 process_queue_semaphore_.Signal(); |
| 102 } | 109 } |
| 103 | 110 |
| 111 void TaskRunner::RegisterModule(v8::internal::Vector<uint16_t> name, |
| 112 v8::Local<v8::Module> module) { |
| 113 modules_[name] = v8::Global<v8::Module>(isolate_, module); |
| 114 } |
| 115 |
| 116 v8::MaybeLocal<v8::Module> TaskRunner::ModuleResolveCallback( |
| 117 v8::Local<v8::Context> context, v8::Local<v8::String> specifier, |
| 118 v8::Local<v8::Module> referrer) { |
| 119 std::string str = *v8::String::Utf8Value(specifier); |
| 120 TaskRunner* runner = TaskRunner::FromContext(context); |
| 121 return runner->modules_[ToVector(specifier)].Get(runner->isolate_); |
| 122 } |
| 123 |
| 104 TaskRunner::Task* TaskRunner::GetNext(bool only_protocol) { | 124 TaskRunner::Task* TaskRunner::GetNext(bool only_protocol) { |
| 105 for (;;) { | 125 for (;;) { |
| 106 if (is_terminated_.Value()) return nullptr; | 126 if (is_terminated_.Value()) return nullptr; |
| 107 if (only_protocol) { | 127 if (only_protocol) { |
| 108 Task* task = nullptr; | 128 Task* task = nullptr; |
| 109 if (queue_.Dequeue(&task)) { | 129 if (queue_.Dequeue(&task)) { |
| 110 if (task->is_inspector_task()) return task; | 130 if (task->is_inspector_task()) return task; |
| 111 deffered_queue_.Enqueue(task); | 131 deffered_queue_.Enqueue(task); |
| 112 } | 132 } |
| 113 } else { | 133 } else { |
| 114 Task* task = nullptr; | 134 Task* task = nullptr; |
| 115 if (deffered_queue_.Dequeue(&task)) return task; | 135 if (deffered_queue_.Dequeue(&task)) return task; |
| 116 if (queue_.Dequeue(&task)) return task; | 136 if (queue_.Dequeue(&task)) return task; |
| 117 } | 137 } |
| 118 process_queue_semaphore_.Wait(); | 138 process_queue_semaphore_.Wait(); |
| 119 } | 139 } |
| 120 return nullptr; | 140 return nullptr; |
| 121 } | 141 } |
| 122 | 142 |
| 123 TaskRunner* TaskRunner::FromContext(v8::Local<v8::Context> context) { | 143 TaskRunner* TaskRunner::FromContext(v8::Local<v8::Context> context) { |
| 124 return static_cast<TaskRunner*>( | 144 return static_cast<TaskRunner*>( |
| 125 context->GetAlignedPointerFromEmbedderData(kTaskRunnerIndex)); | 145 context->GetAlignedPointerFromEmbedderData(kTaskRunnerIndex)); |
| 126 } | 146 } |
| 127 | 147 |
| 128 namespace { | |
| 129 | |
| 130 v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) { | |
| 131 v8::internal::Vector<uint16_t> buffer = | |
| 132 v8::internal::Vector<uint16_t>::New(str->Length()); | |
| 133 str->Write(buffer.start(), 0, str->Length()); | |
| 134 return buffer; | |
| 135 } | |
| 136 | |
| 137 } // namespace | |
| 138 | |
| 139 AsyncTask::AsyncTask(const char* task_name, | 148 AsyncTask::AsyncTask(const char* task_name, |
| 140 v8_inspector::V8Inspector* inspector) | 149 v8_inspector::V8Inspector* inspector) |
| 141 : inspector_(task_name ? inspector : nullptr) { | 150 : inspector_(task_name ? inspector : nullptr) { |
| 142 if (inspector_) { | 151 if (inspector_) { |
| 143 inspector_->asyncTaskScheduled( | 152 inspector_->asyncTaskScheduled( |
| 144 v8_inspector::StringView(reinterpret_cast<const uint8_t*>(task_name), | 153 v8_inspector::StringView(reinterpret_cast<const uint8_t*>(task_name), |
| 145 strlen(task_name)), | 154 strlen(task_name)), |
| 146 this, false); | 155 this, false); |
| 147 } | 156 } |
| 148 } | 157 } |
| 149 | 158 |
| 150 void AsyncTask::Run(v8::Isolate* isolate, | 159 void AsyncTask::Run(v8::Isolate* isolate, |
| 151 const v8::Global<v8::Context>& context) { | 160 const v8::Global<v8::Context>& context) { |
| 152 if (inspector_) inspector_->asyncTaskStarted(this); | 161 if (inspector_) inspector_->asyncTaskStarted(this); |
| 153 AsyncRun(isolate, context); | 162 AsyncRun(isolate, context); |
| 154 if (inspector_) inspector_->asyncTaskFinished(this); | 163 if (inspector_) inspector_->asyncTaskFinished(this); |
| 155 } | 164 } |
| 156 | 165 |
| 157 ExecuteStringTask::ExecuteStringTask( | 166 ExecuteStringTask::ExecuteStringTask( |
| 158 const v8::internal::Vector<uint16_t>& expression, | 167 const v8::internal::Vector<uint16_t>& expression, |
| 159 v8::Local<v8::String> name, v8::Local<v8::Integer> line_offset, | 168 v8::Local<v8::String> name, v8::Local<v8::Integer> line_offset, |
| 160 v8::Local<v8::Integer> column_offset, const char* task_name, | 169 v8::Local<v8::Integer> column_offset, v8::Local<v8::Boolean> is_module, |
| 161 v8_inspector::V8Inspector* inspector) | 170 const char* task_name, v8_inspector::V8Inspector* inspector) |
| 162 : AsyncTask(task_name, inspector), | 171 : AsyncTask(task_name, inspector), |
| 163 expression_(expression), | 172 expression_(expression), |
| 164 name_(ToVector(name)), | 173 name_(ToVector(name)), |
| 165 line_offset_(line_offset.As<v8::Int32>()->Value()), | 174 line_offset_(line_offset.As<v8::Int32>()->Value()), |
| 166 column_offset_(column_offset.As<v8::Int32>()->Value()) {} | 175 column_offset_(column_offset.As<v8::Int32>()->Value()), |
| 176 is_module_(is_module->Value()) {} |
| 167 | 177 |
| 168 ExecuteStringTask::ExecuteStringTask( | 178 ExecuteStringTask::ExecuteStringTask( |
| 169 const v8::internal::Vector<const char>& expression) | 179 const v8::internal::Vector<const char>& expression) |
| 170 : AsyncTask(nullptr, nullptr), | 180 : AsyncTask(nullptr, nullptr), expression_utf8_(expression) {} |
| 171 expression_utf8_(expression), | |
| 172 line_offset_(0), | |
| 173 column_offset_(0) {} | |
| 174 | 181 |
| 175 void ExecuteStringTask::AsyncRun(v8::Isolate* isolate, | 182 void ExecuteStringTask::AsyncRun(v8::Isolate* isolate, |
| 176 const v8::Global<v8::Context>& context) { | 183 const v8::Global<v8::Context>& context) { |
| 177 v8::MicrotasksScope microtasks_scope(isolate, | 184 v8::MicrotasksScope microtasks_scope(isolate, |
| 178 v8::MicrotasksScope::kRunMicrotasks); | 185 v8::MicrotasksScope::kRunMicrotasks); |
| 179 v8::HandleScope handle_scope(isolate); | 186 v8::HandleScope handle_scope(isolate); |
| 180 v8::Local<v8::Context> local_context = context.Get(isolate); | 187 v8::Local<v8::Context> local_context = context.Get(isolate); |
| 181 v8::Context::Scope context_scope(local_context); | 188 v8::Context::Scope context_scope(local_context); |
| 182 | 189 |
| 183 v8::Local<v8::String> name = | 190 v8::Local<v8::String> name = |
| 184 v8::String::NewFromTwoByte(isolate, name_.start(), | 191 v8::String::NewFromTwoByte(isolate, name_.start(), |
| 185 v8::NewStringType::kNormal, name_.length()) | 192 v8::NewStringType::kNormal, name_.length()) |
| 186 .ToLocalChecked(); | 193 .ToLocalChecked(); |
| 187 v8::Local<v8::Integer> line_offset = v8::Integer::New(isolate, line_offset_); | 194 v8::Local<v8::Integer> line_offset = v8::Integer::New(isolate, line_offset_); |
| 188 v8::Local<v8::Integer> column_offset = | 195 v8::Local<v8::Integer> column_offset = |
| 189 v8::Integer::New(isolate, column_offset_); | 196 v8::Integer::New(isolate, column_offset_); |
| 190 | 197 |
| 191 v8::ScriptOrigin origin(name, line_offset, column_offset); | 198 v8::ScriptOrigin origin( |
| 199 name, line_offset, column_offset, |
| 200 /* resource_is_shared_cross_origin */ v8::Local<v8::Boolean>(), |
| 201 /* script_id */ v8::Local<v8::Integer>(), |
| 202 /* source_map_url */ v8::Local<v8::Value>(), |
| 203 /* resource_is_opaque */ v8::Local<v8::Boolean>(), |
| 204 /* is_wasm */ v8::Local<v8::Boolean>(), |
| 205 v8::Boolean::New(isolate, is_module_)); |
| 192 v8::Local<v8::String> source; | 206 v8::Local<v8::String> source; |
| 193 if (expression_.length()) { | 207 if (expression_.length()) { |
| 194 source = v8::String::NewFromTwoByte(isolate, expression_.start(), | 208 source = v8::String::NewFromTwoByte(isolate, expression_.start(), |
| 195 v8::NewStringType::kNormal, | 209 v8::NewStringType::kNormal, |
| 196 expression_.length()) | 210 expression_.length()) |
| 197 .ToLocalChecked(); | 211 .ToLocalChecked(); |
| 198 } else { | 212 } else { |
| 199 source = v8::String::NewFromUtf8(isolate, expression_utf8_.start(), | 213 source = v8::String::NewFromUtf8(isolate, expression_utf8_.start(), |
| 200 v8::NewStringType::kNormal, | 214 v8::NewStringType::kNormal, |
| 201 expression_utf8_.length()) | 215 expression_utf8_.length()) |
| 202 .ToLocalChecked(); | 216 .ToLocalChecked(); |
| 203 } | 217 } |
| 204 | 218 |
| 205 v8::ScriptCompiler::Source scriptSource(source, origin); | 219 v8::ScriptCompiler::Source scriptSource(source, origin); |
| 206 v8::Local<v8::Script> script; | 220 if (!is_module_) { |
| 207 if (!v8::ScriptCompiler::Compile(local_context, &scriptSource) | 221 v8::Local<v8::Script> script; |
| 208 .ToLocal(&script)) | 222 if (!v8::ScriptCompiler::Compile(local_context, &scriptSource) |
| 209 return; | 223 .ToLocal(&script)) |
| 210 v8::MaybeLocal<v8::Value> result; | 224 return; |
| 211 result = script->Run(local_context); | 225 v8::MaybeLocal<v8::Value> result; |
| 226 result = script->Run(local_context); |
| 227 } else { |
| 228 v8::Local<v8::Module> module; |
| 229 if (!v8::ScriptCompiler::CompileModule(isolate, &scriptSource) |
| 230 .ToLocal(&module)) { |
| 231 return; |
| 232 } |
| 233 if (!module->Instantiate(local_context, &TaskRunner::ModuleResolveCallback)) |
| 234 return; |
| 235 v8::Local<v8::Value> result; |
| 236 if (!module->Evaluate(local_context).ToLocal(&result)) return; |
| 237 TaskRunner* runner = TaskRunner::FromContext(local_context); |
| 238 runner->RegisterModule(name_, module); |
| 239 } |
| 212 } | 240 } |
| OLD | NEW |