Skip to content

Commit 4694513

Browse files
anonrigaduh95
authored andcommitted
src: use std::string_view for process emit fns
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>
1 parent a8118fc commit 4694513

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

src/node_process.h

+11-9
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,27 @@ void CreateEnvProxyTemplate(IsolateData* isolate_data);
2424
void RawDebug(const v8::FunctionCallbackInfo<v8::Value>& args);
2525

2626
v8::MaybeLocal<v8::Value> ProcessEmit(Environment* env,
27-
const char* event,
27+
std::string_view event,
2828
v8::Local<v8::Value> message);
2929

3030
v8::Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
31-
const char* warning,
32-
const char* type = nullptr,
33-
const char* code = nullptr);
31+
std::string_view warning,
32+
std::string_view type = "",
33+
std::string_view code = "");
3434

3535
template <typename... Args>
3636
inline v8::Maybe<bool> ProcessEmitWarning(Environment* env,
3737
const char* fmt,
3838
Args&&... args);
3939

40-
v8::Maybe<bool> ProcessEmitWarningSync(Environment* env, const char* message);
40+
v8::Maybe<bool> ProcessEmitWarningSync(Environment* env,
41+
std::string_view message);
4142
v8::Maybe<bool> ProcessEmitExperimentalWarning(Environment* env,
42-
const char* warning);
43-
v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
44-
const char* warning,
45-
const char* deprecation_code);
43+
const std::string& warning);
44+
v8::Maybe<bool> ProcessEmitDeprecationWarning(
45+
Environment* env,
46+
const std::string& warning,
47+
std::string_view deprecation_code);
4648

4749
v8::MaybeLocal<v8::Object> CreateProcessObject(Realm* env);
4850
void PatchProcessObject(const v8::FunctionCallbackInfo<v8::Value>& args);

src/node_process_events.cc

+24-25
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ using v8::Object;
1818
using v8::String;
1919
using v8::Value;
2020

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

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

3940
MaybeLocal<Value> ProcessEmit(Environment* env,
40-
const char* event,
41+
std::string_view event,
4142
Local<Value> message) {
4243
Isolate* isolate = env->isolate();
4344

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

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

5355
Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
54-
const char* warning,
55-
const char* type,
56-
const char* code) {
57-
if (!env->can_call_into_js()) return Just(false);
56+
std::string_view warning,
57+
std::string_view type,
58+
std::string_view code) {
59+
if (!env->can_call_into_js()) {
60+
return Just(false);
61+
}
5862

5963
HandleScope handle_scope(env->isolate());
6064
Context::Scope context_scope(env->context());
@@ -73,19 +77,16 @@ Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
7377

7478
// The caller has to be able to handle a failure anyway, so we might as well
7579
// do proper error checking for string creation.
76-
if (!String::NewFromUtf8(env->isolate(), warning).ToLocal(&args[argc++]))
80+
if (!ToV8Value(env->context(), warning).ToLocal(&args[argc++])) {
7781
return Nothing<bool>();
82+
}
7883

79-
if (type != nullptr) {
80-
if (!String::NewFromOneByte(env->isolate(),
81-
reinterpret_cast<const uint8_t*>(type))
82-
.ToLocal(&args[argc++])) {
84+
if (!type.empty()) {
85+
if (!ToV8Value(env->context(), type).ToLocal(&args[argc++])) {
8386
return Nothing<bool>();
8487
}
85-
if (code != nullptr &&
86-
!String::NewFromOneByte(env->isolate(),
87-
reinterpret_cast<const uint8_t*>(code))
88-
.ToLocal(&args[argc++])) {
88+
if (!code.empty() &&
89+
!ToV8Value(env->context(), code).ToLocal(&args[argc++])) {
8990
return Nothing<bool>();
9091
}
9192
}
@@ -100,13 +101,11 @@ Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
100101
return Just(true);
101102
}
102103

103-
104104
std::set<std::string> experimental_warnings;
105105

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

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

117116
Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
118-
const char* warning,
119-
const char* deprecation_code) {
117+
const std::string& warning,
118+
std::string_view deprecation_code) {
120119
return ProcessEmitWarningGeneric(
121120
env, warning, "DeprecationWarning", deprecation_code);
122121
}

0 commit comments

Comments
 (0)