From b33eff887d1fdca2f0894e2b177a3e385850a478 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 5 Mar 2024 16:40:34 +0100 Subject: [PATCH] src: add C++ ProcessEmitWarningSync() PR-URL: https://github.com/nodejs/node/pull/51977 Reviewed-By: Chengzhong Wu Reviewed-By: Matteo Collina Reviewed-By: Guy Bedford Reviewed-By: Antoine du Hamel Reviewed-By: Geoffrey Booth --- lib/internal/bootstrap/node.js | 3 ++- src/env_properties.h | 1 + src/node_process.h | 2 ++ src/node_process_events.cc | 18 ++++++++++++++++++ src/node_process_methods.cc | 11 +++++++++++ 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 85453fd03ca949..d42e766555a83e 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -298,8 +298,9 @@ ObjectDefineProperty(process, 'features', { hasUncaughtExceptionCaptureCallback; } -const { emitWarning } = require('internal/process/warning'); +const { emitWarning, emitWarningSync } = require('internal/process/warning'); process.emitWarning = emitWarning; +internalBinding('process_methods').setEmitWarningSync(emitWarningSync); // We initialize the tick callbacks and the timer callbacks last during // bootstrap to make sure that any operation done before this are synchronous. diff --git a/src/env_properties.h b/src/env_properties.h index 5b251751892165..c7eae579c4ec7d 100644 --- a/src/env_properties.h +++ b/src/env_properties.h @@ -427,6 +427,7 @@ V(performance_entry_callback, v8::Function) \ V(prepare_stack_trace_callback, v8::Function) \ V(process_object, v8::Object) \ + V(process_emit_warning_sync, v8::Function) \ V(primordials, v8::Object) \ V(primordials_safe_map_prototype_object, v8::Object) \ V(primordials_safe_set_prototype_object, v8::Object) \ diff --git a/src/node_process.h b/src/node_process.h index 0a1f65e9bdfa24..142d0e63e18c46 100644 --- a/src/node_process.h +++ b/src/node_process.h @@ -36,6 +36,8 @@ template inline v8::Maybe ProcessEmitWarning(Environment* env, const char* fmt, Args&&... args); + +v8::Maybe ProcessEmitWarningSync(Environment* env, const char* message); v8::Maybe ProcessEmitExperimentalWarning(Environment* env, const char* warning); v8::Maybe ProcessEmitDeprecationWarning(Environment* env, diff --git a/src/node_process_events.cc b/src/node_process_events.cc index f4c39d7dd9eb68..19774607830a93 100644 --- a/src/node_process_events.cc +++ b/src/node_process_events.cc @@ -18,6 +18,24 @@ using v8::Object; using v8::String; using v8::Value; +Maybe ProcessEmitWarningSync(Environment* env, const char* message) { + Isolate* isolate = env->isolate(); + Local context = env->context(); + Local message_string = OneByteString(isolate, message); + + Local argv[] = {message_string}; + Local emit_function = env->process_emit_warning_sync(); + // If this fails, this is called too early - before the bootstrap is even + // finished. + CHECK(!emit_function.IsEmpty()); + if (emit_function.As() + ->Call(context, v8::Undefined(isolate), arraysize(argv), argv) + .IsEmpty()) { + return Nothing(); + } + return Just(true); +} + MaybeLocal ProcessEmit(Environment* env, const char* event, Local message) { diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 5266aae51f677b..bfa9e88a9f2d01 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -41,6 +41,7 @@ using v8::ArrayBuffer; using v8::CFunction; using v8::Context; using v8::Float64Array; +using v8::Function; using v8::FunctionCallbackInfo; using v8::HeapStatistics; using v8::Integer; @@ -622,6 +623,12 @@ void BindingData::Deserialize(Local context, CHECK_NOT_NULL(binding); } +static void SetEmitWarningSync(const FunctionCallbackInfo& args) { + CHECK(args[0]->IsFunction()); + Environment* env = Environment::GetCurrent(args); + env->set_process_emit_warning_sync(args[0].As()); +} + static void CreatePerIsolateProperties(IsolateData* isolate_data, Local target) { Isolate* isolate = isolate_data->isolate(); @@ -655,6 +662,8 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data, SetMethod(isolate, target, "patchProcessObject", PatchProcessObject); SetMethod(isolate, target, "loadEnvFile", LoadEnvFile); + + SetMethod(isolate, target, "setEmitWarningSync", SetEmitWarningSync); } static void CreatePerContextProperties(Local target, @@ -695,6 +704,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(PatchProcessObject); registry->Register(LoadEnvFile); + + registry->Register(SetEmitWarningSync); } } // namespace process