2016-09-30 15:52:31 +00:00
|
|
|
// 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"
|
|
|
|
|
2016-12-13 19:40:14 +00:00
|
|
|
#include "test/inspector/inspector-impl.h"
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
#include <unistd.h> // NOLINT
|
|
|
|
#endif // !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const int kTaskRunnerIndex = 2;
|
2017-03-07 22:30:05 +00:00
|
|
|
const int kContextGroupIdIndex = 3;
|
2016-09-30 15:52:31 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2017-01-31 00:19:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-04-21 23:41:07 +00:00
|
|
|
TaskRunner::TaskRunner(TaskRunner::SetupGlobalTasks setup_global_tasks,
|
2016-10-05 15:07:57 +00:00
|
|
|
bool catch_exceptions,
|
2017-04-26 15:13:14 +00:00
|
|
|
v8::base::Semaphore* ready_semaphore,
|
|
|
|
v8::StartupData* startup_data)
|
2016-09-30 15:52:31 +00:00
|
|
|
: Thread(Options("Task Runner")),
|
2017-04-21 23:41:07 +00:00
|
|
|
setup_global_tasks_(std::move(setup_global_tasks)),
|
2017-04-26 15:13:14 +00:00
|
|
|
startup_data_(startup_data),
|
2016-10-05 15:07:57 +00:00
|
|
|
catch_exceptions_(catch_exceptions),
|
2016-09-30 15:52:31 +00:00
|
|
|
ready_semaphore_(ready_semaphore),
|
|
|
|
isolate_(nullptr),
|
|
|
|
process_queue_semaphore_(0),
|
|
|
|
nested_loop_count_(0) {
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
TaskRunner::~TaskRunner() { Join(); }
|
|
|
|
|
2017-03-07 22:30:05 +00:00
|
|
|
void TaskRunner::InitializeIsolate() {
|
2016-09-30 15:52:31 +00:00
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.array_buffer_allocator =
|
|
|
|
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
2017-04-26 15:13:14 +00:00
|
|
|
params.snapshot_blob = startup_data_;
|
2016-09-30 15:52:31 +00:00
|
|
|
isolate_ = v8::Isolate::New(params);
|
|
|
|
isolate_->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped);
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate_);
|
|
|
|
v8::HandleScope handle_scope(isolate_);
|
2017-04-21 23:41:07 +00:00
|
|
|
NewContextGroup(setup_global_tasks_);
|
2017-03-07 22:30:05 +00:00
|
|
|
if (ready_semaphore_) ready_semaphore_->Signal();
|
|
|
|
}
|
2016-09-30 15:52:31 +00:00
|
|
|
|
2017-04-21 23:41:07 +00:00
|
|
|
v8::Local<v8::Context> TaskRunner::NewContextGroup(
|
|
|
|
const TaskRunner::SetupGlobalTasks& setup_global_tasks) {
|
2016-09-30 15:52:31 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global_template =
|
|
|
|
v8::ObjectTemplate::New(isolate_);
|
2017-04-21 23:41:07 +00:00
|
|
|
for (auto it = setup_global_tasks.begin(); it != setup_global_tasks.end();
|
|
|
|
++it) {
|
|
|
|
(*it)->Run(isolate_, global_template);
|
|
|
|
}
|
2016-09-30 15:52:31 +00:00
|
|
|
v8::Local<v8::Context> context =
|
2017-04-21 23:41:07 +00:00
|
|
|
v8::Context::New(isolate_, nullptr, global_template);
|
2016-09-30 15:52:31 +00:00
|
|
|
context->SetAlignedPointerInEmbedderData(kTaskRunnerIndex, this);
|
2017-03-07 22:30:05 +00:00
|
|
|
intptr_t context_group_id = ++last_context_group_id_;
|
|
|
|
// Should be 2-byte aligned.
|
|
|
|
context->SetAlignedPointerInEmbedderData(
|
|
|
|
kContextGroupIdIndex, reinterpret_cast<void*>(context_group_id * 2));
|
|
|
|
contexts_[context_group_id].Reset(isolate_, context);
|
|
|
|
return context;
|
|
|
|
}
|
2016-09-30 15:52:31 +00:00
|
|
|
|
2017-03-07 22:30:05 +00:00
|
|
|
v8::Local<v8::Context> TaskRunner::GetContext(int context_group_id) {
|
|
|
|
return contexts_[context_group_id].Get(isolate_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int TaskRunner::GetContextGroupId(v8::Local<v8::Context> context) {
|
2017-03-23 15:39:41 +00:00
|
|
|
return static_cast<int>(
|
|
|
|
reinterpret_cast<intptr_t>(
|
|
|
|
context->GetAlignedPointerFromEmbedderData(kContextGroupIdIndex)) /
|
|
|
|
2);
|
2016-09-30 15:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TaskRunner::Run() {
|
2017-03-07 22:30:05 +00:00
|
|
|
InitializeIsolate();
|
2016-09-30 15:52:31 +00:00
|
|
|
RunMessageLoop(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskRunner::RunMessageLoop(bool only_protocol) {
|
|
|
|
int loop_number = ++nested_loop_count_;
|
2016-10-19 02:04:34 +00:00
|
|
|
while (nested_loop_count_ == loop_number && !is_terminated_.Value()) {
|
2016-09-30 15:52:31 +00:00
|
|
|
TaskRunner::Task* task = GetNext(only_protocol);
|
2016-10-19 02:04:34 +00:00
|
|
|
if (!task) return;
|
2016-09-30 15:52:31 +00:00
|
|
|
v8::Isolate::Scope isolate_scope(isolate_);
|
2016-10-05 15:07:57 +00:00
|
|
|
if (catch_exceptions_) {
|
|
|
|
v8::TryCatch try_catch(isolate_);
|
2017-05-16 20:38:33 +00:00
|
|
|
task->RunOnTaskRunner(this);
|
2016-10-05 15:07:57 +00:00
|
|
|
delete task;
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
ReportUncaughtException(isolate_, try_catch);
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
} else {
|
2017-05-16 20:38:33 +00:00
|
|
|
task->RunOnTaskRunner(this);
|
2016-10-05 15:07:57 +00:00
|
|
|
delete task;
|
2016-09-30 15:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskRunner::QuitMessageLoop() {
|
|
|
|
DCHECK(nested_loop_count_ > 0);
|
|
|
|
--nested_loop_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskRunner::Append(Task* task) {
|
|
|
|
queue_.Enqueue(task);
|
|
|
|
process_queue_semaphore_.Signal();
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:04:34 +00:00
|
|
|
void TaskRunner::Terminate() {
|
|
|
|
is_terminated_.Increment(1);
|
|
|
|
process_queue_semaphore_.Signal();
|
|
|
|
}
|
|
|
|
|
2017-01-31 00:19:41 +00:00
|
|
|
void TaskRunner::RegisterModule(v8::internal::Vector<uint16_t> name,
|
|
|
|
v8::Local<v8::Module> module) {
|
|
|
|
modules_[name] = v8::Global<v8::Module>(isolate_, module);
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::MaybeLocal<v8::Module> TaskRunner::ModuleResolveCallback(
|
|
|
|
v8::Local<v8::Context> context, v8::Local<v8::String> specifier,
|
|
|
|
v8::Local<v8::Module> referrer) {
|
|
|
|
std::string str = *v8::String::Utf8Value(specifier);
|
|
|
|
TaskRunner* runner = TaskRunner::FromContext(context);
|
|
|
|
return runner->modules_[ToVector(specifier)].Get(runner->isolate_);
|
|
|
|
}
|
|
|
|
|
2016-09-30 15:52:31 +00:00
|
|
|
TaskRunner::Task* TaskRunner::GetNext(bool only_protocol) {
|
|
|
|
for (;;) {
|
2016-10-19 02:04:34 +00:00
|
|
|
if (is_terminated_.Value()) return nullptr;
|
2016-09-30 15:52:31 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2016-12-13 19:40:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 20:38:33 +00:00
|
|
|
void AsyncTask::Run() {
|
2016-12-13 19:40:14 +00:00
|
|
|
if (inspector_) inspector_->asyncTaskStarted(this);
|
2017-05-16 20:38:33 +00:00
|
|
|
AsyncRun();
|
2016-12-13 19:40:14 +00:00
|
|
|
if (inspector_) inspector_->asyncTaskFinished(this);
|
|
|
|
}
|
|
|
|
|
2016-10-07 21:16:41 +00:00
|
|
|
ExecuteStringTask::ExecuteStringTask(
|
2016-11-04 19:59:11 +00:00
|
|
|
const v8::internal::Vector<uint16_t>& expression,
|
|
|
|
v8::Local<v8::String> name, v8::Local<v8::Integer> line_offset,
|
2017-01-31 00:19:41 +00:00
|
|
|
v8::Local<v8::Integer> column_offset, v8::Local<v8::Boolean> is_module,
|
|
|
|
const char* task_name, v8_inspector::V8Inspector* inspector)
|
2016-12-13 19:40:14 +00:00
|
|
|
: AsyncTask(task_name, inspector),
|
|
|
|
expression_(expression),
|
2016-11-04 19:59:11 +00:00
|
|
|
name_(ToVector(name)),
|
|
|
|
line_offset_(line_offset.As<v8::Int32>()->Value()),
|
2017-01-31 00:19:41 +00:00
|
|
|
column_offset_(column_offset.As<v8::Int32>()->Value()),
|
|
|
|
is_module_(is_module->Value()) {}
|
2016-09-30 15:52:31 +00:00
|
|
|
|
2016-10-07 21:16:41 +00:00
|
|
|
ExecuteStringTask::ExecuteStringTask(
|
|
|
|
const v8::internal::Vector<const char>& expression)
|
2017-01-31 00:19:41 +00:00
|
|
|
: AsyncTask(nullptr, nullptr), expression_utf8_(expression) {}
|
2016-10-07 21:16:41 +00:00
|
|
|
|
2017-05-16 20:38:33 +00:00
|
|
|
void ExecuteStringTask::AsyncRun() {
|
|
|
|
v8::MicrotasksScope microtasks_scope(isolate(),
|
2016-09-30 15:52:31 +00:00
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
2017-05-16 20:38:33 +00:00
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
v8::Local<v8::Context> context = default_context();
|
|
|
|
v8::Context::Scope context_scope(context);
|
2016-09-30 15:52:31 +00:00
|
|
|
|
2016-11-04 19:59:11 +00:00
|
|
|
v8::Local<v8::String> name =
|
2017-05-16 20:38:33 +00:00
|
|
|
v8::String::NewFromTwoByte(isolate(), name_.start(),
|
2016-11-04 19:59:11 +00:00
|
|
|
v8::NewStringType::kNormal, name_.length())
|
|
|
|
.ToLocalChecked();
|
2017-05-16 20:38:33 +00:00
|
|
|
v8::Local<v8::Integer> line_offset =
|
|
|
|
v8::Integer::New(isolate(), line_offset_);
|
2016-11-04 19:59:11 +00:00
|
|
|
v8::Local<v8::Integer> column_offset =
|
2017-05-16 20:38:33 +00:00
|
|
|
v8::Integer::New(isolate(), column_offset_);
|
2016-11-04 19:59:11 +00:00
|
|
|
|
2017-01-31 00:19:41 +00:00
|
|
|
v8::ScriptOrigin origin(
|
|
|
|
name, line_offset, column_offset,
|
|
|
|
/* resource_is_shared_cross_origin */ v8::Local<v8::Boolean>(),
|
|
|
|
/* script_id */ v8::Local<v8::Integer>(),
|
|
|
|
/* source_map_url */ v8::Local<v8::Value>(),
|
|
|
|
/* resource_is_opaque */ v8::Local<v8::Boolean>(),
|
|
|
|
/* is_wasm */ v8::Local<v8::Boolean>(),
|
2017-05-16 20:38:33 +00:00
|
|
|
v8::Boolean::New(isolate(), is_module_));
|
2016-10-07 21:16:41 +00:00
|
|
|
v8::Local<v8::String> source;
|
|
|
|
if (expression_.length()) {
|
2017-05-16 20:38:33 +00:00
|
|
|
source = v8::String::NewFromTwoByte(isolate(), expression_.start(),
|
2016-10-07 21:16:41 +00:00
|
|
|
v8::NewStringType::kNormal,
|
|
|
|
expression_.length())
|
|
|
|
.ToLocalChecked();
|
|
|
|
} else {
|
2017-05-16 20:38:33 +00:00
|
|
|
source = v8::String::NewFromUtf8(isolate(), expression_utf8_.start(),
|
2016-10-07 21:16:41 +00:00
|
|
|
v8::NewStringType::kNormal,
|
|
|
|
expression_utf8_.length())
|
|
|
|
.ToLocalChecked();
|
|
|
|
}
|
2016-09-30 15:52:31 +00:00
|
|
|
|
|
|
|
v8::ScriptCompiler::Source scriptSource(source, origin);
|
2017-01-31 00:19:41 +00:00
|
|
|
if (!is_module_) {
|
|
|
|
v8::Local<v8::Script> script;
|
2017-05-16 20:38:33 +00:00
|
|
|
if (!v8::ScriptCompiler::Compile(context, &scriptSource).ToLocal(&script))
|
2017-01-31 00:19:41 +00:00
|
|
|
return;
|
|
|
|
v8::MaybeLocal<v8::Value> result;
|
2017-05-16 20:38:33 +00:00
|
|
|
result = script->Run(context);
|
2017-01-31 00:19:41 +00:00
|
|
|
} else {
|
|
|
|
v8::Local<v8::Module> module;
|
2017-05-16 20:38:33 +00:00
|
|
|
if (!v8::ScriptCompiler::CompileModule(isolate(), &scriptSource)
|
2017-01-31 00:19:41 +00:00
|
|
|
.ToLocal(&module)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-16 20:38:33 +00:00
|
|
|
if (!module->Instantiate(context, &TaskRunner::ModuleResolveCallback))
|
2017-01-31 00:19:41 +00:00
|
|
|
return;
|
|
|
|
v8::Local<v8::Value> result;
|
2017-05-16 20:38:33 +00:00
|
|
|
if (!module->Evaluate(context).ToLocal(&result)) return;
|
|
|
|
TaskRunner* runner = TaskRunner::FromContext(context);
|
2017-01-31 00:19:41 +00:00
|
|
|
runner->RegisterModule(name_, module);
|
|
|
|
}
|
2016-09-30 15:52:31 +00:00
|
|
|
}
|