Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/d8-console.h"
6 : #include "src/d8.h"
7 :
8 : namespace v8 {
9 :
10 : namespace {
11 757 : void WriteToFile(FILE* file, Isolate* isolate,
12 : const debug::ConsoleCallArguments& args) {
13 3012 : for (int i = 0; i < args.Length(); i++) {
14 771 : HandleScope handle_scope(isolate);
15 771 : if (i != 0) fprintf(file, " ");
16 :
17 : // Explicitly catch potential exceptions in toString().
18 1520 : v8::TryCatch try_catch(isolate);
19 : Local<Value> arg = args[i];
20 : Local<String> str_obj;
21 :
22 771 : if (arg->IsSymbol()) arg = Local<Symbol>::Cast(arg)->Name();
23 1542 : if (!arg->ToString(isolate->GetCurrentContext()).ToLocal(&str_obj)) {
24 22 : Shell::ReportException(isolate, &try_catch);
25 779 : return;
26 : }
27 :
28 1498 : v8::String::Utf8Value str(str_obj);
29 749 : int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), file));
30 749 : if (n != str.length()) {
31 : printf("Error in fwrite\n");
32 0 : Shell::Exit(1);
33 : }
34 749 : }
35 : fprintf(file, "\n");
36 : }
37 : } // anonymous namespace
38 :
39 58290 : D8Console::D8Console(Isolate* isolate) : isolate_(isolate) {
40 29145 : default_timer_ = base::TimeTicks::HighResolutionNow();
41 29145 : }
42 :
43 722 : void D8Console::Log(const debug::ConsoleCallArguments& args) {
44 722 : WriteToFile(stdout, isolate_, args);
45 722 : }
46 :
47 7 : void D8Console::Error(const debug::ConsoleCallArguments& args) {
48 7 : WriteToFile(stderr, isolate_, args);
49 7 : }
50 :
51 7 : void D8Console::Warn(const debug::ConsoleCallArguments& args) {
52 7 : WriteToFile(stdout, isolate_, args);
53 7 : }
54 :
55 14 : void D8Console::Info(const debug::ConsoleCallArguments& args) {
56 14 : WriteToFile(stdout, isolate_, args);
57 14 : }
58 :
59 7 : void D8Console::Debug(const debug::ConsoleCallArguments& args) {
60 7 : WriteToFile(stdout, isolate_, args);
61 7 : }
62 :
63 28 : void D8Console::Time(const debug::ConsoleCallArguments& args) {
64 28 : if (args.Length() == 0) {
65 7 : default_timer_ = base::TimeTicks::HighResolutionNow();
66 : } else {
67 : Local<Value> arg = args[0];
68 : Local<String> label;
69 21 : v8::TryCatch try_catch(isolate_);
70 42 : if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) {
71 0 : Shell::ReportException(isolate_, &try_catch);
72 28 : return;
73 : }
74 42 : v8::String::Utf8Value utf8(label);
75 21 : std::string string(*utf8);
76 : auto find = timers_.find(string);
77 21 : if (find != timers_.end()) {
78 7 : find->second = base::TimeTicks::HighResolutionNow();
79 : } else {
80 : timers_.insert(std::pair<std::string, base::TimeTicks>(
81 28 : string, base::TimeTicks::HighResolutionNow()));
82 21 : }
83 : }
84 : }
85 :
86 28 : void D8Console::TimeEnd(const debug::ConsoleCallArguments& args) {
87 : base::TimeDelta delta;
88 28 : base::TimeTicks now = base::TimeTicks::HighResolutionNow();
89 28 : if (args.Length() == 0) {
90 14 : delta = base::TimeTicks::HighResolutionNow() - default_timer_;
91 : printf("default: ");
92 : } else {
93 : Local<Value> arg = args[0];
94 : Local<String> label;
95 21 : v8::TryCatch try_catch(isolate_);
96 42 : if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) {
97 0 : Shell::ReportException(isolate_, &try_catch);
98 28 : return;
99 : }
100 42 : v8::String::Utf8Value utf8(label);
101 21 : std::string string(*utf8);
102 : auto find = timers_.find(string);
103 21 : if (find != timers_.end()) {
104 : delta = now - find->second;
105 : }
106 42 : printf("%s: ", *utf8);
107 : }
108 28 : printf("%f\n", delta.InMillisecondsF());
109 : }
110 :
111 : } // namespace v8
|