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 const int kContextGroupIdIndex = 3; |
16 | 17 |
17 void ReportUncaughtException(v8::Isolate* isolate, | 18 void ReportUncaughtException(v8::Isolate* isolate, |
18 const v8::TryCatch& try_catch) { | 19 const v8::TryCatch& try_catch) { |
19 CHECK(try_catch.HasCaught()); | 20 CHECK(try_catch.HasCaught()); |
20 v8::HandleScope handle_scope(isolate); | 21 v8::HandleScope handle_scope(isolate); |
21 std::string message = *v8::String::Utf8Value(try_catch.Message()->Get()); | 22 std::string message = *v8::String::Utf8Value(try_catch.Message()->Get()); |
22 fprintf(stderr, "Unhandle exception: %s\n", message.data()); | 23 fprintf(stderr, "Unhandle exception: %s\n", message.data()); |
23 } | 24 } |
24 | 25 |
25 v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) { | 26 v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) { |
(...skipping 13 matching lines...) Expand all Loading... |
39 catch_exceptions_(catch_exceptions), | 40 catch_exceptions_(catch_exceptions), |
40 ready_semaphore_(ready_semaphore), | 41 ready_semaphore_(ready_semaphore), |
41 isolate_(nullptr), | 42 isolate_(nullptr), |
42 process_queue_semaphore_(0), | 43 process_queue_semaphore_(0), |
43 nested_loop_count_(0) { | 44 nested_loop_count_(0) { |
44 Start(); | 45 Start(); |
45 } | 46 } |
46 | 47 |
47 TaskRunner::~TaskRunner() { Join(); } | 48 TaskRunner::~TaskRunner() { Join(); } |
48 | 49 |
49 void TaskRunner::InitializeContext() { | 50 void TaskRunner::InitializeIsolate() { |
50 v8::Isolate::CreateParams params; | 51 v8::Isolate::CreateParams params; |
51 params.array_buffer_allocator = | 52 params.array_buffer_allocator = |
52 v8::ArrayBuffer::Allocator::NewDefaultAllocator(); | 53 v8::ArrayBuffer::Allocator::NewDefaultAllocator(); |
53 isolate_ = v8::Isolate::New(params); | 54 isolate_ = v8::Isolate::New(params); |
54 isolate_->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped); | 55 isolate_->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped); |
55 v8::Isolate::Scope isolate_scope(isolate_); | 56 v8::Isolate::Scope isolate_scope(isolate_); |
56 v8::HandleScope handle_scope(isolate_); | 57 v8::HandleScope handle_scope(isolate_); |
| 58 NewContextGroup(); |
| 59 if (ready_semaphore_) ready_semaphore_->Signal(); |
| 60 } |
57 | 61 |
| 62 v8::Local<v8::Context> TaskRunner::NewContextGroup() { |
58 v8::Local<v8::ObjectTemplate> global_template = | 63 v8::Local<v8::ObjectTemplate> global_template = |
59 v8::ObjectTemplate::New(isolate_); | 64 v8::ObjectTemplate::New(isolate_); |
60 v8::Local<v8::Context> context = | 65 v8::Local<v8::Context> context = |
61 v8::Context::New(isolate_, extensions_, global_template); | 66 v8::Context::New(isolate_, extensions_, global_template); |
62 context->SetAlignedPointerInEmbedderData(kTaskRunnerIndex, this); | 67 context->SetAlignedPointerInEmbedderData(kTaskRunnerIndex, this); |
63 context_.Reset(isolate_, context); | 68 intptr_t context_group_id = ++last_context_group_id_; |
| 69 // Should be 2-byte aligned. |
| 70 context->SetAlignedPointerInEmbedderData( |
| 71 kContextGroupIdIndex, reinterpret_cast<void*>(context_group_id * 2)); |
| 72 contexts_[context_group_id].Reset(isolate_, context); |
| 73 return context; |
| 74 } |
64 | 75 |
65 if (ready_semaphore_) ready_semaphore_->Signal(); | 76 v8::Local<v8::Context> TaskRunner::GetContext(int context_group_id) { |
| 77 return contexts_[context_group_id].Get(isolate_); |
| 78 } |
| 79 |
| 80 int TaskRunner::GetContextGroupId(v8::Local<v8::Context> context) { |
| 81 return reinterpret_cast<intptr_t>( |
| 82 context->GetAlignedPointerFromEmbedderData(kContextGroupIdIndex)) / |
| 83 2; |
66 } | 84 } |
67 | 85 |
68 void TaskRunner::Run() { | 86 void TaskRunner::Run() { |
69 InitializeContext(); | 87 InitializeIsolate(); |
70 RunMessageLoop(false); | 88 RunMessageLoop(false); |
71 } | 89 } |
72 | 90 |
73 void TaskRunner::RunMessageLoop(bool only_protocol) { | 91 void TaskRunner::RunMessageLoop(bool only_protocol) { |
74 int loop_number = ++nested_loop_count_; | 92 int loop_number = ++nested_loop_count_; |
75 while (nested_loop_count_ == loop_number && !is_terminated_.Value()) { | 93 while (nested_loop_count_ == loop_number && !is_terminated_.Value()) { |
76 TaskRunner::Task* task = GetNext(only_protocol); | 94 TaskRunner::Task* task = GetNext(only_protocol); |
77 if (!task) return; | 95 if (!task) return; |
78 v8::Isolate::Scope isolate_scope(isolate_); | 96 v8::Isolate::Scope isolate_scope(isolate_); |
79 if (catch_exceptions_) { | 97 if (catch_exceptions_) { |
80 v8::TryCatch try_catch(isolate_); | 98 v8::TryCatch try_catch(isolate_); |
81 task->Run(isolate_, context_); | 99 task->Run(isolate_, contexts_.begin()->second); |
82 delete task; | 100 delete task; |
83 if (try_catch.HasCaught()) { | 101 if (try_catch.HasCaught()) { |
84 ReportUncaughtException(isolate_, try_catch); | 102 ReportUncaughtException(isolate_, try_catch); |
85 fflush(stdout); | 103 fflush(stdout); |
86 fflush(stderr); | 104 fflush(stderr); |
87 _exit(0); | 105 _exit(0); |
88 } | 106 } |
89 } else { | 107 } else { |
90 task->Run(isolate_, context_); | 108 task->Run(isolate_, contexts_.begin()->second); |
91 delete task; | 109 delete task; |
92 } | 110 } |
93 } | 111 } |
94 } | 112 } |
95 | 113 |
96 void TaskRunner::QuitMessageLoop() { | 114 void TaskRunner::QuitMessageLoop() { |
97 DCHECK(nested_loop_count_ > 0); | 115 DCHECK(nested_loop_count_ > 0); |
98 --nested_loop_count_; | 116 --nested_loop_count_; |
99 } | 117 } |
100 | 118 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 return; | 253 return; |
236 } | 254 } |
237 if (!module->Instantiate(local_context, &TaskRunner::ModuleResolveCallback)) | 255 if (!module->Instantiate(local_context, &TaskRunner::ModuleResolveCallback)) |
238 return; | 256 return; |
239 v8::Local<v8::Value> result; | 257 v8::Local<v8::Value> result; |
240 if (!module->Evaluate(local_context).ToLocal(&result)) return; | 258 if (!module->Evaluate(local_context).ToLocal(&result)) return; |
241 TaskRunner* runner = TaskRunner::FromContext(local_context); | 259 TaskRunner* runner = TaskRunner::FromContext(local_context); |
242 runner->RegisterModule(name_, module); | 260 runner->RegisterModule(name_, module); |
243 } | 261 } |
244 } | 262 } |
OLD | NEW |