Skip to content

Commit

Permalink
src: add C++ ProcessEmitWarningSync()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
joyeecheung authored and marco-ippolito committed May 3, 2024
1 parent f8209ff commit b33eff8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
2 changes: 2 additions & 0 deletions src/node_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions src/node_process_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -695,6 +704,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(PatchProcessObject);

registry->Register(LoadEnvFile);

registry->Register(SetEmitWarningSync);
}

} // namespace process
Expand Down

0 comments on commit b33eff8

Please sign in to comment.