Skip to content

Commit

Permalink
src: use std::string_view for process emit fns
Browse files Browse the repository at this point in the history
PR-URL: #56086
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
  • Loading branch information
anonrig authored Dec 2, 2024
1 parent 61e4ad5 commit 8bd9a84
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
20 changes: 11 additions & 9 deletions src/node_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,27 @@ void CreateEnvProxyTemplate(IsolateData* isolate_data);
void RawDebug(const v8::FunctionCallbackInfo<v8::Value>& args);

v8::MaybeLocal<v8::Value> ProcessEmit(Environment* env,
const char* event,
std::string_view event,
v8::Local<v8::Value> message);

v8::Maybe<bool> 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 <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> ProcessEmitWarningSync(Environment* env,
std::string_view message);
v8::Maybe<bool> ProcessEmitExperimentalWarning(Environment* env,
const char* warning);
v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
const char* warning,
const char* deprecation_code);
const std::string& warning);
v8::Maybe<bool> ProcessEmitDeprecationWarning(
Environment* env,
const std::string& warning,
std::string_view deprecation_code);

v8::MaybeLocal<v8::Object> CreateProcessObject(Realm* env);
void PatchProcessObject(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
49 changes: 24 additions & 25 deletions src/node_process_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ using v8::Object;
using v8::String;
using v8::Value;

Maybe<bool> ProcessEmitWarningSync(Environment* env, const char* message) {
Maybe<bool> ProcessEmitWarningSync(Environment* env, std::string_view message) {
Isolate* isolate = env->isolate();
Local<Context> context = env->context();
Local<String> message_string = OneByteString(isolate, message);
Local<String> message_string =
OneByteString(isolate, message.data(), message.size());

Local<Value> argv[] = {message_string};
Local<Function> emit_function = env->process_emit_warning_sync();
Expand All @@ -37,24 +38,27 @@ Maybe<bool> ProcessEmitWarningSync(Environment* env, const char* message) {
}

MaybeLocal<Value> ProcessEmit(Environment* env,
const char* event,
std::string_view event,
Local<Value> message) {
Isolate* isolate = env->isolate();

Local<String> event_string;
if (!String::NewFromOneByte(isolate, reinterpret_cast<const uint8_t*>(event))
.ToLocal(&event_string)) return MaybeLocal<Value>();
Local<Value> event_string;
if (!ToV8Value(env->context(), event).ToLocal(&event_string)) {
return MaybeLocal<Value>();
}

Local<Object> process = env->process_object();
Local<Value> argv[] = {event_string, message};
return MakeCallback(isolate, process, "emit", arraysize(argv), argv, {0, 0});
}

Maybe<bool> 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());
Expand All @@ -73,19 +77,16 @@ Maybe<bool> 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<bool>();
}

if (type != nullptr) {
if (!String::NewFromOneByte(env->isolate(),
reinterpret_cast<const uint8_t*>(type))
.ToLocal(&args[argc++])) {
if (!type.empty()) {
if (!ToV8Value(env->context(), type).ToLocal(&args[argc++])) {
return Nothing<bool>();
}
if (code != nullptr &&
!String::NewFromOneByte(env->isolate(),
reinterpret_cast<const uint8_t*>(code))
.ToLocal(&args[argc++])) {
if (!code.empty() &&
!ToV8Value(env->context(), code).ToLocal(&args[argc++])) {
return Nothing<bool>();
}
}
Expand All @@ -100,13 +101,11 @@ Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
return Just(true);
}


std::set<std::string> experimental_warnings;

Maybe<bool> ProcessEmitExperimentalWarning(Environment* env,
const char* warning) {
if (experimental_warnings.find(warning) != experimental_warnings.end())
return Nothing<bool>();
const std::string& warning) {
if (experimental_warnings.contains(warning)) return Nothing<bool>();

experimental_warnings.insert(warning);
std::string message(warning);
Expand All @@ -115,8 +114,8 @@ Maybe<bool> ProcessEmitExperimentalWarning(Environment* env,
}

Maybe<bool> 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);
}
Expand Down

0 comments on commit 8bd9a84

Please sign in to comment.