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

29
    if (code != nullptr &&
70
28
        !String::NewFromOneByte(env->isolate(),
71
9
                                reinterpret_cast<const uint8_t*>(code))
72
18
             .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
160
  if (emit_warning.As<Function>()
80
160
          ->Call(env->context(), process, argc, args)
81
          .IsEmpty()) {
82
1
    return Nothing<bool>();
83
  }
84
39
  return Just(true);
85
}
86
87
// Call process.emitWarning(str), fmt is a snprintf() format string
88
31
Maybe<bool> ProcessEmitWarning(Environment* env, const char* fmt, ...) {
89
  char warning[1024];
90
  va_list ap;
91
92
31
  va_start(ap, fmt);
93
31
  vsnprintf(warning, sizeof(warning), fmt, ap);
94
31
  va_end(ap);
95
96
31
  return ProcessEmitWarningGeneric(env, warning);
97
}
98
99
100
4460
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
9
Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
115
                                          const char* warning,
116
                                          const char* deprecation_code) {
117
  return ProcessEmitWarningGeneric(
118
9
      env, warning, "DeprecationWarning", deprecation_code);
119
}
120
121

13380
}  // namespace node