diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 8812c3b35184b2..58aba09b75b8be 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -297,8 +297,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 1ecd06e1a3546d..98371fd493da53 100644 --- a/src/env_properties.h +++ b/src/env_properties.h @@ -434,6 +434,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 e8d621ddb48bd2..d86858e47a3e9f 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; @@ -619,6 +620,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(); @@ -651,6 +658,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, @@ -690,6 +699,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(PatchProcessObject); registry->Register(LoadEnvFile); + + registry->Register(SetEmitWarningSync); } } // namespace process