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