src: add C++ ProcessEmitWarningSync()

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Joyee Cheung
2024-03-05 16:40:34 +01:00
parent 63391e749d
commit 80f86e5d02
5 changed files with 34 additions and 1 deletions

View File

@@ -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.

View File

@@ -438,6 +438,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) \

View File

@@ -36,6 +36,8 @@ template <typename... Args>
inline v8::Maybe<bool> ProcessEmitWarning(Environment* env,
const char* fmt,
Args&&... args);
v8::Maybe<bool> ProcessEmitWarningSync(Environment* env, const char* message);
v8::Maybe<bool> ProcessEmitExperimentalWarning(Environment* env,
const char* warning);
v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,

View File

@@ -18,6 +18,24 @@ using v8::Object;
using v8::String;
using v8::Value;
Maybe<bool> ProcessEmitWarningSync(Environment* env, const char* message) {
Isolate* isolate = env->isolate();
Local<Context> context = env->context();
Local<String> message_string = OneByteString(isolate, message);
Local<Value> argv[] = {message_string};
Local<Function> 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<Function>()
->Call(context, v8::Undefined(isolate), arraysize(argv), argv)
.IsEmpty()) {
return Nothing<bool>();
}
return Just(true);
}
MaybeLocal<Value> ProcessEmit(Environment* env,
const char* event,
Local<Value> message) {

View File

@@ -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> context,
CHECK_NOT_NULL(binding);
}
static void SetEmitWarningSync(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsFunction());
Environment* env = Environment::GetCurrent(args);
env->set_process_emit_warning_sync(args[0].As<Function>());
}
static void CreatePerIsolateProperties(IsolateData* isolate_data,
Local<ObjectTemplate> 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<Object> target,
@@ -695,6 +704,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(PatchProcessObject);
registry->Register(LoadEnvFile);
registry->Register(SetEmitWarningSync);
}
} // namespace process