Commit f3f9f444 authored by kozyatinskiy's avatar kozyatinskiy Committed by Commit bot

[inspector] added inspector test runner [part 3]

- added test runner, that takes file names and V8 flags as arguments and run scripts from passed files with passed flags in frontend context

BUG=chromium:635948
R=dgozman@chromium.org,alph@chromium.org

Review-Url: https://codereview.chromium.org/2372793002
Cr-Commit-Position: refs/heads/master@{#39891}
parent 6e03b72d
......@@ -10,6 +10,7 @@ v8_executable("inspector-test") {
sources = [
"inspector-impl.cc",
"inspector-impl.h",
"inspector-test.cc",
"task-runner.cc",
"task-runner.h",
]
......
......@@ -2,6 +2,9 @@ include_rules = [
"-src",
"+src/base/macros.h",
"+src/base/platform/platform.h",
"+src/flags.h",
"+src/inspector/string-16.h",
"+src/locked-queue-inl.h",
"+src/utils.h",
"+src/vector.h",
]
\ No newline at end of file
......@@ -32,6 +32,14 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel {
DISALLOW_COPY_AND_ASSIGN(ChannelImpl);
};
InspectorClientImpl* InspectorClientFromContext(
v8::Local<v8::Context> context) {
InspectorClientImpl* inspector_client = static_cast<InspectorClientImpl*>(
context->GetAlignedPointerFromEmbedderData(kInspectorClientIndex));
CHECK(inspector_client);
return inspector_client;
}
} // namespace
class ConnectTask : public TaskRunner::Task {
......@@ -96,12 +104,14 @@ void InspectorClientImpl::quitMessageLoopOnPause() {
task_runner_->QuitMessageLoop();
}
v8_inspector::V8Inspector* InspectorClientImpl::InspectorFromContext(
v8::Local<v8::Context> context) {
return InspectorClientFromContext(context)->inspector_.get();
}
v8_inspector::V8InspectorSession* InspectorClientImpl::SessionFromContext(
v8::Local<v8::Context> context) {
InspectorClientImpl* inspector_client = static_cast<InspectorClientImpl*>(
context->GetAlignedPointerFromEmbedderData(kInspectorClientIndex));
CHECK(inspector_client);
return inspector_client->session_.get();
return InspectorClientFromContext(context)->session_.get();
}
class SendMessageToBackendTask : public TaskRunner::Task {
......
......@@ -25,6 +25,9 @@ class InspectorClientImpl : public v8_inspector::V8InspectorClient {
v8::base::Semaphore* ready_semaphore);
virtual ~InspectorClientImpl();
static v8_inspector::V8Inspector* InspectorFromContext(
v8::Local<v8::Context> context);
private:
// V8InspectorClient implementation.
v8::Local<v8::Context> ensureDefaultContextInGroup(
......
// 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 "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"
#include "test/inspector/inspector-impl.h"
#include "test/inspector/task-runner.h"
namespace {
void Exit() {
fflush(stdout);
fflush(stderr);
_exit(0);
}
class UtilsExtension : public v8::Extension {
public:
UtilsExtension()
: v8::Extension("v8_inspector/utils",
"native function print(); native function quit();") {}
virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
v8::Isolate* isolate, v8::Local<v8::String> name) {
v8::Local<v8::Context> context = isolate->GetCurrentContext();
if (name->Equals(context, v8::String::NewFromUtf8(
isolate, "print", v8::NewStringType::kNormal)
.ToLocalChecked())
.FromJust()) {
return v8::FunctionTemplate::New(isolate, UtilsExtension::Print);
} else if (name->Equals(context,
v8::String::NewFromUtf8(isolate, "quit",
v8::NewStringType::kNormal)
.ToLocalChecked())
.FromJust()) {
return v8::FunctionTemplate::New(isolate, UtilsExtension::Quit);
}
return v8::Local<v8::FunctionTemplate>();
}
private:
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(); }
};
class SetTimeoutTask : public TaskRunner::Task {
public:
SetTimeoutTask(v8::Isolate* isolate, v8::Local<v8::Function> function)
: function_(isolate, function) {}
virtual ~SetTimeoutTask() {}
bool is_inspector_task() final { return false; }
void Run(v8::Isolate* isolate,
const v8::Global<v8::Context>& global_context) override {
v8::MicrotasksScope microtasks_scope(isolate,
v8::MicrotasksScope::kRunMicrotasks);
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = global_context.Get(isolate);
v8::Context::Scope context_scope(context);
v8::Local<v8::Function> function = function_.Get(isolate);
v8::MaybeLocal<v8::Value> result;
v8_inspector::V8Inspector* inspector =
InspectorClientImpl::InspectorFromContext(context);
if (inspector) inspector->willExecuteScript(context, function->ScriptId());
result = function->Call(context, context->Global(), 0, nullptr);
if (inspector) inspector->didExecuteScript(context);
}
private:
v8::Global<v8::Function> function_;
};
class SetTimeoutExtension : public v8::Extension {
public:
SetTimeoutExtension()
: v8::Extension("v8_inspector/setTimeout",
"native function setTimeout();") {}
virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
v8::Isolate* isolate, v8::Local<v8::String> name) {
return v8::FunctionTemplate::New(isolate, SetTimeoutExtension::SetTimeout);
}
private:
static void SetTimeout(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
if (args.Length() != 2 || !args[1]->IsNumber() || !args[0]->IsFunction() ||
args[1].As<v8::Number>()->Value() != 0.0) {
fprintf(stderr,
"Internal error: only setTimeout(function, 0) is supported.");
Exit();
}
v8::Local<v8::Context> context = args.GetIsolate()->GetCurrentContext();
TaskRunner::FromContext(context)->Append(new SetTimeoutTask(
args.GetIsolate(), v8::Local<v8::Function>::Cast(args[0])));
}
};
v8_inspector::String16 ToString16(const v8_inspector::StringView& string) {
if (string.is8Bit())
return v8_inspector::String16(
reinterpret_cast<const char*>(string.characters8()), string.length());
return v8_inspector::String16(
reinterpret_cast<const uint16_t*>(string.characters16()),
string.length());
}
class FrontendChannelImpl : public InspectorClientImpl::FrontendChannel {
public:
explicit FrontendChannelImpl(TaskRunner* frontend_task_runner)
: frontend_task_runner_(frontend_task_runner) {}
virtual ~FrontendChannelImpl() {}
void SendMessageToFrontend(const v8_inspector::StringView& message) final {
v8_inspector::String16Builder script;
script.append("InspectorTest.dispatchMessage(");
script.append(ToString16(message));
script.append(")");
frontend_task_runner_->Append(new ExecuteStringTask(script.toString()));
}
private:
TaskRunner* frontend_task_runner_;
};
} // namespace
int main(int argc, char* argv[]) {
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
v8::internal::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
v8::V8::InitializeExternalStartupData(argv[0]);
v8::V8::Initialize();
SetTimeoutExtension set_timeout_extension;
v8::RegisterExtension(&set_timeout_extension);
UtilsExtension utils_extension;
v8::RegisterExtension(&utils_extension);
SendMessageToBackendExtension send_message_to_backend_extension;
v8::RegisterExtension(&send_message_to_backend_extension);
v8::base::Semaphore ready_semaphore(0);
const char* backend_extensions[] = {"v8_inspector/setTimeout"};
v8::ExtensionConfiguration backend_configuration(
arraysize(backend_extensions), backend_extensions);
TaskRunner backend_runner(&backend_configuration, &ready_semaphore);
ready_semaphore.Wait();
SendMessageToBackendExtension::set_backend_task_runner(&backend_runner);
const char* frontend_extensions[] = {"v8_inspector/utils",
"v8_inspector/frontend"};
v8::ExtensionConfiguration frontend_configuration(
arraysize(frontend_extensions), frontend_extensions);
TaskRunner frontend_runner(&frontend_configuration, &ready_semaphore);
ready_semaphore.Wait();
FrontendChannelImpl frontend_channel(&frontend_runner);
InspectorClientImpl inspector_client(&backend_runner, &frontend_channel,
&ready_semaphore);
ready_semaphore.Wait();
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') break;
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();
}
v8_inspector::String16 source =
v8_inspector::String16::fromUTF8(chars.start(), chars.length());
frontend_runner.Append(new ExecuteStringTask(source));
}
frontend_runner.Join();
return 0;
}
......@@ -8,6 +8,7 @@
'inspector_protocol_sources': [
'inspector-impl.cc',
'inspector-impl.h',
'inspector-test.cc',
'task-runner.cc',
'task-runner.h',
],
......
......@@ -132,6 +132,3 @@ void ExecuteStringTask::Run(v8::Isolate* isolate,
v8::MaybeLocal<v8::Value> result;
result = script->Run(local_context);
}
// main to make compiler happy before other patch will be landed.
int main(int argc, char* argv[]) { return 0; }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment