GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_process_events.cc Lines: 39 49 79.6 %
Date: 2020-02-27 22:14:15 Branches: 18 28 64.3 %

Line Branch Exec Source
1
#include <cstdarg>
2
#include <set>
3
4
#include "env-inl.h"
5
#include "node_process.h"
6
#include "util.h"
7
8
namespace node {
9
using v8::Context;
10
using v8::Function;
11
using v8::HandleScope;
12
using v8::Isolate;
13
using v8::Just;
14
using v8::Local;
15
using v8::Maybe;
16
using v8::MaybeLocal;
17
using v8::NewStringType;
18
using v8::Nothing;
19
using v8::Object;
20
using v8::String;
21
using v8::Value;
22
23
7909
MaybeLocal<Value> ProcessEmit(Environment* env,
24
                              const char* event,
25
                              Local<Value> message) {
26
  // Send message to enable debug in cluster workers
27
7909
  Local<Object> process = env->process_object();
28
7911
  Isolate* isolate = env->isolate();
29
15824
  Local<Value> argv[] = {OneByteString(isolate, event), message};
30
31
7912
  return MakeCallback(isolate, process, "emit", arraysize(argv), argv, {0, 0});
32
}
33
34
42
Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
35
                                      const char* warning,
36
                                      const char* type,
37
                                      const char* code) {
38
42
  if (!env->can_call_into_js()) return Just(false);
39
40
84
  HandleScope handle_scope(env->isolate());
41
42
  Context::Scope context_scope(env->context());
42
43
42
  Local<Object> process = env->process_object();
44
  Local<Value> emit_warning;
45
168
  if (!process->Get(env->context(), env->emit_warning_string())
46
42
           .ToLocal(&emit_warning)) {
47
1
    return Nothing<bool>();
48
  }
49
50
41
  if (!emit_warning->IsFunction()) return Just(false);
51
52
41
  int argc = 0;
53
164
  Local<Value> args[3];  // warning, type, code
54
55
  // The caller has to be able to handle a failure anyway, so we might as well
56
  // do proper error checking for string creation.
57
82
  if (!String::NewFromUtf8(env->isolate(), warning, NewStringType::kNormal)
58
82
           .ToLocal(&args[argc++])) {
59
    return Nothing<bool>();
60
  }
61
41
  if (type != nullptr) {
62
10
    if (!String::NewFromOneByte(env->isolate(),
63
                                reinterpret_cast<const uint8_t*>(type),
64
5
                                NewStringType::kNormal)
65
10
             .ToLocal(&args[argc++])) {
66
      return Nothing<bool>();
67
    }
68

14
    if (code != nullptr &&
69
13
        !String::NewFromOneByte(env->isolate(),
70
                                reinterpret_cast<const uint8_t*>(code),
71
4
                                NewStringType::kNormal)
72
8
             .ToLocal(&args[argc++])) {
73
      return Nothing<bool>();
74
    }
75
  }
76
77
  // MakeCallback() unneeded because emitWarning is internal code, it calls
78
  // process.emit('warning', ...), but does so on the nextTick.
79
164
  if (emit_warning.As<Function>()
80
164
          ->Call(env->context(), process, argc, args)
81
          .IsEmpty()) {
82
1
    return Nothing<bool>();
83
  }
84
40
  return Just(true);
85
}
86
87
// Call process.emitWarning(str), fmt is a snprintf() format string
88
37
Maybe<bool> ProcessEmitWarning(Environment* env, const char* fmt, ...) {
89
  char warning[1024];
90
  va_list ap;
91
92
37
  va_start(ap, fmt);
93
37
  vsnprintf(warning, sizeof(warning), fmt, ap);
94
37
  va_end(ap);
95
96
37
  return ProcessEmitWarningGeneric(env, warning);
97
}
98
99
100
4202
std::set<std::string> experimental_warnings;
101
102
Maybe<bool> ProcessEmitExperimentalWarning(Environment* env,
103
                                          const char* warning) {
104
  if (experimental_warnings.find(warning) != experimental_warnings.end())
105
    return Nothing<bool>();
106
107
  experimental_warnings.insert(warning);
108
  std::string message(warning);
109
  message.append(
110
      " is an experimental feature. This feature could change at any time");
111
  return ProcessEmitWarningGeneric(env, message.c_str(), "ExperimentalWarning");
112
}
113
114
4
Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
115
                                          const char* warning,
116
                                          const char* deprecation_code) {
117
  return ProcessEmitWarningGeneric(
118
4
      env, warning, "DeprecationWarning", deprecation_code);
119
}
120
121

12606
}  // namespace node