Skip to content

Commit 80f86e5

Browse files
committed
src: add C++ ProcessEmitWarningSync()
PR-URL: #51977 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 63391e7 commit 80f86e5

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

lib/internal/bootstrap/node.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,9 @@ ObjectDefineProperty(process, 'features', {
298298
hasUncaughtExceptionCaptureCallback;
299299
}
300300

301-
const { emitWarning } = require('internal/process/warning');
301+
const { emitWarning, emitWarningSync } = require('internal/process/warning');
302302
process.emitWarning = emitWarning;
303+
internalBinding('process_methods').setEmitWarningSync(emitWarningSync);
303304

304305
// We initialize the tick callbacks and the timer callbacks last during
305306
// bootstrap to make sure that any operation done before this are synchronous.

src/env_properties.h

+1
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@
438438
V(performance_entry_callback, v8::Function) \
439439
V(prepare_stack_trace_callback, v8::Function) \
440440
V(process_object, v8::Object) \
441+
V(process_emit_warning_sync, v8::Function) \
441442
V(primordials, v8::Object) \
442443
V(primordials_safe_map_prototype_object, v8::Object) \
443444
V(primordials_safe_set_prototype_object, v8::Object) \

src/node_process.h

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ template <typename... Args>
3636
inline v8::Maybe<bool> ProcessEmitWarning(Environment* env,
3737
const char* fmt,
3838
Args&&... args);
39+
40+
v8::Maybe<bool> ProcessEmitWarningSync(Environment* env, const char* message);
3941
v8::Maybe<bool> ProcessEmitExperimentalWarning(Environment* env,
4042
const char* warning);
4143
v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,

src/node_process_events.cc

+18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ using v8::Object;
1818
using v8::String;
1919
using v8::Value;
2020

21+
Maybe<bool> ProcessEmitWarningSync(Environment* env, const char* message) {
22+
Isolate* isolate = env->isolate();
23+
Local<Context> context = env->context();
24+
Local<String> message_string = OneByteString(isolate, message);
25+
26+
Local<Value> argv[] = {message_string};
27+
Local<Function> emit_function = env->process_emit_warning_sync();
28+
// If this fails, this is called too early - before the bootstrap is even
29+
// finished.
30+
CHECK(!emit_function.IsEmpty());
31+
if (emit_function.As<Function>()
32+
->Call(context, v8::Undefined(isolate), arraysize(argv), argv)
33+
.IsEmpty()) {
34+
return Nothing<bool>();
35+
}
36+
return Just(true);
37+
}
38+
2139
MaybeLocal<Value> ProcessEmit(Environment* env,
2240
const char* event,
2341
Local<Value> message) {

src/node_process_methods.cc

+11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ using v8::ArrayBuffer;
4141
using v8::CFunction;
4242
using v8::Context;
4343
using v8::Float64Array;
44+
using v8::Function;
4445
using v8::FunctionCallbackInfo;
4546
using v8::HeapStatistics;
4647
using v8::Integer;
@@ -622,6 +623,12 @@ void BindingData::Deserialize(Local<Context> context,
622623
CHECK_NOT_NULL(binding);
623624
}
624625

626+
static void SetEmitWarningSync(const FunctionCallbackInfo<Value>& args) {
627+
CHECK(args[0]->IsFunction());
628+
Environment* env = Environment::GetCurrent(args);
629+
env->set_process_emit_warning_sync(args[0].As<Function>());
630+
}
631+
625632
static void CreatePerIsolateProperties(IsolateData* isolate_data,
626633
Local<ObjectTemplate> target) {
627634
Isolate* isolate = isolate_data->isolate();
@@ -655,6 +662,8 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
655662
SetMethod(isolate, target, "patchProcessObject", PatchProcessObject);
656663

657664
SetMethod(isolate, target, "loadEnvFile", LoadEnvFile);
665+
666+
SetMethod(isolate, target, "setEmitWarningSync", SetEmitWarningSync);
658667
}
659668

660669
static void CreatePerContextProperties(Local<Object> target,
@@ -695,6 +704,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
695704
registry->Register(PatchProcessObject);
696705

697706
registry->Register(LoadEnvFile);
707+
708+
registry->Register(SetEmitWarningSync);
698709
}
699710

700711
} // namespace process

0 commit comments

Comments
 (0)