Skip to content

Commit 28baf26

Browse files
joyeecheungBridgeAR
authored andcommitted
process: move C++ process events into node_process_events.cc
Move the C++ `process.emit` and `process.emitWarning` methods from `node.cc` into into `node_process_events.cc`, and reuse the implementation in other places that need to do `process.emit` in C++. PR-URL: #25397 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 2ed3fa1 commit 28baf26

File tree

5 files changed

+122
-113
lines changed

5 files changed

+122
-113
lines changed

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@
371371
'src/node_perf.cc',
372372
'src/node_platform.cc',
373373
'src/node_postmortem_metadata.cc',
374+
'src/node_process_events.cc',
374375
'src/node_process_methods.cc',
375376
'src/node_process_object.cc',
376377
'src/node_serdes.cc',

src/inspector_agent.cc

+2-15
Original file line numberDiff line numberDiff line change
@@ -370,26 +370,13 @@ class SameThreadInspectorSession : public InspectorSession {
370370
void NotifyClusterWorkersDebugEnabled(Environment* env) {
371371
Isolate* isolate = env->isolate();
372372
HandleScope handle_scope(isolate);
373-
auto context = env->context();
373+
Local<Context> context = env->context();
374374

375375
// Send message to enable debug in cluster workers
376-
Local<Object> process_object = env->process_object();
377-
Local<Value> emit_fn =
378-
process_object->Get(context, FIXED_ONE_BYTE_STRING(isolate, "emit"))
379-
.ToLocalChecked();
380-
// In case the thread started early during the startup
381-
if (!emit_fn->IsFunction())
382-
return;
383-
384376
Local<Object> message = Object::New(isolate);
385377
message->Set(context, FIXED_ONE_BYTE_STRING(isolate, "cmd"),
386378
FIXED_ONE_BYTE_STRING(isolate, "NODE_DEBUG_ENABLED")).FromJust();
387-
Local<Value> argv[] = {
388-
FIXED_ONE_BYTE_STRING(isolate, "internalMessage"),
389-
message
390-
};
391-
MakeCallback(env->isolate(), process_object, emit_fn.As<Function>(),
392-
arraysize(argv), argv, {0, 0});
379+
ProcessEmit(env, "internalMessage", message);
393380
}
394381

395382
#ifdef _WIN32

src/node.cc

+7-98
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ using v8::Maybe;
124124
using v8::MaybeLocal;
125125
using v8::Message;
126126
using v8::MicrotasksPolicy;
127-
using v8::NewStringType;
128-
using v8::Nothing;
129127
using v8::Object;
130128
using v8::ObjectTemplate;
131129
using v8::Script;
@@ -586,82 +584,6 @@ void Exit(const FunctionCallbackInfo<Value>& args) {
586584
env->Exit(code);
587585
}
588586

589-
static Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
590-
const char* warning,
591-
const char* type = nullptr,
592-
const char* code = nullptr) {
593-
HandleScope handle_scope(env->isolate());
594-
Context::Scope context_scope(env->context());
595-
596-
Local<Object> process = env->process_object();
597-
Local<Value> emit_warning;
598-
if (!process->Get(env->context(),
599-
env->emit_warning_string()).ToLocal(&emit_warning)) {
600-
return Nothing<bool>();
601-
}
602-
603-
if (!emit_warning->IsFunction()) return Just(false);
604-
605-
int argc = 0;
606-
Local<Value> args[3]; // warning, type, code
607-
608-
// The caller has to be able to handle a failure anyway, so we might as well
609-
// do proper error checking for string creation.
610-
if (!String::NewFromUtf8(env->isolate(),
611-
warning,
612-
NewStringType::kNormal).ToLocal(&args[argc++])) {
613-
return Nothing<bool>();
614-
}
615-
if (type != nullptr) {
616-
if (!String::NewFromOneByte(env->isolate(),
617-
reinterpret_cast<const uint8_t*>(type),
618-
NewStringType::kNormal)
619-
.ToLocal(&args[argc++])) {
620-
return Nothing<bool>();
621-
}
622-
if (code != nullptr &&
623-
!String::NewFromOneByte(env->isolate(),
624-
reinterpret_cast<const uint8_t*>(code),
625-
NewStringType::kNormal)
626-
.ToLocal(&args[argc++])) {
627-
return Nothing<bool>();
628-
}
629-
}
630-
631-
// MakeCallback() unneeded because emitWarning is internal code, it calls
632-
// process.emit('warning', ...), but does so on the nextTick.
633-
if (emit_warning.As<Function>()->Call(env->context(),
634-
process,
635-
argc,
636-
args).IsEmpty()) {
637-
return Nothing<bool>();
638-
}
639-
return Just(true);
640-
}
641-
642-
643-
// Call process.emitWarning(str), fmt is a snprintf() format string
644-
Maybe<bool> ProcessEmitWarning(Environment* env, const char* fmt, ...) {
645-
char warning[1024];
646-
va_list ap;
647-
648-
va_start(ap, fmt);
649-
vsnprintf(warning, sizeof(warning), fmt, ap);
650-
va_end(ap);
651-
652-
return ProcessEmitWarningGeneric(env, warning);
653-
}
654-
655-
656-
Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
657-
const char* warning,
658-
const char* deprecation_code) {
659-
return ProcessEmitWarningGeneric(env,
660-
warning,
661-
"DeprecationWarning",
662-
deprecation_code);
663-
}
664-
665587
static void OnMessage(Local<Message> message, Local<Value> error) {
666588
Isolate* isolate = message->GetIsolate();
667589
switch (message->ErrorLevel()) {
@@ -1164,19 +1086,14 @@ void RunBeforeExit(Environment* env) {
11641086
void EmitBeforeExit(Environment* env) {
11651087
HandleScope handle_scope(env->isolate());
11661088
Context::Scope context_scope(env->context());
1167-
Local<Object> process_object = env->process_object();
1168-
Local<String> exit_code = env->exit_code_string();
1169-
Local<Value> args[] = {
1170-
FIXED_ONE_BYTE_STRING(env->isolate(), "beforeExit"),
1171-
process_object->Get(env->context(), exit_code).ToLocalChecked()
1172-
->ToInteger(env->context()).ToLocalChecked()
1173-
};
1174-
MakeCallback(env->isolate(),
1175-
process_object, "emit", arraysize(args), args,
1176-
{0, 0}).ToLocalChecked();
1089+
Local<Value> exit_code = env->process_object()
1090+
->Get(env->context(), env->exit_code_string())
1091+
.ToLocalChecked()
1092+
->ToInteger(env->context())
1093+
.ToLocalChecked();
1094+
ProcessEmit(env, "beforeExit", exit_code).ToLocalChecked();
11771095
}
11781096

1179-
11801097
int EmitExit(Environment* env) {
11811098
// process.emit('exit')
11821099
HandleScope handle_scope(env->isolate());
@@ -1189,15 +1106,7 @@ int EmitExit(Environment* env) {
11891106
Local<String> exit_code = env->exit_code_string();
11901107
int code = process_object->Get(env->context(), exit_code).ToLocalChecked()
11911108
->Int32Value(env->context()).ToChecked();
1192-
1193-
Local<Value> args[] = {
1194-
FIXED_ONE_BYTE_STRING(env->isolate(), "exit"),
1195-
Integer::New(env->isolate(), code)
1196-
};
1197-
1198-
MakeCallback(env->isolate(),
1199-
process_object, "emit", arraysize(args), args,
1200-
{0, 0}).ToLocalChecked();
1109+
ProcessEmit(env, "exit", Integer::New(env->isolate(), code));
12011110

12021111
// Reload exit code, it may be changed by `emit('exit')`
12031112
return process_object->Get(env->context(), exit_code).ToLocalChecked()

src/node_process.h

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ v8::Local<v8::Object> CreateEnvVarProxy(v8::Local<v8::Context> context,
1717
// function, it is useful to bypass JavaScript entirely.
1818
void RawDebug(const v8::FunctionCallbackInfo<v8::Value>& args);
1919

20+
v8::MaybeLocal<v8::Value> ProcessEmit(Environment* env,
21+
const char* event,
22+
v8::Local<v8::Value> message);
23+
24+
v8::Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
25+
const char* warning,
26+
const char* type = nullptr,
27+
const char* code = nullptr);
28+
2029
v8::Maybe<bool> ProcessEmitWarning(Environment* env, const char* fmt, ...);
2130
v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
2231
const char* warning,

src/node_process_events.cc

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <stdarg.h>
2+
3+
#include "env.h"
4+
#include "node_internals.h"
5+
#include "node_process.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+
MaybeLocal<Value> ProcessEmit(Environment* env,
23+
const char* event,
24+
Local<Value> message) {
25+
// Send message to enable debug in cluster workers
26+
Local<Object> process = env->process_object();
27+
Isolate* isolate = env->isolate();
28+
Local<Value> argv[] = {OneByteString(isolate, event), message};
29+
30+
return MakeCallback(isolate, process, "emit", arraysize(argv), argv, {0, 0});
31+
}
32+
33+
Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
34+
const char* warning,
35+
const char* type,
36+
const char* code) {
37+
HandleScope handle_scope(env->isolate());
38+
Context::Scope context_scope(env->context());
39+
40+
Local<Object> process = env->process_object();
41+
Local<Value> emit_warning;
42+
if (!process->Get(env->context(), env->emit_warning_string())
43+
.ToLocal(&emit_warning)) {
44+
return Nothing<bool>();
45+
}
46+
47+
if (!emit_warning->IsFunction()) return Just(false);
48+
49+
int argc = 0;
50+
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+
if (!String::NewFromUtf8(env->isolate(), warning, NewStringType::kNormal)
55+
.ToLocal(&args[argc++])) {
56+
return Nothing<bool>();
57+
}
58+
if (type != nullptr) {
59+
if (!String::NewFromOneByte(env->isolate(),
60+
reinterpret_cast<const uint8_t*>(type),
61+
NewStringType::kNormal)
62+
.ToLocal(&args[argc++])) {
63+
return Nothing<bool>();
64+
}
65+
if (code != nullptr &&
66+
!String::NewFromOneByte(env->isolate(),
67+
reinterpret_cast<const uint8_t*>(code),
68+
NewStringType::kNormal)
69+
.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+
if (emit_warning.As<Function>()
77+
->Call(env->context(), process, argc, args)
78+
.IsEmpty()) {
79+
return Nothing<bool>();
80+
}
81+
return Just(true);
82+
}
83+
84+
// Call process.emitWarning(str), fmt is a snprintf() format string
85+
Maybe<bool> ProcessEmitWarning(Environment* env, const char* fmt, ...) {
86+
char warning[1024];
87+
va_list ap;
88+
89+
va_start(ap, fmt);
90+
vsnprintf(warning, sizeof(warning), fmt, ap);
91+
va_end(ap);
92+
93+
return ProcessEmitWarningGeneric(env, warning);
94+
}
95+
96+
Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
97+
const char* warning,
98+
const char* deprecation_code) {
99+
return ProcessEmitWarningGeneric(
100+
env, warning, "DeprecationWarning", deprecation_code);
101+
}
102+
103+
} // namespace node

0 commit comments

Comments
 (0)