OLD | NEW |
(Empty) | |
| 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 void WriteToFile(FILE* file, Isolate* isolate, |
| 12 const debug::ConsoleCallArguments& args) { |
| 13 for (int i = 0; i < args.Length(); i++) { |
| 14 HandleScope handle_scope(isolate); |
| 15 if (i != 0) fprintf(file, " "); |
| 16 |
| 17 // Explicitly catch potential exceptions in toString(). |
| 18 v8::TryCatch try_catch(isolate); |
| 19 Local<Value> arg = args[i]; |
| 20 Local<String> str_obj; |
| 21 |
| 22 if (arg->IsSymbol()) arg = Local<Symbol>::Cast(arg)->Name(); |
| 23 if (!arg->ToString(isolate->GetCurrentContext()).ToLocal(&str_obj)) { |
| 24 try_catch.ReThrow(); |
| 25 return; |
| 26 } |
| 27 |
| 28 v8::String::Utf8Value str(str_obj); |
| 29 int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), file)); |
| 30 if (n != str.length()) { |
| 31 printf("Error in fwrite\n"); |
| 32 Shell::Exit(1); |
| 33 } |
| 34 } |
| 35 fprintf(file, "\n"); |
| 36 } |
| 37 } // anonymous namespace |
| 38 |
| 39 D8Console::D8Console(Isolate* isolate) : isolate_(isolate) { |
| 40 default_timer_ = base::TimeTicks::HighResolutionNow(); |
| 41 } |
| 42 |
| 43 void D8Console::Log(const debug::ConsoleCallArguments& args) { |
| 44 WriteToFile(stdout, isolate_, args); |
| 45 } |
| 46 |
| 47 void D8Console::Error(const debug::ConsoleCallArguments& args) { |
| 48 WriteToFile(stderr, isolate_, args); |
| 49 } |
| 50 |
| 51 void D8Console::Warn(const debug::ConsoleCallArguments& args) { |
| 52 WriteToFile(stdout, isolate_, args); |
| 53 } |
| 54 |
| 55 void D8Console::Info(const debug::ConsoleCallArguments& args) { |
| 56 WriteToFile(stdout, isolate_, args); |
| 57 } |
| 58 |
| 59 void D8Console::Debug(const debug::ConsoleCallArguments& args) { |
| 60 WriteToFile(stdout, isolate_, args); |
| 61 } |
| 62 |
| 63 void D8Console::Time(const debug::ConsoleCallArguments& args) { |
| 64 if (args.Length() == 0) { |
| 65 default_timer_ = base::TimeTicks::HighResolutionNow(); |
| 66 } else { |
| 67 Local<Value> arg = args[0]; |
| 68 Local<String> label; |
| 69 v8::TryCatch try_catch(isolate_); |
| 70 if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) { |
| 71 try_catch.ReThrow(); |
| 72 return; |
| 73 } |
| 74 v8::String::Utf8Value utf8(label); |
| 75 std::string string(*utf8); |
| 76 auto find = timers_.find(string); |
| 77 if (find != timers_.end()) { |
| 78 find->second = base::TimeTicks::HighResolutionNow(); |
| 79 } else { |
| 80 timers_.insert(std::pair<std::string, base::TimeTicks>( |
| 81 string, base::TimeTicks::HighResolutionNow())); |
| 82 } |
| 83 } |
| 84 } |
| 85 |
| 86 void D8Console::TimeEnd(const debug::ConsoleCallArguments& args) { |
| 87 base::TimeDelta delta; |
| 88 base::TimeTicks now = base::TimeTicks::HighResolutionNow(); |
| 89 if (args.Length() == 0) { |
| 90 delta = base::TimeTicks::HighResolutionNow() - default_timer_; |
| 91 printf("default: "); |
| 92 } else { |
| 93 Local<Value> arg = args[0]; |
| 94 Local<String> label; |
| 95 v8::TryCatch try_catch(isolate_); |
| 96 if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) { |
| 97 try_catch.ReThrow(); |
| 98 return; |
| 99 } |
| 100 v8::String::Utf8Value utf8(label); |
| 101 std::string string(*utf8); |
| 102 auto find = timers_.find(string); |
| 103 if (find != timers_.end()) { |
| 104 delta = now - find->second; |
| 105 } |
| 106 printf("%s: ", *utf8); |
| 107 } |
| 108 printf("%f\n", delta.InMillisecondsF()); |
| 109 } |
| 110 |
| 111 } // namespace v8 |
OLD | NEW |