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 #include "src/inspector/string-util.h" | |
8 | |
9 namespace { | |
10 const int kTaskQueueIndex = 2; | |
11 } // namespace | |
12 | |
13 TaskScope::TaskScope(v8::Isolate* isolate, | |
14 const v8::Global<v8::Context>& context) | |
15 : isolate_(isolate), | |
16 isolate_scope_(isolate), | |
17 handle_scope_(isolate), | |
18 context_(context.Get(isolate)), | |
19 context_scope_(context_), | |
20 microtasks_scope_(isolate_, v8::MicrotasksScope::kRunMicrotasks), | |
21 try_catch_(isolate_) {} | |
22 | |
23 TaskScope::~TaskScope() { | |
24 if (try_catch_.HasCaught()) { | |
25 v8_inspector::String16 message = | |
26 v8_inspector::toProtocolString(try_catch_.Message()->Get()); | |
27 fprintf(stderr, "Unhandle exception: %s\n", message.utf8().data()); | |
28 fflush(stdout); | |
29 fflush(stderr); | |
30 _exit(0); | |
31 } | |
32 } | |
33 | |
34 ExecuteStringTask::ExecuteStringTask(const v8_inspector::StringView& expression) | |
35 : expression_(toString16(expression)) {} | |
36 | |
37 void ExecuteStringTask::Run(v8::Isolate* isolate, | |
38 const v8::Global<v8::Context>& global_context) { | |
39 TaskScope task_scope(isolate, global_context); | |
40 v8::Local<v8::Context> context = task_scope.context(); | |
41 v8::ScriptOrigin origin(v8::String::Empty(isolate)); | |
42 v8::ScriptCompiler::Source scriptSource(toV8String(isolate, expression_), | |
43 origin); | |
44 v8::Local<v8::Script> script; | |
45 if (!v8::ScriptCompiler::Compile(context, &scriptSource).ToLocal(&script)) | |
46 return; | |
47 v8::MaybeLocal<v8::Value> result; | |
dgozman
2016/09/23 03:09:55
... = script->Run(context);
kozy
2016/09/23 17:05:19
I split it into two lines to avoid redundant check
| |
48 result = script->Run(context); | |
49 } | |
50 | |
51 TaskQueue::TaskQueue() : process_queue_semaphore_(0) {} | |
52 | |
53 TaskQueue::~TaskQueue() {} | |
54 | |
55 void TaskQueue::Append(Task* task) { | |
56 queue_.Enqueue(task); | |
57 process_queue_semaphore_.Signal(); | |
58 } | |
59 | |
60 Task* TaskQueue::GetNext(bool only_protocol) { | |
61 for (;;) { | |
62 if (only_protocol) { | |
63 for (;;) { | |
64 Task* task = nullptr; | |
65 if (!queue_.Dequeue(&task)) break; | |
66 if (task->is_protocol_task()) return task; | |
67 deffered_queue_.Enqueue(task); | |
68 } | |
69 } else { | |
70 Task* task = nullptr; | |
71 if (deffered_queue_.Dequeue(&task)) return task; | |
72 if (queue_.Dequeue(&task)) return task; | |
73 } | |
74 process_queue_semaphore_.Wait(); | |
75 } | |
76 } | |
77 | |
78 void TaskQueue::SetContext(v8::Local<v8::Context> context) { | |
79 context->SetAlignedPointerInEmbedderData(kTaskQueueIndex, this); | |
80 } | |
81 | |
82 TaskQueue* TaskQueue::FromContext(v8::Local<v8::Context> context) { | |
83 return static_cast<TaskQueue*>( | |
84 context->GetAlignedPointerFromEmbedderData(kTaskQueueIndex)); | |
85 } | |
86 | |
87 TaskRunner::TaskRunner(TaskQueue* queue) | |
88 : Thread(Options("Task Runner")), | |
89 queue_(queue), | |
90 state_(TaskRunner::kRunning), | |
91 isolate_(NULL) { | |
dgozman
2016/09/23 03:09:55
nullptr
kozy
2016/09/23 17:05:20
Done.
| |
92 Start(); | |
93 } | |
94 | |
95 TaskRunner::~TaskRunner() { Join(); } | |
96 | |
97 void TaskRunner::SetContext(v8::Local<v8::Context> context) { | |
dgozman
2016/09/23 03:09:56
Why not pass in constructor?
kozy
2016/09/23 17:05:20
Moved isolate and context initialization logic int
| |
98 isolate_ = context->GetIsolate(); | |
99 context_.Reset(isolate_, context); | |
100 } | |
101 | |
102 void TaskRunner::Run() { | |
103 while (state_.Value() == kRunning) { | |
104 Task* task = queue_->GetNext(false); | |
dgozman
2016/09/23 03:09:56
Looks like this runs forever even if Stop was call
kozy
2016/09/23 17:05:20
I expose RunMessageLoop and QuitMessageLoop interf
| |
105 if (!task) return; | |
106 if (isolate_) | |
107 task->Run(isolate_, context_); | |
dgozman
2016/09/23 03:09:55
Why two different types of tasks?
kozy
2016/09/23 17:05:19
Removed.
| |
108 else | |
109 task->Run(); | |
110 delete task; | |
111 } | |
112 } | |
113 | |
114 void TaskRunner::Stop() { state_.SetValue(kStopRequested); } | |
115 | |
116 // main to make compiler happy before other patch will be landed. | |
117 int main(int argc, char* argv[]) { return 0; } | |
OLD | NEW |