From 8bd9a848d7a34c7b2dc64dcef47ff9aa52e2942e Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 2 Dec 2024 10:08:33 -0500 Subject: [PATCH] src: use std::string_view for process emit fns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/56086 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Juan José Arboleda --- src/node_process.h | 20 +++++++++------- src/node_process_events.cc | 49 +++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/node_process.h b/src/node_process.h index d4f1c0d45dec50..461a92161ab2e5 100644 --- a/src/node_process.h +++ b/src/node_process.h @@ -24,25 +24,27 @@ void CreateEnvProxyTemplate(IsolateData* isolate_data); void RawDebug(const v8::FunctionCallbackInfo& args); v8::MaybeLocal ProcessEmit(Environment* env, - const char* event, + std::string_view event, v8::Local message); v8::Maybe ProcessEmitWarningGeneric(Environment* env, - const char* warning, - const char* type = nullptr, - const char* code = nullptr); + std::string_view warning, + std::string_view type = "", + std::string_view code = ""); template inline v8::Maybe ProcessEmitWarning(Environment* env, const char* fmt, Args&&... args); -v8::Maybe ProcessEmitWarningSync(Environment* env, const char* message); +v8::Maybe ProcessEmitWarningSync(Environment* env, + std::string_view message); v8::Maybe ProcessEmitExperimentalWarning(Environment* env, - const char* warning); -v8::Maybe ProcessEmitDeprecationWarning(Environment* env, - const char* warning, - const char* deprecation_code); + const std::string& warning); +v8::Maybe ProcessEmitDeprecationWarning( + Environment* env, + const std::string& warning, + std::string_view deprecation_code); v8::MaybeLocal CreateProcessObject(Realm* env); void PatchProcessObject(const v8::FunctionCallbackInfo& args); diff --git a/src/node_process_events.cc b/src/node_process_events.cc index 19774607830a93..128ad9fbb1f257 100644 --- a/src/node_process_events.cc +++ b/src/node_process_events.cc @@ -18,10 +18,11 @@ using v8::Object; using v8::String; using v8::Value; -Maybe ProcessEmitWarningSync(Environment* env, const char* message) { +Maybe ProcessEmitWarningSync(Environment* env, std::string_view message) { Isolate* isolate = env->isolate(); Local context = env->context(); - Local message_string = OneByteString(isolate, message); + Local message_string = + OneByteString(isolate, message.data(), message.size()); Local argv[] = {message_string}; Local emit_function = env->process_emit_warning_sync(); @@ -37,13 +38,14 @@ Maybe ProcessEmitWarningSync(Environment* env, const char* message) { } MaybeLocal ProcessEmit(Environment* env, - const char* event, + std::string_view event, Local message) { Isolate* isolate = env->isolate(); - Local event_string; - if (!String::NewFromOneByte(isolate, reinterpret_cast(event)) - .ToLocal(&event_string)) return MaybeLocal(); + Local event_string; + if (!ToV8Value(env->context(), event).ToLocal(&event_string)) { + return MaybeLocal(); + } Local process = env->process_object(); Local argv[] = {event_string, message}; @@ -51,10 +53,12 @@ MaybeLocal ProcessEmit(Environment* env, } Maybe ProcessEmitWarningGeneric(Environment* env, - const char* warning, - const char* type, - const char* code) { - if (!env->can_call_into_js()) return Just(false); + std::string_view warning, + std::string_view type, + std::string_view code) { + if (!env->can_call_into_js()) { + return Just(false); + } HandleScope handle_scope(env->isolate()); Context::Scope context_scope(env->context()); @@ -73,19 +77,16 @@ Maybe ProcessEmitWarningGeneric(Environment* env, // The caller has to be able to handle a failure anyway, so we might as well // do proper error checking for string creation. - if (!String::NewFromUtf8(env->isolate(), warning).ToLocal(&args[argc++])) + if (!ToV8Value(env->context(), warning).ToLocal(&args[argc++])) { return Nothing(); + } - if (type != nullptr) { - if (!String::NewFromOneByte(env->isolate(), - reinterpret_cast(type)) - .ToLocal(&args[argc++])) { + if (!type.empty()) { + if (!ToV8Value(env->context(), type).ToLocal(&args[argc++])) { return Nothing(); } - if (code != nullptr && - !String::NewFromOneByte(env->isolate(), - reinterpret_cast(code)) - .ToLocal(&args[argc++])) { + if (!code.empty() && + !ToV8Value(env->context(), code).ToLocal(&args[argc++])) { return Nothing(); } } @@ -100,13 +101,11 @@ Maybe ProcessEmitWarningGeneric(Environment* env, return Just(true); } - std::set experimental_warnings; Maybe ProcessEmitExperimentalWarning(Environment* env, - const char* warning) { - if (experimental_warnings.find(warning) != experimental_warnings.end()) - return Nothing(); + const std::string& warning) { + if (experimental_warnings.contains(warning)) return Nothing(); experimental_warnings.insert(warning); std::string message(warning); @@ -115,8 +114,8 @@ Maybe ProcessEmitExperimentalWarning(Environment* env, } Maybe ProcessEmitDeprecationWarning(Environment* env, - const char* warning, - const char* deprecation_code) { + const std::string& warning, + std::string_view deprecation_code) { return ProcessEmitWarningGeneric( env, warning, "DeprecationWarning", deprecation_code); }