2016-10-02 18:10:27 +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.
|
|
|
|
|
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
#include <unistd.h> // NOLINT
|
|
|
|
#endif // !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
|
2016-10-13 20:31:17 +00:00
|
|
|
#include <locale.h>
|
|
|
|
|
2016-10-02 18:10:27 +00:00
|
|
|
#include "include/libplatform/libplatform.h"
|
|
|
|
#include "include/v8.h"
|
|
|
|
|
|
|
|
#include "src/base/platform/platform.h"
|
|
|
|
#include "src/flags.h"
|
|
|
|
#include "src/utils.h"
|
|
|
|
#include "src/vector.h"
|
|
|
|
|
2017-05-22 00:48:27 +00:00
|
|
|
#include "test/inspector/isolate-data.h"
|
2016-10-02 18:10:27 +00:00
|
|
|
#include "test/inspector/task-runner.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-10-19 02:04:34 +00:00
|
|
|
std::vector<TaskRunner*> task_runners;
|
|
|
|
|
|
|
|
void Terminate() {
|
|
|
|
for (size_t i = 0; i < task_runners.size(); ++i) {
|
|
|
|
task_runners[i]->Terminate();
|
|
|
|
task_runners[i]->Join();
|
|
|
|
}
|
|
|
|
std::vector<TaskRunner*> empty;
|
|
|
|
task_runners.swap(empty);
|
|
|
|
}
|
|
|
|
|
2016-10-02 18:10:27 +00:00
|
|
|
void Exit() {
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
2016-10-19 02:04:34 +00:00
|
|
|
Terminate();
|
2016-10-02 18:10:27 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 19:59:11 +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;
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:04:24 +00:00
|
|
|
v8::Local<v8::String> ToV8String(v8::Isolate* isolate, const char* str) {
|
|
|
|
return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked();
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
v8::Local<v8::String> ToV8String(v8::Isolate* isolate, const char* str,
|
|
|
|
int length) {
|
|
|
|
return v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal,
|
|
|
|
length)
|
|
|
|
.ToLocalChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::String> ToV8String(v8::Isolate* isolate,
|
|
|
|
const v8::internal::Vector<uint16_t>& buffer) {
|
|
|
|
return v8::String::NewFromTwoByte(isolate, buffer.start(),
|
|
|
|
v8::NewStringType::kNormal, buffer.length())
|
|
|
|
.ToLocalChecked();
|
|
|
|
}
|
|
|
|
|
2017-05-18 23:11:20 +00:00
|
|
|
v8::internal::Vector<uint16_t> ToVector(
|
|
|
|
const v8_inspector::StringView& string) {
|
|
|
|
v8::internal::Vector<uint16_t> buffer =
|
|
|
|
v8::internal::Vector<uint16_t>::New(static_cast<int>(string.length()));
|
|
|
|
for (size_t i = 0; i < string.length(); i++) {
|
|
|
|
if (string.is8Bit())
|
|
|
|
buffer[i] = string.characters8()[i];
|
|
|
|
else
|
|
|
|
buffer[i] = string.characters16()[i];
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
class FrontendChannelImpl : public v8_inspector::V8Inspector::Channel {
|
2017-05-18 23:11:20 +00:00
|
|
|
public:
|
2017-05-22 20:46:42 +00:00
|
|
|
FrontendChannelImpl(TaskRunner* task_runner, int context_group_id,
|
|
|
|
v8::Isolate* isolate, v8::Local<v8::Function> function)
|
|
|
|
: task_runner_(task_runner),
|
2017-05-18 23:11:20 +00:00
|
|
|
context_group_id_(context_group_id),
|
2017-05-22 20:46:42 +00:00
|
|
|
function_(isolate, function) {}
|
|
|
|
virtual ~FrontendChannelImpl() = default;
|
|
|
|
|
|
|
|
void set_session_id(int session_id) { session_id_ = session_id; }
|
2017-05-18 23:11:20 +00:00
|
|
|
|
|
|
|
private:
|
2017-05-22 20:46:42 +00:00
|
|
|
void sendResponse(
|
|
|
|
int callId,
|
|
|
|
std::unique_ptr<v8_inspector::StringBuffer> message) override {
|
|
|
|
task_runner_->Append(
|
|
|
|
new SendMessageTask(this, ToVector(message->string())));
|
|
|
|
}
|
|
|
|
void sendNotification(
|
|
|
|
std::unique_ptr<v8_inspector::StringBuffer> message) override {
|
|
|
|
task_runner_->Append(
|
|
|
|
new SendMessageTask(this, ToVector(message->string())));
|
|
|
|
}
|
|
|
|
void flushProtocolNotifications() override {}
|
|
|
|
|
|
|
|
class SendMessageTask : public TaskRunner::Task {
|
|
|
|
public:
|
|
|
|
SendMessageTask(FrontendChannelImpl* channel,
|
|
|
|
const v8::internal::Vector<uint16_t>& message)
|
|
|
|
: channel_(channel), message_(message) {}
|
|
|
|
virtual ~SendMessageTask() {}
|
|
|
|
bool is_priority_task() final { return false; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Run(IsolateData* data) override {
|
|
|
|
v8::MicrotasksScope microtasks_scope(data->isolate(),
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
|
|
|
v8::HandleScope handle_scope(data->isolate());
|
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
data->GetContext(channel_->context_group_id_);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
v8::Local<v8::Value> message = ToV8String(data->isolate(), message_);
|
|
|
|
v8::MaybeLocal<v8::Value> result;
|
|
|
|
result = channel_->function_.Get(data->isolate())
|
|
|
|
->Call(context, context->Global(), 1, &message);
|
|
|
|
}
|
|
|
|
FrontendChannelImpl* channel_;
|
|
|
|
v8::internal::Vector<uint16_t> message_;
|
|
|
|
};
|
2017-05-18 23:11:20 +00:00
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
TaskRunner* task_runner_;
|
2017-05-18 23:11:20 +00:00
|
|
|
int context_group_id_;
|
2017-05-22 20:46:42 +00:00
|
|
|
v8::Global<v8::Function> function_;
|
|
|
|
int session_id_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(FrontendChannelImpl);
|
2017-05-18 23:11:20 +00:00
|
|
|
};
|
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
template <typename T>
|
|
|
|
void RunSyncTask(TaskRunner* task_runner, T callback) {
|
|
|
|
class SyncTask : public TaskRunner::Task {
|
|
|
|
public:
|
|
|
|
SyncTask(v8::base::Semaphore* ready_semaphore, T callback)
|
|
|
|
: ready_semaphore_(ready_semaphore), callback_(callback) {}
|
|
|
|
virtual ~SyncTask() = default;
|
|
|
|
bool is_priority_task() final { return true; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Run(IsolateData* data) override {
|
|
|
|
callback_(data);
|
|
|
|
if (ready_semaphore_) ready_semaphore_->Signal();
|
|
|
|
}
|
2017-05-18 23:11:20 +00:00
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
v8::base::Semaphore* ready_semaphore_;
|
|
|
|
T callback_;
|
|
|
|
};
|
2017-05-18 23:11:20 +00:00
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
v8::base::Semaphore ready_semaphore(0);
|
|
|
|
task_runner->Append(new SyncTask(&ready_semaphore, callback));
|
|
|
|
ready_semaphore.Wait();
|
|
|
|
}
|
2017-05-18 23:11:20 +00:00
|
|
|
|
|
|
|
class SendMessageToBackendTask : public TaskRunner::Task {
|
|
|
|
public:
|
2017-05-22 20:46:42 +00:00
|
|
|
SendMessageToBackendTask(int session_id,
|
|
|
|
const v8::internal::Vector<uint16_t>& message)
|
2017-05-18 23:11:20 +00:00
|
|
|
: session_id_(session_id), message_(message) {}
|
2017-05-22 20:46:42 +00:00
|
|
|
bool is_priority_task() final { return true; }
|
2017-05-18 23:11:20 +00:00
|
|
|
|
|
|
|
private:
|
2017-05-22 20:46:42 +00:00
|
|
|
void Run(IsolateData* data) override {
|
2017-05-18 23:11:20 +00:00
|
|
|
v8_inspector::StringView message_view(message_.start(), message_.length());
|
2017-05-22 20:46:42 +00:00
|
|
|
data->SendMessage(session_id_, message_view);
|
2017-05-18 23:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int session_id_;
|
|
|
|
v8::internal::Vector<uint16_t> message_;
|
|
|
|
};
|
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
void RunAsyncTask(TaskRunner* task_runner, const char* task_name,
|
|
|
|
TaskRunner::Task* task) {
|
|
|
|
class AsyncTask : public TaskRunner::Task {
|
|
|
|
public:
|
|
|
|
explicit AsyncTask(TaskRunner::Task* inner) : inner_(inner) {}
|
|
|
|
virtual ~AsyncTask() = default;
|
|
|
|
bool is_priority_task() override { return inner_->is_priority_task(); }
|
|
|
|
void Run(IsolateData* data) override {
|
|
|
|
data->AsyncTaskStarted(inner_.get());
|
|
|
|
inner_->Run(data);
|
|
|
|
data->AsyncTaskFinished(inner_.get());
|
|
|
|
}
|
2017-05-18 23:11:20 +00:00
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
private:
|
|
|
|
std::unique_ptr<TaskRunner::Task> inner_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AsyncTask);
|
|
|
|
};
|
2017-05-18 23:11:20 +00:00
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
task_runner->data()->AsyncTaskScheduled(
|
|
|
|
v8_inspector::StringView(reinterpret_cast<const uint8_t*>(task_name),
|
|
|
|
strlen(task_name)),
|
|
|
|
task, false);
|
|
|
|
task_runner->Append(new AsyncTask(task));
|
|
|
|
}
|
2017-05-18 23:11:20 +00:00
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
class ExecuteStringTask : public TaskRunner::Task {
|
2017-05-18 23:11:20 +00:00
|
|
|
public:
|
2017-05-22 20:46:42 +00:00
|
|
|
ExecuteStringTask(int context_group_id,
|
|
|
|
const v8::internal::Vector<uint16_t>& expression,
|
|
|
|
v8::Local<v8::String> name,
|
|
|
|
v8::Local<v8::Integer> line_offset,
|
|
|
|
v8::Local<v8::Integer> column_offset,
|
|
|
|
v8::Local<v8::Boolean> is_module)
|
|
|
|
: expression_(expression),
|
|
|
|
name_(ToVector(name)),
|
|
|
|
line_offset_(line_offset.As<v8::Int32>()->Value()),
|
|
|
|
column_offset_(column_offset.As<v8::Int32>()->Value()),
|
|
|
|
is_module_(is_module->Value()),
|
2017-05-18 23:11:20 +00:00
|
|
|
context_group_id_(context_group_id) {}
|
2017-05-22 20:46:42 +00:00
|
|
|
ExecuteStringTask(const v8::internal::Vector<const char>& expression,
|
|
|
|
int context_group_id)
|
|
|
|
: expression_utf8_(expression), context_group_id_(context_group_id) {}
|
|
|
|
bool is_priority_task() override { return false; }
|
|
|
|
void Run(IsolateData* data) override {
|
|
|
|
v8::MicrotasksScope microtasks_scope(data->isolate(),
|
2017-05-18 23:11:20 +00:00
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
2017-05-22 20:46:42 +00:00
|
|
|
v8::HandleScope handle_scope(data->isolate());
|
|
|
|
v8::Local<v8::Context> context = data->GetContext(context_group_id_);
|
2017-05-18 23:11:20 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2017-05-22 20:46:42 +00:00
|
|
|
v8::ScriptOrigin origin(
|
|
|
|
ToV8String(data->isolate(), name_),
|
|
|
|
v8::Integer::New(data->isolate(), line_offset_),
|
|
|
|
v8::Integer::New(data->isolate(), 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>(),
|
|
|
|
v8::Boolean::New(data->isolate(), is_module_));
|
|
|
|
v8::Local<v8::String> source;
|
|
|
|
if (expression_.length())
|
|
|
|
source = ToV8String(data->isolate(), expression_);
|
|
|
|
else
|
|
|
|
source = ToV8String(data->isolate(), expression_utf8_.start(),
|
|
|
|
expression_utf8_.length());
|
2017-05-18 23:11:20 +00:00
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
v8::ScriptCompiler::Source scriptSource(source, origin);
|
|
|
|
if (!is_module_) {
|
|
|
|
v8::Local<v8::Script> script;
|
|
|
|
if (!v8::ScriptCompiler::Compile(context, &scriptSource).ToLocal(&script))
|
|
|
|
return;
|
|
|
|
v8::MaybeLocal<v8::Value> result;
|
|
|
|
result = script->Run(context);
|
|
|
|
} else {
|
|
|
|
data->RegisterModule(context, name_, &scriptSource);
|
|
|
|
}
|
2017-05-18 23:11:20 +00:00
|
|
|
}
|
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
private:
|
|
|
|
v8::internal::Vector<uint16_t> expression_;
|
|
|
|
v8::internal::Vector<const char> expression_utf8_;
|
|
|
|
v8::internal::Vector<uint16_t> name_;
|
|
|
|
int32_t line_offset_ = 0;
|
|
|
|
int32_t column_offset_ = 0;
|
|
|
|
bool is_module_ = false;
|
2017-05-18 23:11:20 +00:00
|
|
|
int context_group_id_;
|
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(ExecuteStringTask);
|
|
|
|
};
|
2017-05-18 23:11:20 +00:00
|
|
|
|
2017-05-16 23:14:46 +00:00
|
|
|
class UtilsExtension : public IsolateData::SetupGlobalTask {
|
2016-10-02 18:10:27 +00:00
|
|
|
public:
|
2017-04-20 18:04:24 +00:00
|
|
|
~UtilsExtension() override = default;
|
|
|
|
void Run(v8::Isolate* isolate,
|
2017-04-21 23:41:07 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global) override {
|
2017-04-20 18:04:24 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> utils = v8::ObjectTemplate::New(isolate);
|
|
|
|
utils->Set(ToV8String(isolate, "print"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &UtilsExtension::Print));
|
|
|
|
utils->Set(ToV8String(isolate, "quit"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &UtilsExtension::Quit));
|
|
|
|
utils->Set(ToV8String(isolate, "setlocale"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &UtilsExtension::Setlocale));
|
|
|
|
utils->Set(ToV8String(isolate, "read"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &UtilsExtension::Read));
|
|
|
|
utils->Set(ToV8String(isolate, "load"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &UtilsExtension::Load));
|
|
|
|
utils->Set(ToV8String(isolate, "compileAndRunWithOrigin"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &UtilsExtension::CompileAndRunWithOrigin));
|
|
|
|
utils->Set(ToV8String(isolate, "setCurrentTimeMSForTest"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &UtilsExtension::SetCurrentTimeMSForTest));
|
|
|
|
utils->Set(ToV8String(isolate, "setMemoryInfoForTest"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &UtilsExtension::SetMemoryInfoForTest));
|
|
|
|
utils->Set(ToV8String(isolate, "schedulePauseOnNextStatement"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &UtilsExtension::SchedulePauseOnNextStatement));
|
|
|
|
utils->Set(ToV8String(isolate, "cancelPauseOnNextStatement"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &UtilsExtension::CancelPauseOnNextStatement));
|
|
|
|
utils->Set(ToV8String(isolate, "setLogConsoleApiMessageCalls"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &UtilsExtension::SetLogConsoleApiMessageCalls));
|
|
|
|
utils->Set(ToV8String(isolate, "createContextGroup"),
|
|
|
|
v8::FunctionTemplate::New(isolate,
|
|
|
|
&UtilsExtension::CreateContextGroup));
|
2017-05-18 23:11:20 +00:00
|
|
|
utils->Set(
|
|
|
|
ToV8String(isolate, "connectSession"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &UtilsExtension::ConnectSession));
|
|
|
|
utils->Set(
|
|
|
|
ToV8String(isolate, "disconnectSession"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &UtilsExtension::DisconnectSession));
|
|
|
|
utils->Set(ToV8String(isolate, "sendMessageToBackend"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &UtilsExtension::SendMessageToBackend));
|
2017-04-21 23:41:07 +00:00
|
|
|
global->Set(ToV8String(isolate, "utils"), utils);
|
2016-10-02 18:10:27 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 19:59:11 +00:00
|
|
|
static void set_backend_task_runner(TaskRunner* runner) {
|
|
|
|
backend_runner_ = runner;
|
|
|
|
}
|
|
|
|
|
2016-10-02 18:10:27 +00:00
|
|
|
private:
|
2016-11-04 19:59:11 +00:00
|
|
|
static TaskRunner* backend_runner_;
|
|
|
|
|
2016-10-02 18:10:27 +00:00
|
|
|
static void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
for (int i = 0; i < args.Length(); i++) {
|
|
|
|
v8::HandleScope handle_scope(args.GetIsolate());
|
|
|
|
if (i != 0) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicitly catch potential exceptions in toString().
|
|
|
|
v8::TryCatch try_catch(args.GetIsolate());
|
|
|
|
v8::Local<v8::Value> arg = args[i];
|
|
|
|
v8::Local<v8::String> str_obj;
|
|
|
|
|
|
|
|
if (arg->IsSymbol()) {
|
|
|
|
arg = v8::Local<v8::Symbol>::Cast(arg)->Name();
|
|
|
|
}
|
|
|
|
if (!arg->ToString(args.GetIsolate()->GetCurrentContext())
|
|
|
|
.ToLocal(&str_obj)) {
|
|
|
|
try_catch.ReThrow();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::String::Utf8Value str(str_obj);
|
|
|
|
int n =
|
|
|
|
static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout));
|
|
|
|
if (n != str.length()) {
|
|
|
|
printf("Error in fwrite\n");
|
|
|
|
Quit(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { Exit(); }
|
2016-10-13 20:31:17 +00:00
|
|
|
|
2017-04-20 18:04:24 +00:00
|
|
|
static void Setlocale(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2016-10-13 20:31:17 +00:00
|
|
|
if (args.Length() != 1 || !args[0]->IsString()) {
|
|
|
|
fprintf(stderr, "Internal error: setlocale get one string argument.");
|
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
v8::String::Utf8Value str(args[0]);
|
|
|
|
setlocale(LC_NUMERIC, *str);
|
|
|
|
}
|
2016-10-18 09:58:20 +00:00
|
|
|
|
2017-02-27 20:20:39 +00:00
|
|
|
static bool ReadFile(v8::Isolate* isolate, v8::Local<v8::Value> name,
|
|
|
|
v8::internal::Vector<const char>* chars) {
|
|
|
|
v8::String::Utf8Value str(name);
|
2016-10-18 09:58:20 +00:00
|
|
|
bool exists = false;
|
|
|
|
std::string filename(*str, str.length());
|
2017-02-27 20:20:39 +00:00
|
|
|
*chars = v8::internal::ReadFile(filename.c_str(), &exists);
|
2016-10-18 09:58:20 +00:00
|
|
|
if (!exists) {
|
2017-05-22 20:46:42 +00:00
|
|
|
isolate->ThrowException(ToV8String(isolate, "Error reading file"));
|
2017-02-27 20:20:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 1 || !args[0]->IsString()) {
|
|
|
|
fprintf(stderr, "Internal error: read gets one string argument.");
|
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
v8::internal::Vector<const char> chars;
|
|
|
|
v8::Isolate* isolate = args.GetIsolate();
|
2017-05-22 20:46:42 +00:00
|
|
|
if (ReadFile(isolate, args[0], &chars))
|
|
|
|
args.GetReturnValue().Set(ToV8String(isolate, chars.start()));
|
2017-02-27 20:20:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 1 || !args[0]->IsString()) {
|
|
|
|
fprintf(stderr, "Internal error: load gets one string argument.");
|
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
v8::internal::Vector<const char> chars;
|
|
|
|
v8::Isolate* isolate = args.GetIsolate();
|
2017-05-18 23:11:20 +00:00
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
|
|
IsolateData* data = IsolateData::FromContext(context);
|
|
|
|
int context_group_id = data->GetContextGroupId(context);
|
2017-02-27 20:20:39 +00:00
|
|
|
if (ReadFile(isolate, args[0], &chars)) {
|
2017-05-22 20:46:42 +00:00
|
|
|
ExecuteStringTask(chars, context_group_id).Run(data);
|
2016-10-18 09:58:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 19:59:11 +00:00
|
|
|
|
|
|
|
static void CompileAndRunWithOrigin(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2017-05-18 23:11:20 +00:00
|
|
|
if (args.Length() != 6 || !args[0]->IsInt32() || !args[1]->IsString() ||
|
|
|
|
!args[2]->IsString() || !args[3]->IsInt32() || !args[4]->IsInt32() ||
|
|
|
|
!args[5]->IsBoolean()) {
|
2016-11-04 19:59:11 +00:00
|
|
|
fprintf(stderr,
|
2017-05-18 23:11:20 +00:00
|
|
|
"Internal error: compileAndRunWithOrigin(context_group_id, "
|
|
|
|
"source, name, line, "
|
2017-01-31 00:19:41 +00:00
|
|
|
"column, is_module).");
|
2016-11-04 19:59:11 +00:00
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
backend_runner_->Append(new ExecuteStringTask(
|
2017-05-22 20:46:42 +00:00
|
|
|
args[0].As<v8::Int32>()->Value(), ToVector(args[1].As<v8::String>()),
|
|
|
|
args[2].As<v8::String>(), args[3].As<v8::Int32>(),
|
|
|
|
args[4].As<v8::Int32>(), args[5].As<v8::Boolean>()));
|
2016-11-04 19:59:11 +00:00
|
|
|
}
|
2017-01-17 20:21:38 +00:00
|
|
|
|
|
|
|
static void SetCurrentTimeMSForTest(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 1 || !args[0]->IsNumber()) {
|
|
|
|
fprintf(stderr, "Internal error: setCurrentTimeMSForTest(time).");
|
|
|
|
Exit();
|
|
|
|
}
|
2017-05-22 00:48:27 +00:00
|
|
|
backend_runner_->data()->SetCurrentTimeMS(
|
2017-01-17 20:21:38 +00:00
|
|
|
args[0].As<v8::Number>()->Value());
|
|
|
|
}
|
2017-01-18 16:57:00 +00:00
|
|
|
|
2017-03-29 21:40:07 +00:00
|
|
|
static void SetMemoryInfoForTest(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 1) {
|
|
|
|
fprintf(stderr, "Internal error: setMemoryInfoForTest(value).");
|
|
|
|
Exit();
|
|
|
|
}
|
2017-05-22 00:48:27 +00:00
|
|
|
backend_runner_->data()->SetMemoryInfo(args[0]);
|
2017-03-29 21:40:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:57:00 +00:00
|
|
|
static void SchedulePauseOnNextStatement(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2017-05-18 23:11:20 +00:00
|
|
|
if (args.Length() != 3 || !args[0]->IsInt32() || !args[1]->IsString() ||
|
|
|
|
!args[2]->IsString()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Internal error: schedulePauseOnNextStatement(context_group_id, "
|
|
|
|
"'reason', 'details').");
|
2017-01-18 16:57:00 +00:00
|
|
|
Exit();
|
|
|
|
}
|
2017-05-18 23:11:20 +00:00
|
|
|
v8::internal::Vector<uint16_t> reason = ToVector(args[1].As<v8::String>());
|
|
|
|
v8::internal::Vector<uint16_t> details = ToVector(args[2].As<v8::String>());
|
2017-05-22 20:46:42 +00:00
|
|
|
int context_group_id = args[0].As<v8::Int32>()->Value();
|
|
|
|
RunSyncTask(backend_runner_, [&context_group_id, &reason,
|
|
|
|
&details](IsolateData* data) {
|
|
|
|
data->SchedulePauseOnNextStatement(
|
|
|
|
context_group_id,
|
|
|
|
v8_inspector::StringView(reason.start(), reason.length()),
|
|
|
|
v8_inspector::StringView(details.start(), details.length()));
|
|
|
|
});
|
2017-01-18 16:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void CancelPauseOnNextStatement(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2017-05-18 23:11:20 +00:00
|
|
|
if (args.Length() != 1 || !args[0]->IsInt32()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Internal error: cancelPauseOnNextStatement(context_group_id).");
|
2017-01-18 16:57:00 +00:00
|
|
|
Exit();
|
|
|
|
}
|
2017-05-22 20:46:42 +00:00
|
|
|
int context_group_id = args[0].As<v8::Int32>()->Value();
|
|
|
|
RunSyncTask(backend_runner_, [&context_group_id](IsolateData* data) {
|
|
|
|
data->CancelPauseOnNextStatement(context_group_id);
|
|
|
|
});
|
2017-01-18 16:57:00 +00:00
|
|
|
}
|
2017-02-28 20:22:24 +00:00
|
|
|
|
2017-05-18 23:11:20 +00:00
|
|
|
static void SetLogConsoleApiMessageCalls(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 1 || !args[0]->IsBoolean()) {
|
|
|
|
fprintf(stderr, "Internal error: setLogConsoleApiMessageCalls(bool).");
|
|
|
|
Exit();
|
|
|
|
}
|
2017-05-22 00:48:27 +00:00
|
|
|
backend_runner_->data()->SetLogConsoleApiMessageCalls(
|
2017-05-18 23:11:20 +00:00
|
|
|
args[0].As<v8::Boolean>()->Value());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CreateContextGroup(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2017-02-28 20:22:24 +00:00
|
|
|
if (args.Length() != 0) {
|
2017-05-18 23:11:20 +00:00
|
|
|
fprintf(stderr, "Internal error: createContextGroup().");
|
2017-02-28 20:22:24 +00:00
|
|
|
Exit();
|
|
|
|
}
|
2017-05-18 23:11:20 +00:00
|
|
|
int context_group_id = 0;
|
2017-05-22 20:46:42 +00:00
|
|
|
RunSyncTask(backend_runner_, [&context_group_id](IsolateData* data) {
|
|
|
|
context_group_id = data->CreateContextGroup();
|
|
|
|
});
|
2017-05-18 23:11:20 +00:00
|
|
|
args.GetReturnValue().Set(
|
|
|
|
v8::Int32::New(args.GetIsolate(), context_group_id));
|
2017-02-28 20:22:24 +00:00
|
|
|
}
|
2017-03-07 22:30:05 +00:00
|
|
|
|
2017-05-18 23:11:20 +00:00
|
|
|
static void ConnectSession(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 3 || !args[0]->IsInt32() || !args[1]->IsString() ||
|
|
|
|
!args[2]->IsFunction()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Internal error: connectionSession(context_group_id, state, "
|
|
|
|
"dispatch).");
|
2017-04-25 22:55:45 +00:00
|
|
|
Exit();
|
|
|
|
}
|
2017-05-22 20:46:42 +00:00
|
|
|
v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext();
|
|
|
|
FrontendChannelImpl* channel = new FrontendChannelImpl(
|
|
|
|
IsolateData::FromContext(context)->task_runner(),
|
|
|
|
IsolateData::FromContext(context)->GetContextGroupId(context),
|
|
|
|
args.GetIsolate(), args[2].As<v8::Function>());
|
|
|
|
|
2017-05-18 23:11:20 +00:00
|
|
|
v8::internal::Vector<uint16_t> state = ToVector(args[1].As<v8::String>());
|
2017-05-22 20:46:42 +00:00
|
|
|
int context_group_id = args[0].As<v8::Int32>()->Value();
|
2017-05-18 23:11:20 +00:00
|
|
|
int session_id = 0;
|
2017-05-22 20:46:42 +00:00
|
|
|
RunSyncTask(backend_runner_, [&context_group_id, &session_id, &channel,
|
|
|
|
&state](IsolateData* data) {
|
|
|
|
session_id = data->ConnectSession(
|
|
|
|
context_group_id,
|
|
|
|
v8_inspector::StringView(state.start(), state.length()), channel);
|
|
|
|
channel->set_session_id(session_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
channels_[session_id].reset(channel);
|
2017-05-18 23:11:20 +00:00
|
|
|
args.GetReturnValue().Set(v8::Int32::New(args.GetIsolate(), session_id));
|
2017-04-25 22:55:45 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 23:11:20 +00:00
|
|
|
static void DisconnectSession(
|
2017-04-18 15:53:08 +00:00
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2017-05-18 23:11:20 +00:00
|
|
|
if (args.Length() != 1 || !args[0]->IsInt32()) {
|
|
|
|
fprintf(stderr, "Internal error: disconnectionSession(session_id).");
|
2017-04-18 15:53:08 +00:00
|
|
|
Exit();
|
|
|
|
}
|
2017-05-18 23:11:20 +00:00
|
|
|
int session_id = args[0].As<v8::Int32>()->Value();
|
|
|
|
v8::internal::Vector<uint16_t> state;
|
2017-05-22 20:46:42 +00:00
|
|
|
RunSyncTask(backend_runner_, [&session_id, &state](IsolateData* data) {
|
|
|
|
state = ToVector(data->DisconnectSession(session_id)->string());
|
|
|
|
});
|
|
|
|
channels_.erase(session_id);
|
|
|
|
args.GetReturnValue().Set(ToV8String(args.GetIsolate(), state));
|
2017-04-18 15:53:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 23:11:20 +00:00
|
|
|
static void SendMessageToBackend(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 2 || !args[0]->IsInt32() || !args[1]->IsString()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Internal error: sendMessageToBackend(session_id, message).");
|
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
backend_runner_->Append(new SendMessageToBackendTask(
|
|
|
|
args[0].As<v8::Int32>()->Value(), ToVector(args[1].As<v8::String>())));
|
|
|
|
}
|
2017-05-22 20:46:42 +00:00
|
|
|
|
|
|
|
static std::map<int, std::unique_ptr<FrontendChannelImpl>> channels_;
|
2016-10-02 18:10:27 +00:00
|
|
|
};
|
|
|
|
|
2016-11-04 19:59:11 +00:00
|
|
|
TaskRunner* UtilsExtension::backend_runner_ = nullptr;
|
2017-05-22 20:46:42 +00:00
|
|
|
std::map<int, std::unique_ptr<FrontendChannelImpl>> UtilsExtension::channels_;
|
2016-11-04 19:59:11 +00:00
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
class SetTimeoutTask : public TaskRunner::Task {
|
2016-10-02 18:10:27 +00:00
|
|
|
public:
|
2017-05-22 20:46:42 +00:00
|
|
|
SetTimeoutTask(int context_group_id, v8::Isolate* isolate,
|
2017-05-18 23:11:20 +00:00
|
|
|
v8::Local<v8::Function> function)
|
2017-05-22 20:46:42 +00:00
|
|
|
: function_(isolate, function), context_group_id_(context_group_id) {}
|
2016-10-02 18:10:27 +00:00
|
|
|
virtual ~SetTimeoutTask() {}
|
2017-05-22 20:46:42 +00:00
|
|
|
bool is_priority_task() final { return false; }
|
2016-10-02 18:10:27 +00:00
|
|
|
|
2017-05-16 20:38:33 +00:00
|
|
|
private:
|
2017-05-22 20:46:42 +00:00
|
|
|
void Run(IsolateData* data) override {
|
|
|
|
v8::MicrotasksScope microtasks_scope(data->isolate(),
|
2016-10-02 18:10:27 +00:00
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
2017-05-22 20:46:42 +00:00
|
|
|
v8::HandleScope handle_scope(data->isolate());
|
|
|
|
v8::Local<v8::Context> context = data->GetContext(context_group_id_);
|
2016-10-02 18:10:27 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
2017-05-22 20:46:42 +00:00
|
|
|
v8::Local<v8::Function> function = function_.Get(data->isolate());
|
2016-10-02 18:10:27 +00:00
|
|
|
v8::MaybeLocal<v8::Value> result;
|
|
|
|
result = function->Call(context, context->Global(), 0, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Global<v8::Function> function_;
|
2017-05-18 23:11:20 +00:00
|
|
|
int context_group_id_;
|
2016-10-02 18:10:27 +00:00
|
|
|
};
|
|
|
|
|
2017-05-16 23:14:46 +00:00
|
|
|
class SetTimeoutExtension : public IsolateData::SetupGlobalTask {
|
2016-10-02 18:10:27 +00:00
|
|
|
public:
|
2017-04-21 23:41:07 +00:00
|
|
|
void Run(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::ObjectTemplate> global) override {
|
|
|
|
global->Set(
|
|
|
|
ToV8String(isolate, "setTimeout"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &SetTimeoutExtension::SetTimeout));
|
2016-10-02 18:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void SetTimeout(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2016-10-05 15:07:57 +00:00
|
|
|
if (args.Length() != 2 || !args[1]->IsNumber() ||
|
|
|
|
(!args[0]->IsFunction() && !args[0]->IsString()) ||
|
2016-10-02 18:10:27 +00:00
|
|
|
args[1].As<v8::Number>()->Value() != 0.0) {
|
2017-05-18 23:11:20 +00:00
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"Internal error: only setTimeout(function|code, 0) is supported.");
|
2016-10-02 18:10:27 +00:00
|
|
|
Exit();
|
|
|
|
}
|
2016-11-04 19:59:11 +00:00
|
|
|
v8::Isolate* isolate = args.GetIsolate();
|
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
2017-05-18 23:11:20 +00:00
|
|
|
IsolateData* data = IsolateData::FromContext(context);
|
|
|
|
int context_group_id = data->GetContextGroupId(context);
|
2016-10-05 15:07:57 +00:00
|
|
|
if (args[0]->IsFunction()) {
|
2017-05-22 20:46:42 +00:00
|
|
|
RunAsyncTask(data->task_runner(), "setTimeout",
|
|
|
|
new SetTimeoutTask(context_group_id, isolate,
|
|
|
|
v8::Local<v8::Function>::Cast(args[0])));
|
2016-11-04 19:59:11 +00:00
|
|
|
} else {
|
2017-05-22 20:46:42 +00:00
|
|
|
RunAsyncTask(
|
|
|
|
data->task_runner(), "setTimeout",
|
|
|
|
new ExecuteStringTask(
|
|
|
|
context_group_id, ToVector(args[0].As<v8::String>()),
|
|
|
|
v8::String::Empty(isolate), v8::Integer::New(isolate, 0),
|
|
|
|
v8::Integer::New(isolate, 0), v8::Boolean::New(isolate, false)));
|
2016-10-05 15:07:57 +00:00
|
|
|
}
|
2016-10-02 18:10:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-04 01:21:58 +00:00
|
|
|
bool StrictAccessCheck(v8::Local<v8::Context> accessing_context,
|
|
|
|
v8::Local<v8::Object> accessed_object,
|
|
|
|
v8::Local<v8::Value> data) {
|
|
|
|
CHECK(accessing_context.IsEmpty());
|
|
|
|
return accessing_context.IsEmpty();
|
|
|
|
}
|
|
|
|
|
2017-05-16 23:14:46 +00:00
|
|
|
class InspectorExtension : public IsolateData::SetupGlobalTask {
|
2016-10-26 20:26:56 +00:00
|
|
|
public:
|
2017-04-21 23:41:07 +00:00
|
|
|
~InspectorExtension() override = default;
|
|
|
|
void Run(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::ObjectTemplate> global) override {
|
|
|
|
v8::Local<v8::ObjectTemplate> inspector = v8::ObjectTemplate::New(isolate);
|
2017-05-18 23:11:20 +00:00
|
|
|
inspector->Set(ToV8String(isolate, "fireContextCreated"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &InspectorExtension::FireContextCreated));
|
|
|
|
inspector->Set(ToV8String(isolate, "fireContextDestroyed"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &InspectorExtension::FireContextDestroyed));
|
2017-08-01 21:17:31 +00:00
|
|
|
inspector->Set(
|
|
|
|
ToV8String(isolate, "freeContext"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &InspectorExtension::FreeContext));
|
2017-06-05 17:37:25 +00:00
|
|
|
inspector->Set(ToV8String(isolate, "addInspectedObject"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &InspectorExtension::AddInspectedObject));
|
2017-04-21 23:41:07 +00:00
|
|
|
inspector->Set(ToV8String(isolate, "setMaxAsyncTaskStacks"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &InspectorExtension::SetMaxAsyncTaskStacks));
|
|
|
|
inspector->Set(
|
|
|
|
ToV8String(isolate, "dumpAsyncTaskStacksStateForTest"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &InspectorExtension::DumpAsyncTaskStacksStateForTest));
|
|
|
|
inspector->Set(
|
|
|
|
ToV8String(isolate, "breakProgram"),
|
|
|
|
v8::FunctionTemplate::New(isolate, &InspectorExtension::BreakProgram));
|
|
|
|
inspector->Set(
|
|
|
|
ToV8String(isolate, "createObjectWithStrictCheck"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &InspectorExtension::CreateObjectWithStrictCheck));
|
|
|
|
inspector->Set(ToV8String(isolate, "callWithScheduledBreak"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &InspectorExtension::CallWithScheduledBreak));
|
|
|
|
inspector->Set(ToV8String(isolate, "allowAccessorFormatting"),
|
|
|
|
v8::FunctionTemplate::New(
|
|
|
|
isolate, &InspectorExtension::AllowAccessorFormatting));
|
|
|
|
global->Set(ToV8String(isolate, "inspector"), inspector);
|
2016-10-26 20:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-05-18 23:11:20 +00:00
|
|
|
static void FireContextCreated(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext();
|
|
|
|
IsolateData* data = IsolateData::FromContext(context);
|
2017-05-22 00:48:27 +00:00
|
|
|
data->FireContextCreated(context, data->GetContextGroupId(context));
|
2016-10-26 20:26:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 23:11:20 +00:00
|
|
|
static void FireContextDestroyed(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext();
|
|
|
|
IsolateData* data = IsolateData::FromContext(context);
|
2017-05-22 00:48:27 +00:00
|
|
|
data->FireContextDestroyed(context);
|
2016-10-26 20:26:56 +00:00
|
|
|
}
|
2016-12-18 17:04:40 +00:00
|
|
|
|
2017-08-01 21:17:31 +00:00
|
|
|
static void FreeContext(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext();
|
|
|
|
IsolateData* data = IsolateData::FromContext(context);
|
|
|
|
data->FreeContext(context);
|
|
|
|
}
|
|
|
|
|
2017-06-05 17:37:25 +00:00
|
|
|
static void AddInspectedObject(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 2 || !args[0]->IsInt32()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Internal error: addInspectedObject(session_id, object).");
|
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext();
|
|
|
|
IsolateData* data = IsolateData::FromContext(context);
|
|
|
|
data->AddInspectedObject(args[0].As<v8::Int32>()->Value(), args[1]);
|
|
|
|
}
|
|
|
|
|
2016-12-18 17:04:40 +00:00
|
|
|
static void SetMaxAsyncTaskStacks(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 1 || !args[0]->IsInt32()) {
|
|
|
|
fprintf(stderr, "Internal error: setMaxAsyncTaskStacks(max).");
|
|
|
|
Exit();
|
|
|
|
}
|
2017-05-22 20:46:42 +00:00
|
|
|
IsolateData::FromContext(args.GetIsolate()->GetCurrentContext())
|
|
|
|
->SetMaxAsyncTaskStacksForTest(args[0].As<v8::Int32>()->Value());
|
2016-12-18 17:04:40 +00:00
|
|
|
}
|
2017-01-18 16:57:00 +00:00
|
|
|
|
2017-04-20 15:49:02 +00:00
|
|
|
static void DumpAsyncTaskStacksStateForTest(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 0) {
|
|
|
|
fprintf(stderr, "Internal error: dumpAsyncTaskStacksStateForTest().");
|
|
|
|
Exit();
|
|
|
|
}
|
2017-05-22 20:46:42 +00:00
|
|
|
IsolateData::FromContext(args.GetIsolate()->GetCurrentContext())
|
|
|
|
->DumpAsyncTaskStacksStateForTest();
|
2017-04-20 15:49:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:57:00 +00:00
|
|
|
static void BreakProgram(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) {
|
|
|
|
fprintf(stderr, "Internal error: breakProgram('reason', 'details').");
|
|
|
|
Exit();
|
|
|
|
}
|
2017-05-18 23:11:20 +00:00
|
|
|
v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext();
|
|
|
|
IsolateData* data = IsolateData::FromContext(context);
|
2017-01-18 16:57:00 +00:00
|
|
|
v8::internal::Vector<uint16_t> reason = ToVector(args[0].As<v8::String>());
|
|
|
|
v8_inspector::StringView reason_view(reason.start(), reason.length());
|
|
|
|
v8::internal::Vector<uint16_t> details = ToVector(args[1].As<v8::String>());
|
|
|
|
v8_inspector::StringView details_view(details.start(), details.length());
|
2017-05-22 00:48:27 +00:00
|
|
|
data->BreakProgram(data->GetContextGroupId(context), reason_view,
|
|
|
|
details_view);
|
2017-01-18 16:57:00 +00:00
|
|
|
}
|
2017-02-04 01:21:58 +00:00
|
|
|
|
|
|
|
static void CreateObjectWithStrictCheck(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 0) {
|
|
|
|
fprintf(stderr, "Internal error: createObjectWithStrictCheck().");
|
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
v8::Local<v8::ObjectTemplate> templ =
|
|
|
|
v8::ObjectTemplate::New(args.GetIsolate());
|
|
|
|
templ->SetAccessCheckCallback(&StrictAccessCheck);
|
|
|
|
args.GetReturnValue().Set(
|
|
|
|
templ->NewInstance(args.GetIsolate()->GetCurrentContext())
|
|
|
|
.ToLocalChecked());
|
|
|
|
}
|
2017-02-08 01:42:54 +00:00
|
|
|
|
|
|
|
static void CallWithScheduledBreak(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 3 || !args[0]->IsFunction() || !args[1]->IsString() ||
|
|
|
|
!args[2]->IsString()) {
|
2017-05-18 23:11:20 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Internal error: callWithScheduledBreak('reason', 'details').");
|
2017-02-08 01:42:54 +00:00
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
v8::internal::Vector<uint16_t> reason = ToVector(args[1].As<v8::String>());
|
|
|
|
v8_inspector::StringView reason_view(reason.start(), reason.length());
|
|
|
|
v8::internal::Vector<uint16_t> details = ToVector(args[2].As<v8::String>());
|
|
|
|
v8_inspector::StringView details_view(details.start(), details.length());
|
|
|
|
v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext();
|
2017-05-18 23:11:20 +00:00
|
|
|
IsolateData* data = IsolateData::FromContext(context);
|
|
|
|
int context_group_id = data->GetContextGroupId(context);
|
2017-05-22 00:48:27 +00:00
|
|
|
data->SchedulePauseOnNextStatement(context_group_id, reason_view,
|
|
|
|
details_view);
|
2017-02-08 01:42:54 +00:00
|
|
|
v8::MaybeLocal<v8::Value> result;
|
|
|
|
result = args[0].As<v8::Function>()->Call(context, context->Global(), 0,
|
|
|
|
nullptr);
|
2017-05-22 00:48:27 +00:00
|
|
|
data->CancelPauseOnNextStatement(context_group_id);
|
2017-02-08 01:42:54 +00:00
|
|
|
}
|
2017-03-03 19:30:40 +00:00
|
|
|
|
|
|
|
static void AllowAccessorFormatting(
|
|
|
|
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
if (args.Length() != 1 || !args[0]->IsObject()) {
|
|
|
|
fprintf(stderr, "Internal error: allowAccessorFormatting('object').");
|
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
v8::Local<v8::Object> object = args[0].As<v8::Object>();
|
|
|
|
v8::Isolate* isolate = args.GetIsolate();
|
|
|
|
v8::Local<v8::Private> shouldFormatAccessorsPrivate = v8::Private::ForApi(
|
2017-05-22 20:46:42 +00:00
|
|
|
isolate, ToV8String(isolate, "allowAccessorFormatting"));
|
2017-03-03 19:30:40 +00:00
|
|
|
object
|
|
|
|
->SetPrivate(isolate->GetCurrentContext(), shouldFormatAccessorsPrivate,
|
|
|
|
v8::Null(isolate))
|
|
|
|
.ToChecked();
|
|
|
|
}
|
2016-10-26 20:26:56 +00:00
|
|
|
};
|
|
|
|
|
2016-10-02 18:10:27 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
v8::V8::InitializeICUDefaultLocation(argv[0]);
|
|
|
|
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
|
|
|
|
v8::V8::InitializePlatform(platform);
|
2016-10-18 20:07:41 +00:00
|
|
|
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
|
2016-10-02 18:10:27 +00:00
|
|
|
v8::V8::InitializeExternalStartupData(argv[0]);
|
|
|
|
v8::V8::Initialize();
|
|
|
|
|
|
|
|
v8::base::Semaphore ready_semaphore(0);
|
|
|
|
|
2017-04-26 15:13:14 +00:00
|
|
|
v8::StartupData startup_data = {nullptr, 0};
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if (strcmp(argv[i], "--embed") == 0) {
|
|
|
|
argv[i++] = nullptr;
|
|
|
|
printf("Embedding script '%s'\n", argv[i]);
|
|
|
|
startup_data = v8::V8::CreateSnapshotDataBlob(argv[i]);
|
|
|
|
argv[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 23:14:46 +00:00
|
|
|
IsolateData::SetupGlobalTasks frontend_extensions;
|
2017-04-21 23:41:07 +00:00
|
|
|
frontend_extensions.emplace_back(new UtilsExtension());
|
|
|
|
TaskRunner frontend_runner(std::move(frontend_extensions), true,
|
2017-05-22 20:46:42 +00:00
|
|
|
&ready_semaphore, nullptr, false);
|
2017-05-18 23:11:20 +00:00
|
|
|
ready_semaphore.Wait();
|
|
|
|
|
|
|
|
int frontend_context_group_id = 0;
|
2017-05-22 20:46:42 +00:00
|
|
|
RunSyncTask(&frontend_runner,
|
|
|
|
[&frontend_context_group_id](IsolateData* data) {
|
|
|
|
frontend_context_group_id = data->CreateContextGroup();
|
|
|
|
});
|
2016-10-02 18:10:27 +00:00
|
|
|
|
2017-05-18 23:11:20 +00:00
|
|
|
IsolateData::SetupGlobalTasks backend_extensions;
|
|
|
|
backend_extensions.emplace_back(new SetTimeoutExtension());
|
|
|
|
backend_extensions.emplace_back(new InspectorExtension());
|
2017-05-22 20:46:42 +00:00
|
|
|
TaskRunner backend_runner(std::move(backend_extensions), false,
|
|
|
|
&ready_semaphore,
|
|
|
|
startup_data.data ? &startup_data : nullptr, true);
|
2016-10-02 18:10:27 +00:00
|
|
|
ready_semaphore.Wait();
|
2017-05-18 23:11:20 +00:00
|
|
|
UtilsExtension::set_backend_task_runner(&backend_runner);
|
2016-10-02 18:10:27 +00:00
|
|
|
|
2016-10-19 02:04:34 +00:00
|
|
|
task_runners.push_back(&frontend_runner);
|
|
|
|
task_runners.push_back(&backend_runner);
|
|
|
|
|
2016-10-02 18:10:27 +00:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
2017-03-29 18:08:51 +00:00
|
|
|
// Ignore unknown flags.
|
2017-04-26 15:13:14 +00:00
|
|
|
if (argv[i] == nullptr || argv[i][0] == '-') continue;
|
2016-10-02 18:10:27 +00:00
|
|
|
|
|
|
|
bool exists = false;
|
|
|
|
v8::internal::Vector<const char> chars =
|
|
|
|
v8::internal::ReadFile(argv[i], &exists, true);
|
|
|
|
if (!exists) {
|
|
|
|
fprintf(stderr, "Internal error: script file doesn't exists: %s\n",
|
|
|
|
argv[i]);
|
|
|
|
Exit();
|
|
|
|
}
|
2017-05-18 23:11:20 +00:00
|
|
|
frontend_runner.Append(
|
|
|
|
new ExecuteStringTask(chars, frontend_context_group_id));
|
2016-10-02 18:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
frontend_runner.Join();
|
2016-10-19 02:04:34 +00:00
|
|
|
backend_runner.Join();
|
2017-04-26 15:13:14 +00:00
|
|
|
|
|
|
|
delete startup_data.data;
|
2016-10-02 18:10:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|