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: 41 51 80.4 %
Date: 2020-05-27 22:15:15 Branches: 19 30 63.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
8216
MaybeLocal<Value> ProcessEmit(Environment* env,
24
                              const char* event,
25
                              Local<Value> message) {
26
  // Send message to enable debug in cluster workers
27
8216
  Isolate* isolate = env->isolate();
28
29
  Local<String> event_string;
30
16432
  if (!String::NewFromOneByte(isolate, reinterpret_cast<const uint8_t*>(event))
31
8216
      .ToLocal(&event_string)) return MaybeLocal<Value>();
32
33
8216
  Local<Object> process = env->process_object();
34
16432
  Local<Value> argv[] = {event_string, message};
35
8216
  return MakeCallback(isolate, process, "emit", arraysize(argv), argv, {0, 0});
36
}
37
38
50
Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
39
                                      const char* warning,
40
                                      const char* type,
41
                                      const char* code) {
42
50
  if (!env->can_call_into_js()) return Just(false);
43
44
100
  HandleScope handle_scope(env->isolate());
45
50
  Context::Scope context_scope(env->context());
46
47
50
  Local<Object> process = env->process_object();
48
  Local<Value> emit_warning;
49
200
  if (!process->Get(env->context(), env->emit_warning_string())
50
50
           .ToLocal(&emit_warning)) {
51
1
    return Nothing<bool>();
52
  }
53
54
49
  if (!emit_warning->IsFunction()) return Just(false);
55
56
49
  int argc = 0;
57
196
  Local<Value> args[3];  // warning, type, code
58
59
  // The caller has to be able to handle a failure anyway, so we might as well
60
  // do proper error checking for string creation.
61
98
  if (!String::NewFromUtf8(env->isolate(), warning, NewStringType::kNormal)
62
98
           .ToLocal(&args[argc++])) {
63
    return Nothing<bool>();
64
  }
65
49
  if (type != nullptr) {
66
22
    if (!String::NewFromOneByte(env->isolate(),
67
                                reinterpret_cast<const uint8_t*>(type),
68
11
                                NewStringType::kNormal)
69
22
             .ToLocal(&args[argc++])) {
70
      return Nothing<bool>();
71
    }
72

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

12978
}  // namespace node