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