GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_process_events.cc Lines: 34 44 77.3 %
Date: 2022-08-16 04:20:39 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
10303
MaybeLocal<Value> ProcessEmit(Environment* env,
22
                              const char* event,
23
                              Local<Value> message) {
24
10303
  Isolate* isolate = env->isolate();
25
26
  Local<String> event_string;
27
10303
  if (!String::NewFromOneByte(isolate, reinterpret_cast<const uint8_t*>(event))
28
10303
      .ToLocal(&event_string)) return MaybeLocal<Value>();
29
30
10303
  Local<Object> process = env->process_object();
31
10303
  Local<Value> argv[] = {event_string, message};
32
10303
  return MakeCallback(isolate, process, "emit", arraysize(argv), argv, {0, 0});
33
}
34
35
43
Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
36
                                      const char* warning,
37
                                      const char* type,
38
                                      const char* code) {
39
43
  if (!env->can_call_into_js()) return Just(false);
40
41
86
  HandleScope handle_scope(env->isolate());
42
43
  Context::Scope context_scope(env->context());
43
44
43
  Local<Object> process = env->process_object();
45
  Local<Value> emit_warning;
46
86
  if (!process->Get(env->context(), env->emit_warning_string())
47
43
           .ToLocal(&emit_warning)) {
48
1
    return Nothing<bool>();
49
  }
50
51
42
  if (!emit_warning->IsFunction()) return Just(false);
52
53
42
  int argc = 0;
54
168
  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
84
  if (!String::NewFromUtf8(env->isolate(), warning).ToLocal(&args[argc++]))
59
    return Nothing<bool>();
60
61
42
  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
42
  if (emit_warning.As<Function>()
78
42
          ->Call(env->context(), process, argc, args)
79
42
          .IsEmpty()) {
80
1
    return Nothing<bool>();
81
  }
82
41
  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