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: 36 39 92.3 %
Date: 2019-09-07 22:28:56 Branches: 17 22 77.3 %

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

11
    if (code != nullptr &&
66
        !String::NewFromOneByte(env->isolate(),
67
                                reinterpret_cast<const uint8_t*>(code),
68
2
                                NewStringType::kNormal)
69
11
             .ToLocal(&args[argc++])) {
70
      return Nothing<bool>();
71
    }
72
  }
73
74
  // MakeCallback() unneeded because emitWarning is internal code, it calls
75
  // process.emit('warning', ...), but does so on the nextTick.
76
56
  if (emit_warning.As<Function>()
77
112
          ->Call(env->context(), process, argc, args)
78
56
          .IsEmpty()) {
79
1
    return Nothing<bool>();
80
  }
81
56
  return Just(true);
82
}
83
84
// Call process.emitWarning(str), fmt is a snprintf() format string
85
26
Maybe<bool> ProcessEmitWarning(Environment* env, const char* fmt, ...) {
86
  char warning[1024];
87
  va_list ap;
88
89
26
  va_start(ap, fmt);
90
26
  vsnprintf(warning, sizeof(warning), fmt, ap);
91
26
  va_end(ap);
92
93
26
  return ProcessEmitWarningGeneric(env, warning);
94
}
95
96
2
Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
97
                                          const char* warning,
98
                                          const char* deprecation_code) {
99
  return ProcessEmitWarningGeneric(
100
2
      env, warning, "DeprecationWarning", deprecation_code);
101
}
102
103
}  // namespace node