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 |
|
8985 |
MaybeLocal<Value> ProcessEmit(Environment* env, |
23 |
|
|
const char* event, |
24 |
|
|
Local<Value> message) { |
25 |
|
|
// Send message to enable debug in cluster workers |
26 |
|
8985 |
Isolate* isolate = env->isolate(); |
27 |
|
|
|
28 |
|
|
Local<String> event_string; |
29 |
✗✓ |
17968 |
if (!String::NewFromOneByte(isolate, reinterpret_cast<const uint8_t*>(event)) |
30 |
|
8984 |
.ToLocal(&event_string)) return MaybeLocal<Value>(); |
31 |
|
|
|
32 |
|
8984 |
Local<Object> process = env->process_object(); |
33 |
|
17972 |
Local<Value> argv[] = {event_string, message}; |
34 |
|
8986 |
return MakeCallback(isolate, process, "emit", arraysize(argv), argv, {0, 0}); |
35 |
|
|
} |
36 |
|
|
|
37 |
|
36 |
Maybe<bool> ProcessEmitWarningGeneric(Environment* env, |
38 |
|
|
const char* warning, |
39 |
|
|
const char* type, |
40 |
|
|
const char* code) { |
41 |
✗✓ |
36 |
if (!env->can_call_into_js()) return Just(false); |
42 |
|
|
|
43 |
|
72 |
HandleScope handle_scope(env->isolate()); |
44 |
|
36 |
Context::Scope context_scope(env->context()); |
45 |
|
|
|
46 |
|
36 |
Local<Object> process = env->process_object(); |
47 |
|
|
Local<Value> emit_warning; |
48 |
✓✓ |
144 |
if (!process->Get(env->context(), env->emit_warning_string()) |
49 |
|
36 |
.ToLocal(&emit_warning)) { |
50 |
|
1 |
return Nothing<bool>(); |
51 |
|
|
} |
52 |
|
|
|
53 |
✗✓ |
35 |
if (!emit_warning->IsFunction()) return Just(false); |
54 |
|
|
|
55 |
|
35 |
int argc = 0; |
56 |
✓✓ |
140 |
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 |
✗✓ |
70 |
if (!String::NewFromUtf8(env->isolate(), warning).ToLocal(&args[argc++])) |
61 |
|
|
return Nothing<bool>(); |
62 |
|
|
|
63 |
✓✓ |
35 |
if (type != nullptr) { |
64 |
✗✓ |
10 |
if (!String::NewFromOneByte(env->isolate(), |
65 |
|
5 |
reinterpret_cast<const uint8_t*>(type)) |
66 |
|
10 |
.ToLocal(&args[argc++])) { |
67 |
|
|
return Nothing<bool>(); |
68 |
|
|
} |
69 |
✓✓✗✓ ✗✓ |
14 |
if (code != nullptr && |
70 |
|
13 |
!String::NewFromOneByte(env->isolate(), |
71 |
|
4 |
reinterpret_cast<const uint8_t*>(code)) |
72 |
|
8 |
.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 |
✓✓ |
140 |
if (emit_warning.As<Function>() |
80 |
|
140 |
->Call(env->context(), process, argc, args) |
81 |
|
|
.IsEmpty()) { |
82 |
|
1 |
return Nothing<bool>(); |
83 |
|
|
} |
84 |
|
34 |
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 |
|
4678 |
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 |
|
4 |
Maybe<bool> ProcessEmitDeprecationWarning(Environment* env, |
115 |
|
|
const char* warning, |
116 |
|
|
const char* deprecation_code) { |
117 |
|
|
return ProcessEmitWarningGeneric( |
118 |
|
4 |
env, warning, "DeprecationWarning", deprecation_code); |
119 |
|
|
} |
120 |
|
|
|
121 |
✓✗✓✗
|
14034 |
} // namespace node |