LCOV - code coverage report
Current view: top level - src/builtins - builtins-console.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 58 58 100.0 %
Date: 2019-04-19 Functions: 50 73 68.5 %

          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/api-inl.h"
       6             : #include "src/builtins/builtins-utils-inl.h"
       7             : #include "src/builtins/builtins.h"
       8             : #include "src/counters.h"
       9             : #include "src/debug/interface-types.h"
      10             : #include "src/log.h"
      11             : #include "src/objects-inl.h"
      12             : 
      13             : namespace v8 {
      14             : namespace internal {
      15             : 
      16             : // -----------------------------------------------------------------------------
      17             : // Console
      18             : 
      19             : #define CONSOLE_METHOD_LIST(V)      \
      20             :   V(Debug, debug)                   \
      21             :   V(Error, error)                   \
      22             :   V(Info, info)                     \
      23             :   V(Log, log)                       \
      24             :   V(Warn, warn)                     \
      25             :   V(Dir, dir)                       \
      26             :   V(DirXml, dirXml)                 \
      27             :   V(Table, table)                   \
      28             :   V(Trace, trace)                   \
      29             :   V(Group, group)                   \
      30             :   V(GroupCollapsed, groupCollapsed) \
      31             :   V(GroupEnd, groupEnd)             \
      32             :   V(Clear, clear)                   \
      33             :   V(Count, count)                   \
      34             :   V(CountReset, countReset)         \
      35             :   V(Assert, assert)                 \
      36             :   V(Profile, profile)               \
      37             :   V(ProfileEnd, profileEnd)         \
      38             :   V(TimeLog, timeLog)
      39             : 
      40             : namespace {
      41       12915 : void ConsoleCall(
      42             :     Isolate* isolate, internal::BuiltinArguments& args,
      43             :     void (debug::ConsoleDelegate::*func)(const v8::debug::ConsoleCallArguments&,
      44             :                                          const v8::debug::ConsoleContext&)) {
      45       12915 :   CHECK(!isolate->has_pending_exception());
      46       12915 :   CHECK(!isolate->has_scheduled_exception());
      47       12960 :   if (!isolate->console_delegate()) return;
      48             :   HandleScope scope(isolate);
      49       12870 :   debug::ConsoleCallArguments wrapper(args);
      50             :   Handle<Object> context_id_obj = JSObject::GetDataProperty(
      51       12870 :       args.target(), isolate->factory()->console_context_id_symbol());
      52             :   int context_id =
      53       12870 :       context_id_obj->IsSmi() ? Handle<Smi>::cast(context_id_obj)->value() : 0;
      54             :   Handle<Object> context_name_obj = JSObject::GetDataProperty(
      55       12870 :       args.target(), isolate->factory()->console_context_name_symbol());
      56             :   Handle<String> context_name = context_name_obj->IsString()
      57             :                                     ? Handle<String>::cast(context_name_obj)
      58       12870 :                                     : isolate->factory()->anonymous_string();
      59       25740 :   (isolate->console_delegate()->*func)(
      60             :       wrapper,
      61       25740 :       v8::debug::ConsoleContext(context_id, Utils::ToLocal(context_name)));
      62             : }
      63             : 
      64         341 : void LogTimerEvent(Isolate* isolate, BuiltinArguments args,
      65             :                    Logger::StartEnd se) {
      66         341 :   if (!isolate->logger()->is_logging()) return;
      67             :   HandleScope scope(isolate);
      68             :   std::unique_ptr<char[]> name;
      69             :   const char* raw_name = "default";
      70          55 :   if (args.length() > 1 && args[1]->IsString()) {
      71             :     // Try converting the first argument to a string.
      72          40 :     name = args.at<String>(1)->ToCString();
      73             :     raw_name = name.get();
      74             :   }
      75          35 :   LOG(isolate, TimerEvent(se, raw_name));
      76             : }
      77             : }  // namespace
      78             : 
      79             : #define CONSOLE_BUILTIN_IMPLEMENTATION(call, name)             \
      80             :   BUILTIN(Console##call) {                                     \
      81             :     ConsoleCall(isolate, args, &debug::ConsoleDelegate::call); \
      82             :     RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);            \
      83             :     return ReadOnlyRoots(isolate).undefined_value();           \
      84             :   }
      85       87963 : CONSOLE_METHOD_LIST(CONSOLE_BUILTIN_IMPLEMENTATION)
      86             : #undef CONSOLE_BUILTIN_IMPLEMENTATION
      87             : 
      88         590 : BUILTIN(ConsoleTime) {
      89         118 :   LogTimerEvent(isolate, args, Logger::START);
      90         118 :   ConsoleCall(isolate, args, &debug::ConsoleDelegate::Time);
      91         118 :   RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
      92         118 :   return ReadOnlyRoots(isolate).undefined_value();
      93             : }
      94             : 
      95         590 : BUILTIN(ConsoleTimeEnd) {
      96         118 :   LogTimerEvent(isolate, args, Logger::END);
      97         118 :   ConsoleCall(isolate, args, &debug::ConsoleDelegate::TimeEnd);
      98         118 :   RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
      99         118 :   return ReadOnlyRoots(isolate).undefined_value();
     100             : }
     101             : 
     102         525 : BUILTIN(ConsoleTimeStamp) {
     103         105 :   LogTimerEvent(isolate, args, Logger::STAMP);
     104         105 :   ConsoleCall(isolate, args, &debug::ConsoleDelegate::TimeStamp);
     105         105 :   RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate);
     106         105 :   return ReadOnlyRoots(isolate).undefined_value();
     107             : }
     108             : 
     109             : namespace {
     110       64130 : void InstallContextFunction(Isolate* isolate, Handle<JSObject> target,
     111             :                             const char* name, Builtins::Name builtin_id,
     112             :                             int context_id, Handle<Object> context_name) {
     113             :   Factory* const factory = isolate->factory();
     114             : 
     115             :   Handle<String> name_string =
     116      192390 :       Name::ToFunctionName(isolate, factory->InternalizeUtf8String(name))
     117             :           .ToHandleChecked();
     118             :   NewFunctionArgs args = NewFunctionArgs::ForBuiltinWithoutPrototype(
     119       64130 :       name_string, builtin_id, i::LanguageMode::kSloppy);
     120       64130 :   Handle<JSFunction> fun = factory->NewFunction(args);
     121             : 
     122       64130 :   fun->shared()->set_native(true);
     123             :   fun->shared()->DontAdaptArguments();
     124             :   fun->shared()->set_length(1);
     125             : 
     126       64130 :   JSObject::AddProperty(isolate, fun, factory->console_context_id_symbol(),
     127       64130 :                         handle(Smi::FromInt(context_id), isolate), NONE);
     128       64130 :   if (context_name->IsString()) {
     129         660 :     JSObject::AddProperty(isolate, fun, factory->console_context_name_symbol(),
     130         660 :                           context_name, NONE);
     131             :   }
     132       64130 :   JSObject::AddProperty(isolate, target, name_string, fun, NONE);
     133       64130 : }
     134             : }  // namespace
     135             : 
     136       14575 : BUILTIN(ConsoleContext) {
     137             :   HandleScope scope(isolate);
     138             : 
     139             :   Factory* const factory = isolate->factory();
     140        2915 :   Handle<String> name = factory->InternalizeUtf8String("Context");
     141             :   NewFunctionArgs arguments = NewFunctionArgs::ForFunctionWithoutCode(
     142        2915 :       name, isolate->sloppy_function_map(), LanguageMode::kSloppy);
     143        2915 :   Handle<JSFunction> cons = factory->NewFunction(arguments);
     144             : 
     145        2915 :   Handle<JSObject> prototype = factory->NewJSObject(isolate->object_function());
     146        2915 :   JSFunction::SetPrototype(cons, prototype);
     147             : 
     148        2915 :   Handle<JSObject> context = factory->NewJSObject(cons, AllocationType::kOld);
     149             :   DCHECK(context->IsJSObject());
     150        2915 :   int id = isolate->last_console_context_id() + 1;
     151             :   isolate->set_last_console_context_id(id);
     152             : 
     153             : #define CONSOLE_BUILTIN_SETUP(call, name)                                   \
     154             :   InstallContextFunction(isolate, context, #name, Builtins::kConsole##call, \
     155             :                          id, args.at(1));
     156        2915 :   CONSOLE_METHOD_LIST(CONSOLE_BUILTIN_SETUP)
     157             : #undef CONSOLE_BUILTIN_SETUP
     158             :   InstallContextFunction(isolate, context, "time", Builtins::kConsoleTime, id,
     159        2915 :                          args.at(1));
     160             :   InstallContextFunction(isolate, context, "timeEnd", Builtins::kConsoleTimeEnd,
     161        2915 :                          id, args.at(1));
     162             :   InstallContextFunction(isolate, context, "timeStamp",
     163        2915 :                          Builtins::kConsoleTimeStamp, id, args.at(1));
     164             : 
     165        5830 :   return *context;
     166             : }
     167             : 
     168             : #undef CONSOLE_METHOD_LIST
     169             : 
     170             : }  // namespace internal
     171      122036 : }  // namespace v8

Generated by: LCOV version 1.10