GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_process_events.cc Lines: 34 44 77.3 %
Date: 2021-09-24 04:12:43 Branches: 17 26 65.4 %

Line Branch Exec Source
1
#include <set>
2
3
#include "env-inl.h"
4
#include "node_process-inl.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::Nothing;
17
using v8::Object;
18
using v8::String;
19
using v8::Value;
20
21
9656
MaybeLocal<Value> ProcessEmit(Environment* env,
22
                              const char* event,
23
                              Local<Value> message) {
24
  // Send message to enable debug in cluster workers
25
9656
  Isolate* isolate = env->isolate();
26
27
  Local<String> event_string;
28
9656
  if (!String::NewFromOneByte(isolate, reinterpret_cast<const uint8_t*>(event))
29
9656
      .ToLocal(&event_string)) return MaybeLocal<Value>();
30
31
9656
  Local<Object> process = env->process_object();
32
9656
  Local<Value> argv[] = {event_string, message};
33
9656
  return MakeCallback(isolate, process, "emit", arraysize(argv), argv, {0, 0});
34
}
35
36
45
Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
37
                                      const char* warning,
38
                                      const char* type,
39
                                      const char* code) {
40
45
  if (!env->can_call_into_js()) return Just(false);
41
42
90
  HandleScope handle_scope(env->isolate());
43
45
  Context::Scope context_scope(env->context());
44
45
45
  Local<Object> process = env->process_object();
46
  Local<Value> emit_warning;
47
90
  if (!process->Get(env->context(), env->emit_warning_string())
48
45
           .ToLocal(&emit_warning)) {
49
1
    return Nothing<bool>();
50
  }
51
52
44
  if (!emit_warning->IsFunction()) return Just(false);
53
54
44
  int argc = 0;
55
176
  Local<Value> args[3];  // warning, type, code
56
57
  // The caller has to be able to handle a failure anyway, so we might as well
58
  // do proper error checking for string creation.
59
88
  if (!String::NewFromUtf8(env->isolate(), warning).ToLocal(&args[argc++]))
60
    return Nothing<bool>();
61
62
44
  if (type != nullptr) {
63
6
    if (!String::NewFromOneByte(env->isolate(),
64
6
                                reinterpret_cast<const uint8_t*>(type))
65
12
             .ToLocal(&args[argc++])) {
66
      return Nothing<bool>();
67
    }
68
11
    if (code != nullptr &&
69
6
        !String::NewFromOneByte(env->isolate(),
70
5
                                reinterpret_cast<const uint8_t*>(code))
71
10
             .ToLocal(&args[argc++])) {
72
      return Nothing<bool>();
73
    }
74
  }
75
76
  // MakeCallback() unneeded because emitWarning is internal code, it calls
77
  // process.emit('warning', ...), but does so on the nextTick.
78
44
  if (emit_warning.As<Function>()
79
44
          ->Call(env->context(), process, argc, args)
80
44
          .IsEmpty()) {
81
1
    return Nothing<bool>();
82
  }
83
43
  return Just(true);
84
}
85
86
87
std::set<std::string> experimental_warnings;
88
89
Maybe<bool> ProcessEmitExperimentalWarning(Environment* env,
90
                                          const char* warning) {
91
  if (experimental_warnings.find(warning) != experimental_warnings.end())
92
    return Nothing<bool>();
93
94
  experimental_warnings.insert(warning);
95
  std::string message(warning);
96
  message.append(
97
      " is an experimental feature. This feature could change at any time");
98
  return ProcessEmitWarningGeneric(env, message.c_str(), "ExperimentalWarning");
99
}
100
101
5
Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
102
                                          const char* warning,
103
                                          const char* deprecation_code) {
104
  return ProcessEmitWarningGeneric(
105
5
      env, warning, "DeprecationWarning", deprecation_code);
106
}
107
108
}  // namespace node