11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#include "src/d8/d8-console.h" 61cb0ef41Sopenharmony_ci#include "src/d8/d8.h" 71cb0ef41Sopenharmony_ci#include "src/execution/isolate.h" 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_cinamespace v8 { 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cinamespace { 121cb0ef41Sopenharmony_civoid WriteToFile(const char* prefix, FILE* file, Isolate* isolate, 131cb0ef41Sopenharmony_ci const debug::ConsoleCallArguments& args) { 141cb0ef41Sopenharmony_ci if (prefix) fprintf(file, "%s: ", prefix); 151cb0ef41Sopenharmony_ci for (int i = 0; i < args.Length(); i++) { 161cb0ef41Sopenharmony_ci HandleScope handle_scope(isolate); 171cb0ef41Sopenharmony_ci if (i > 0) fprintf(file, " "); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci Local<Value> arg = args[i]; 201cb0ef41Sopenharmony_ci Local<String> str_obj; 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci if (arg->IsSymbol()) arg = Local<Symbol>::Cast(arg)->Description(isolate); 231cb0ef41Sopenharmony_ci if (!arg->ToString(isolate->GetCurrentContext()).ToLocal(&str_obj)) return; 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci v8::String::Utf8Value str(isolate, str_obj); 261cb0ef41Sopenharmony_ci int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), file)); 271cb0ef41Sopenharmony_ci if (n != str.length()) { 281cb0ef41Sopenharmony_ci printf("Error in fwrite\n"); 291cb0ef41Sopenharmony_ci base::OS::ExitProcess(1); 301cb0ef41Sopenharmony_ci } 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci fprintf(file, "\n"); 331cb0ef41Sopenharmony_ci} 341cb0ef41Sopenharmony_ci} // anonymous namespace 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciD8Console::D8Console(Isolate* isolate) : isolate_(isolate) { 371cb0ef41Sopenharmony_ci default_timer_ = base::TimeTicks::Now(); 381cb0ef41Sopenharmony_ci} 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_civoid D8Console::Assert(const debug::ConsoleCallArguments& args, 411cb0ef41Sopenharmony_ci const v8::debug::ConsoleContext&) { 421cb0ef41Sopenharmony_ci // If no arguments given, the "first" argument is undefined which is 431cb0ef41Sopenharmony_ci // false-ish. 441cb0ef41Sopenharmony_ci if (args.Length() > 0 && args[0]->BooleanValue(isolate_)) return; 451cb0ef41Sopenharmony_ci WriteToFile("console.assert", stdout, isolate_, args); 461cb0ef41Sopenharmony_ci isolate_->ThrowError("console.assert failed"); 471cb0ef41Sopenharmony_ci} 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_civoid D8Console::Log(const debug::ConsoleCallArguments& args, 501cb0ef41Sopenharmony_ci const v8::debug::ConsoleContext&) { 511cb0ef41Sopenharmony_ci WriteToFile(nullptr, stdout, isolate_, args); 521cb0ef41Sopenharmony_ci} 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_civoid D8Console::Error(const debug::ConsoleCallArguments& args, 551cb0ef41Sopenharmony_ci const v8::debug::ConsoleContext&) { 561cb0ef41Sopenharmony_ci WriteToFile("console.error", stderr, isolate_, args); 571cb0ef41Sopenharmony_ci} 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_civoid D8Console::Warn(const debug::ConsoleCallArguments& args, 601cb0ef41Sopenharmony_ci const v8::debug::ConsoleContext&) { 611cb0ef41Sopenharmony_ci WriteToFile("console.warn", stdout, isolate_, args); 621cb0ef41Sopenharmony_ci} 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_civoid D8Console::Info(const debug::ConsoleCallArguments& args, 651cb0ef41Sopenharmony_ci const v8::debug::ConsoleContext&) { 661cb0ef41Sopenharmony_ci WriteToFile("console.info", stdout, isolate_, args); 671cb0ef41Sopenharmony_ci} 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_civoid D8Console::Debug(const debug::ConsoleCallArguments& args, 701cb0ef41Sopenharmony_ci const v8::debug::ConsoleContext&) { 711cb0ef41Sopenharmony_ci WriteToFile("console.debug", stdout, isolate_, args); 721cb0ef41Sopenharmony_ci} 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_civoid D8Console::Time(const debug::ConsoleCallArguments& args, 751cb0ef41Sopenharmony_ci const v8::debug::ConsoleContext&) { 761cb0ef41Sopenharmony_ci if (internal::FLAG_correctness_fuzzer_suppressions) return; 771cb0ef41Sopenharmony_ci if (args.Length() == 0) { 781cb0ef41Sopenharmony_ci default_timer_ = base::TimeTicks::Now(); 791cb0ef41Sopenharmony_ci } else { 801cb0ef41Sopenharmony_ci Local<Value> arg = args[0]; 811cb0ef41Sopenharmony_ci Local<String> label; 821cb0ef41Sopenharmony_ci v8::TryCatch try_catch(isolate_); 831cb0ef41Sopenharmony_ci if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) return; 841cb0ef41Sopenharmony_ci v8::String::Utf8Value utf8(isolate_, label); 851cb0ef41Sopenharmony_ci std::string string(*utf8); 861cb0ef41Sopenharmony_ci auto find = timers_.find(string); 871cb0ef41Sopenharmony_ci if (find != timers_.end()) { 881cb0ef41Sopenharmony_ci find->second = base::TimeTicks::Now(); 891cb0ef41Sopenharmony_ci } else { 901cb0ef41Sopenharmony_ci timers_.insert(std::pair<std::string, base::TimeTicks>( 911cb0ef41Sopenharmony_ci string, base::TimeTicks::Now())); 921cb0ef41Sopenharmony_ci } 931cb0ef41Sopenharmony_ci } 941cb0ef41Sopenharmony_ci} 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_civoid D8Console::TimeEnd(const debug::ConsoleCallArguments& args, 971cb0ef41Sopenharmony_ci const v8::debug::ConsoleContext&) { 981cb0ef41Sopenharmony_ci if (internal::FLAG_correctness_fuzzer_suppressions) return; 991cb0ef41Sopenharmony_ci base::TimeDelta delta; 1001cb0ef41Sopenharmony_ci if (args.Length() == 0) { 1011cb0ef41Sopenharmony_ci delta = base::TimeTicks::Now() - default_timer_; 1021cb0ef41Sopenharmony_ci printf("console.timeEnd: default, %f\n", delta.InMillisecondsF()); 1031cb0ef41Sopenharmony_ci } else { 1041cb0ef41Sopenharmony_ci base::TimeTicks now = base::TimeTicks::Now(); 1051cb0ef41Sopenharmony_ci Local<Value> arg = args[0]; 1061cb0ef41Sopenharmony_ci Local<String> label; 1071cb0ef41Sopenharmony_ci v8::TryCatch try_catch(isolate_); 1081cb0ef41Sopenharmony_ci if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) return; 1091cb0ef41Sopenharmony_ci v8::String::Utf8Value utf8(isolate_, label); 1101cb0ef41Sopenharmony_ci std::string string(*utf8); 1111cb0ef41Sopenharmony_ci auto find = timers_.find(string); 1121cb0ef41Sopenharmony_ci if (find != timers_.end()) { 1131cb0ef41Sopenharmony_ci delta = now - find->second; 1141cb0ef41Sopenharmony_ci timers_.erase(find); 1151cb0ef41Sopenharmony_ci } 1161cb0ef41Sopenharmony_ci printf("console.timeEnd: %s, %f\n", *utf8, delta.InMillisecondsF()); 1171cb0ef41Sopenharmony_ci } 1181cb0ef41Sopenharmony_ci} 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_civoid D8Console::TimeStamp(const debug::ConsoleCallArguments& args, 1211cb0ef41Sopenharmony_ci const v8::debug::ConsoleContext&) { 1221cb0ef41Sopenharmony_ci if (internal::FLAG_correctness_fuzzer_suppressions) return; 1231cb0ef41Sopenharmony_ci base::TimeDelta delta = base::TimeTicks::Now() - default_timer_; 1241cb0ef41Sopenharmony_ci if (args.Length() == 0) { 1251cb0ef41Sopenharmony_ci printf("console.timeStamp: default, %f\n", delta.InMillisecondsF()); 1261cb0ef41Sopenharmony_ci } else { 1271cb0ef41Sopenharmony_ci Local<Value> arg = args[0]; 1281cb0ef41Sopenharmony_ci Local<String> label; 1291cb0ef41Sopenharmony_ci v8::TryCatch try_catch(isolate_); 1301cb0ef41Sopenharmony_ci if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) return; 1311cb0ef41Sopenharmony_ci v8::String::Utf8Value utf8(isolate_, label); 1321cb0ef41Sopenharmony_ci std::string string(*utf8); 1331cb0ef41Sopenharmony_ci printf("console.timeStamp: %s, %f\n", *utf8, delta.InMillisecondsF()); 1341cb0ef41Sopenharmony_ci } 1351cb0ef41Sopenharmony_ci} 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_civoid D8Console::Trace(const debug::ConsoleCallArguments& args, 1381cb0ef41Sopenharmony_ci const v8::debug::ConsoleContext&) { 1391cb0ef41Sopenharmony_ci if (internal::FLAG_correctness_fuzzer_suppressions) return; 1401cb0ef41Sopenharmony_ci i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate_); 1411cb0ef41Sopenharmony_ci i_isolate->PrintStack(stderr, i::Isolate::kPrintStackConcise); 1421cb0ef41Sopenharmony_ci} 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci} // namespace v8 145