d8-console.cc 5.21 KB
Newer Older
1 2 3 4
// 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.

5 6
#include "src/d8/d8-console.h"
#include "src/d8/d8.h"
7
#include "src/execution/isolate.h"
8 9 10 11

namespace v8 {

namespace {
12
void WriteToFile(const char* prefix, FILE* file, Isolate* isolate,
13
                 const debug::ConsoleCallArguments& args) {
14
  if (prefix) fprintf(file, "%s: ", prefix);
15 16
  for (int i = 0; i < args.Length(); i++) {
    HandleScope handle_scope(isolate);
17
    if (i > 0) fprintf(file, " ");
18 19 20 21

    Local<Value> arg = args[i];
    Local<String> str_obj;

22
    if (arg->IsSymbol()) arg = Local<Symbol>::Cast(arg)->Description();
23
    if (!arg->ToString(isolate->GetCurrentContext()).ToLocal(&str_obj)) return;
24

25
    v8::String::Utf8Value str(isolate, str_obj);
26 27 28
    int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), file));
    if (n != str.length()) {
      printf("Error in fwrite\n");
29
      base::OS::ExitProcess(1);
30 31 32 33 34 35 36 37 38 39
    }
  }
  fprintf(file, "\n");
}
}  // anonymous namespace

D8Console::D8Console(Isolate* isolate) : isolate_(isolate) {
  default_timer_ = base::TimeTicks::HighResolutionNow();
}

40 41
void D8Console::Assert(const debug::ConsoleCallArguments& args,
                       const v8::debug::ConsoleContext&) {
42 43 44
  // If no arguments given, the "first" argument is undefined which is
  // false-ish.
  if (args.Length() > 0 && args[0]->BooleanValue(isolate_)) return;
45
  WriteToFile("console.assert", stdout, isolate_, args);
46
  isolate_->ThrowException(v8::Exception::Error(
47
      v8::String::NewFromUtf8Literal(isolate_, "console.assert failed")));
48 49
}

50 51
void D8Console::Log(const debug::ConsoleCallArguments& args,
                    const v8::debug::ConsoleContext&) {
52
  WriteToFile(nullptr, stdout, isolate_, args);
53 54
}

55 56
void D8Console::Error(const debug::ConsoleCallArguments& args,
                      const v8::debug::ConsoleContext&) {
57
  WriteToFile("console.error", stderr, isolate_, args);
58 59
}

60 61
void D8Console::Warn(const debug::ConsoleCallArguments& args,
                     const v8::debug::ConsoleContext&) {
62
  WriteToFile("console.warn", stdout, isolate_, args);
63 64
}

65 66
void D8Console::Info(const debug::ConsoleCallArguments& args,
                     const v8::debug::ConsoleContext&) {
67
  WriteToFile("console.info", stdout, isolate_, args);
68 69
}

70 71
void D8Console::Debug(const debug::ConsoleCallArguments& args,
                      const v8::debug::ConsoleContext&) {
72
  WriteToFile("console.debug", stdout, isolate_, args);
73 74
}

75 76
void D8Console::Time(const debug::ConsoleCallArguments& args,
                     const v8::debug::ConsoleContext&) {
77
  if (internal::FLAG_correctness_fuzzer_suppressions) return;
78 79 80 81 82 83
  if (args.Length() == 0) {
    default_timer_ = base::TimeTicks::HighResolutionNow();
  } else {
    Local<Value> arg = args[0];
    Local<String> label;
    v8::TryCatch try_catch(isolate_);
84
    if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) return;
85
    v8::String::Utf8Value utf8(isolate_, label);
86 87 88 89 90 91 92 93 94 95 96
    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()));
    }
  }
}

97 98
void D8Console::TimeEnd(const debug::ConsoleCallArguments& args,
                        const v8::debug::ConsoleContext&) {
99
  if (internal::FLAG_correctness_fuzzer_suppressions) return;
100 101 102
  base::TimeDelta delta;
  if (args.Length() == 0) {
    delta = base::TimeTicks::HighResolutionNow() - default_timer_;
103
    printf("console.timeEnd: default, %f\n", delta.InMillisecondsF());
104
  } else {
105
    base::TimeTicks now = base::TimeTicks::HighResolutionNow();
106 107 108
    Local<Value> arg = args[0];
    Local<String> label;
    v8::TryCatch try_catch(isolate_);
109
    if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) return;
110
    v8::String::Utf8Value utf8(isolate_, label);
111 112 113 114
    std::string string(*utf8);
    auto find = timers_.find(string);
    if (find != timers_.end()) {
      delta = now - find->second;
115
      timers_.erase(find);
116
    }
117
    printf("console.timeEnd: %s, %f\n", *utf8, delta.InMillisecondsF());
118
  }
119 120 121 122
}

void D8Console::TimeStamp(const debug::ConsoleCallArguments& args,
                          const v8::debug::ConsoleContext&) {
123
  if (internal::FLAG_correctness_fuzzer_suppressions) return;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  base::TimeDelta delta = base::TimeTicks::HighResolutionNow() - default_timer_;
  if (args.Length() == 0) {
    printf("console.timeStamp: default, %f\n", delta.InMillisecondsF());
  } else {
    Local<Value> arg = args[0];
    Local<String> label;
    v8::TryCatch try_catch(isolate_);
    if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) return;
    v8::String::Utf8Value utf8(isolate_, label);
    std::string string(*utf8);
    printf("console.timeStamp: %s, %f\n", *utf8, delta.InMillisecondsF());
  }
}

void D8Console::Trace(const debug::ConsoleCallArguments& args,
                      const v8::debug::ConsoleContext&) {
140
  if (internal::FLAG_correctness_fuzzer_suppressions) return;
141 142
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate_);
  i_isolate->PrintStack(stderr, i::Isolate::kPrintStackConcise);
143 144 145
}

}  // namespace v8