Commit 461e47a8 authored by yangguo's avatar yangguo Committed by Commit bot

[d8] implement console for d8.

for now, it's just the methods
- log
- warn
- debug
- info
- error
- time
- timeEnd

R=jarin@chromium.org

Review-Url: https://codereview.chromium.org/2840543002
Cr-Commit-Position: refs/heads/master@{#44797}
parent 6a833f23
......@@ -2721,6 +2721,8 @@ if (is_component_build) {
v8_executable("d8") {
sources = [
"$target_gen_dir/d8-js.cc",
"src/d8-console.cc",
"src/d8-console.h",
"src/d8.cc",
"src/d8.h",
]
......
// Copyright 2017 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 "src/d8-console.h"
#include "src/d8.h"
namespace v8 {
namespace {
void WriteToFile(FILE* file, Isolate* isolate,
const debug::ConsoleCallArguments& args) {
for (int i = 0; i < args.Length(); i++) {
HandleScope handle_scope(isolate);
if (i != 0) fprintf(file, " ");
// Explicitly catch potential exceptions in toString().
v8::TryCatch try_catch(isolate);
Local<Value> arg = args[i];
Local<String> str_obj;
if (arg->IsSymbol()) arg = Local<Symbol>::Cast(arg)->Name();
if (!arg->ToString(isolate->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(), file));
if (n != str.length()) {
printf("Error in fwrite\n");
Shell::Exit(1);
}
}
fprintf(file, "\n");
}
} // anonymous namespace
D8Console::D8Console(Isolate* isolate) : isolate_(isolate) {
default_timer_ = base::TimeTicks::HighResolutionNow();
}
void D8Console::Log(const debug::ConsoleCallArguments& args) {
WriteToFile(stdout, isolate_, args);
}
void D8Console::Error(const debug::ConsoleCallArguments& args) {
WriteToFile(stderr, isolate_, args);
}
void D8Console::Warn(const debug::ConsoleCallArguments& args) {
WriteToFile(stdout, isolate_, args);
}
void D8Console::Info(const debug::ConsoleCallArguments& args) {
WriteToFile(stdout, isolate_, args);
}
void D8Console::Debug(const debug::ConsoleCallArguments& args) {
WriteToFile(stdout, isolate_, args);
}
void D8Console::Time(const debug::ConsoleCallArguments& args) {
if (args.Length() == 0) {
default_timer_ = base::TimeTicks::HighResolutionNow();
} else {
Local<Value> arg = args[0];
Local<String> label;
v8::TryCatch try_catch(isolate_);
if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) {
try_catch.ReThrow();
return;
}
v8::String::Utf8Value utf8(label);
std::string string(*utf8);
auto find = timers_.find(string);
if (find != timers_.end()) {
find->second = base::TimeTicks::HighResolutionNow();
} else {
timers_.insert(std::pair<std::string, base::TimeTicks>(
string, base::TimeTicks::HighResolutionNow()));
}
}
}
void D8Console::TimeEnd(const debug::ConsoleCallArguments& args) {
base::TimeDelta delta;
base::TimeTicks now = base::TimeTicks::HighResolutionNow();
if (args.Length() == 0) {
delta = base::TimeTicks::HighResolutionNow() - default_timer_;
printf("default: ");
} else {
Local<Value> arg = args[0];
Local<String> label;
v8::TryCatch try_catch(isolate_);
if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) {
try_catch.ReThrow();
return;
}
v8::String::Utf8Value utf8(label);
std::string string(*utf8);
auto find = timers_.find(string);
if (find != timers_.end()) {
delta = now - find->second;
}
printf("%s: ", *utf8);
}
printf("%f\n", delta.InMillisecondsF());
}
} // namespace v8
// Copyright 2017 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.
#ifndef V8_D8_CONSOLE_H_
#define V8_D8_CONSOLE_H_
#include "src/base/platform/time.h"
#include "src/debug/debug-interface.h"
#include "src/debug/interface-types.h"
namespace v8 {
class D8Console : public debug::ConsoleDelegate {
public:
explicit D8Console(Isolate* isolate);
private:
void Log(const debug::ConsoleCallArguments& args) override;
void Error(const debug::ConsoleCallArguments& args) override;
void Warn(const debug::ConsoleCallArguments& args) override;
void Info(const debug::ConsoleCallArguments& args) override;
void Debug(const debug::ConsoleCallArguments& args) override;
void Time(const debug::ConsoleCallArguments& args) override;
void TimeEnd(const debug::ConsoleCallArguments& args) override;
Isolate* isolate_;
std::map<std::string, base::TimeTicks> timers_;
base::TimeTicks default_timer_;
};
} // namespace v8
#endif // V8_D8_CONSOLE_H_
......@@ -17,6 +17,7 @@
#include "src/third_party/vtune/v8-vtune.h"
#endif
#include "src/d8-console.h"
#include "src/d8.h"
#include "src/ostreams.h"
......@@ -2261,6 +2262,8 @@ void SourceGroup::ExecuteInThread() {
create_params.host_import_module_dynamically_callback_ =
Shell::HostImportModuleDynamically;
Isolate* isolate = Isolate::New(create_params);
D8Console console(isolate);
debug::SetConsoleDelegate(isolate, &console);
for (int i = 0; i < Shell::options.stress_runs; ++i) {
next_semaphore_.Wait();
{
......@@ -2401,6 +2404,8 @@ void Worker::ExecuteInThread() {
create_params.host_import_module_dynamically_callback_ =
Shell::HostImportModuleDynamically;
Isolate* isolate = Isolate::New(create_params);
D8Console console(isolate);
debug::SetConsoleDelegate(isolate, &console);
{
Isolate::Scope iscope(isolate);
{
......@@ -3017,10 +3022,12 @@ int Shell::Main(int argc, char* argv[]) {
}
Isolate* isolate = Isolate::New(create_params);
D8Console console(isolate);
{
Isolate::Scope scope(isolate);
Initialize(isolate);
PerIsolateData data(isolate);
debug::SetConsoleDelegate(isolate, &console);
if (options.trace_enabled) {
platform::tracing::TraceConfig* trace_config;
......
......@@ -50,6 +50,8 @@
'sources': [
'd8.h',
'd8.cc',
'd8-console.h',
'd8-console.cc',
'<(SHARED_INTERMEDIATE_DIR)/d8-js.cc',
],
'conditions': [
......
......@@ -209,7 +209,8 @@ enum Builtin {
Local<Function> GetBuiltin(Isolate* isolate, Builtin builtin);
void SetConsoleDelegate(Isolate* isolate, ConsoleDelegate* delegate);
V8_EXPORT_PRIVATE void SetConsoleDelegate(Isolate* isolate,
ConsoleDelegate* delegate);
int GetStackFrameId(v8::Local<v8::StackFrame> frame);
......
// Copyright 2017 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.
// Flags: --no-stress-opt
console.time();
console.timeEnd();
console.time("abcd");
console.timeEnd({ toString: () => "ab" + "cd" });
console.time("a");
console.timeEnd("b");
console.time("a", "b");
console.timeEnd("a", "b");
console.log("log", "more");
console.warn("warn", { toString: () => 2 });
console.error("error");
console.debug("debug");
console.info("info");
default: {NUMBER}
abcd: {NUMBER}
b: 0.000000
a: {NUMBER}
log more
warn 2
debug
info
......@@ -107,6 +107,7 @@ class MessageTestSuite(testsuite.TestSuite):
expected_lines, actual_lines, fillvalue=''):
pattern = re.escape(expected.rstrip() % env)
pattern = pattern.replace("\\*", ".*")
pattern = pattern.replace("\\{NUMBER\\}", "\d(?:\.\d*)?")
pattern = "^%s$" % pattern
if not re.match(pattern, actual):
return True
......
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