summaryrefslogtreecommitdiff
path: root/deps/v8/test/inspector/task-runner.cc
blob: 14270177f00a2ebc12bb0028bca730e41cefdede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "test/inspector/task-runner.h"

#include "test/inspector/inspector-impl.h"

#if !defined(_WIN32) && !defined(_WIN64)
#include <unistd.h>  // NOLINT
#endif               // !defined(_WIN32) && !defined(_WIN64)

namespace {

const int kTaskRunnerIndex = 2;

void ReportUncaughtException(v8::Isolate* isolate,
                             const v8::TryCatch& try_catch) {
  CHECK(try_catch.HasCaught());
  v8::HandleScope handle_scope(isolate);
  std::string message = *v8::String::Utf8Value(try_catch.Message()->Get());
  fprintf(stderr, "Unhandle exception: %s\n", message.data());
}

}  //  namespace

TaskRunner::TaskRunner(v8::ExtensionConfiguration* extensions,
                       bool catch_exceptions,
                       v8::base::Semaphore* ready_semaphore)
    : Thread(Options("Task Runner")),
      extensions_(extensions),
      catch_exceptions_(catch_exceptions),
      ready_semaphore_(ready_semaphore),
      isolate_(nullptr),
      process_queue_semaphore_(0),
      nested_loop_count_(0) {
  Start();
}

TaskRunner::~TaskRunner() { Join(); }

void TaskRunner::InitializeContext() {
  v8::Isolate::CreateParams params;
  params.array_buffer_allocator =
      v8::ArrayBuffer::Allocator::NewDefaultAllocator();
  isolate_ = v8::Isolate::New(params);
  isolate_->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped);
  v8::Isolate::Scope isolate_scope(isolate_);
  v8::HandleScope handle_scope(isolate_);

  v8::Local<v8::ObjectTemplate> global_template =
      v8::ObjectTemplate::New(isolate_);
  v8::Local<v8::Context> context =
      v8::Context::New(isolate_, extensions_, global_template);
  context->SetAlignedPointerInEmbedderData(kTaskRunnerIndex, this);
  context_.Reset(isolate_, context);

  if (ready_semaphore_) ready_semaphore_->Signal();
}

void TaskRunner::Run() {
  InitializeContext();
  RunMessageLoop(false);
}

void TaskRunner::RunMessageLoop(bool only_protocol) {
  int loop_number = ++nested_loop_count_;
  while (nested_loop_count_ == loop_number && !is_terminated_.Value()) {
    TaskRunner::Task* task = GetNext(only_protocol);
    if (!task) return;
    v8::Isolate::Scope isolate_scope(isolate_);
    if (catch_exceptions_) {
      v8::TryCatch try_catch(isolate_);
      task->Run(isolate_, context_);
      delete task;
      if (try_catch.HasCaught()) {
        ReportUncaughtException(isolate_, try_catch);
        fflush(stdout);
        fflush(stderr);
        _exit(0);
      }
    } else {
      task->Run(isolate_, context_);
      delete task;
    }
  }
}

void TaskRunner::QuitMessageLoop() {
  DCHECK(nested_loop_count_ > 0);
  --nested_loop_count_;
}

void TaskRunner::Append(Task* task) {
  queue_.Enqueue(task);
  process_queue_semaphore_.Signal();
}

void TaskRunner::Terminate() {
  is_terminated_.Increment(1);
  process_queue_semaphore_.Signal();
}

TaskRunner::Task* TaskRunner::GetNext(bool only_protocol) {
  for (;;) {
    if (is_terminated_.Value()) return nullptr;
    if (only_protocol) {
      Task* task = nullptr;
      if (queue_.Dequeue(&task)) {
        if (task->is_inspector_task()) return task;
        deffered_queue_.Enqueue(task);
      }
    } else {
      Task* task = nullptr;
      if (deffered_queue_.Dequeue(&task)) return task;
      if (queue_.Dequeue(&task)) return task;
    }
    process_queue_semaphore_.Wait();
  }
  return nullptr;
}

TaskRunner* TaskRunner::FromContext(v8::Local<v8::Context> context) {
  return static_cast<TaskRunner*>(
      context->GetAlignedPointerFromEmbedderData(kTaskRunnerIndex));
}

namespace {

v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) {
  v8::internal::Vector<uint16_t> buffer =
      v8::internal::Vector<uint16_t>::New(str->Length());
  str->Write(buffer.start(), 0, str->Length());
  return buffer;
}

}  // namespace

AsyncTask::AsyncTask(const char* task_name,
                     v8_inspector::V8Inspector* inspector)
    : inspector_(task_name ? inspector : nullptr) {
  if (inspector_) {
    inspector_->asyncTaskScheduled(
        v8_inspector::StringView(reinterpret_cast<const uint8_t*>(task_name),
                                 strlen(task_name)),
        this, false);
  }
}

void AsyncTask::Run(v8::Isolate* isolate,
                    const v8::Global<v8::Context>& context) {
  if (inspector_) inspector_->asyncTaskStarted(this);
  AsyncRun(isolate, context);
  if (inspector_) inspector_->asyncTaskFinished(this);
}

ExecuteStringTask::ExecuteStringTask(
    const v8::internal::Vector<uint16_t>& expression,
    v8::Local<v8::String> name, v8::Local<v8::Integer> line_offset,
    v8::Local<v8::Integer> column_offset, const char* task_name,
    v8_inspector::V8Inspector* inspector)
    : AsyncTask(task_name, inspector),
      expression_(expression),
      name_(ToVector(name)),
      line_offset_(line_offset.As<v8::Int32>()->Value()),
      column_offset_(column_offset.As<v8::Int32>()->Value()) {}

ExecuteStringTask::ExecuteStringTask(
    const v8::internal::Vector<const char>& expression)
    : AsyncTask(nullptr, nullptr),
      expression_utf8_(expression),
      line_offset_(0),
      column_offset_(0) {}

void ExecuteStringTask::AsyncRun(v8::Isolate* isolate,
                                 const v8::Global<v8::Context>& context) {
  v8::MicrotasksScope microtasks_scope(isolate,
                                       v8::MicrotasksScope::kRunMicrotasks);
  v8::HandleScope handle_scope(isolate);
  v8::Local<v8::Context> local_context = context.Get(isolate);
  v8::Context::Scope context_scope(local_context);

  v8::Local<v8::String> name =
      v8::String::NewFromTwoByte(isolate, name_.start(),
                                 v8::NewStringType::kNormal, name_.length())
          .ToLocalChecked();
  v8::Local<v8::Integer> line_offset = v8::Integer::New(isolate, line_offset_);
  v8::Local<v8::Integer> column_offset =
      v8::Integer::New(isolate, column_offset_);

  v8::ScriptOrigin origin(name, line_offset, column_offset);
  v8::Local<v8::String> source;
  if (expression_.length()) {
    source = v8::String::NewFromTwoByte(isolate, expression_.start(),
                                        v8::NewStringType::kNormal,
                                        expression_.length())
                 .ToLocalChecked();
  } else {
    source = v8::String::NewFromUtf8(isolate, expression_utf8_.start(),
                                     v8::NewStringType::kNormal,
                                     expression_utf8_.length())
                 .ToLocalChecked();
  }

  v8::ScriptCompiler::Source scriptSource(source, origin);
  v8::Local<v8::Script> script;
  if (!v8::ScriptCompiler::Compile(local_context, &scriptSource)
           .ToLocal(&script))
    return;
  v8::MaybeLocal<v8::Value> result;
  result = script->Run(local_context);
}