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